library_query.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387
  1. /*
  2. * ***** BEGIN GPL LICENSE BLOCK *****
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software Foundation,
  16. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. *
  18. * The Original Code is Copyright (C) 2014 by Blender Foundation.
  19. * All rights reserved.
  20. *
  21. * Contributor(s): Sergey Sharybin.
  22. *
  23. * ***** END GPL LICENSE BLOCK *****
  24. */
  25. /** \file blender/blenkernel/intern/library_query.c
  26. * \ingroup bke
  27. */
  28. #include <stdlib.h>
  29. #include "MEM_guardedalloc.h"
  30. #include "DNA_actuator_types.h"
  31. #include "DNA_anim_types.h"
  32. #include "DNA_armature_types.h"
  33. #include "DNA_brush_types.h"
  34. #include "DNA_camera_types.h"
  35. #include "DNA_constraint_types.h"
  36. #include "DNA_controller_types.h"
  37. #include "DNA_group_types.h"
  38. #include "DNA_gpencil_types.h"
  39. #include "DNA_key_types.h"
  40. #include "DNA_lamp_types.h"
  41. #include "DNA_lattice_types.h"
  42. #include "DNA_linestyle_types.h"
  43. #include "DNA_material_types.h"
  44. #include "DNA_mesh_types.h"
  45. #include "DNA_meshdata_types.h"
  46. #include "DNA_meta_types.h"
  47. #include "DNA_movieclip_types.h"
  48. #include "DNA_mask_types.h"
  49. #include "DNA_node_types.h"
  50. #include "DNA_object_force.h"
  51. #include "DNA_rigidbody_types.h"
  52. #include "DNA_scene_types.h"
  53. #include "DNA_sensor_types.h"
  54. #include "DNA_sequence_types.h"
  55. #include "DNA_screen_types.h"
  56. #include "DNA_speaker_types.h"
  57. #include "DNA_sound_types.h"
  58. #include "DNA_text_types.h"
  59. #include "DNA_vfont_types.h"
  60. #include "DNA_world_types.h"
  61. #include "BLI_utildefines.h"
  62. #include "BLI_listbase.h"
  63. #include "BLI_ghash.h"
  64. #include "BLI_linklist_stack.h"
  65. #include "BKE_animsys.h"
  66. #include "BKE_constraint.h"
  67. #include "BKE_fcurve.h"
  68. #include "BKE_idprop.h"
  69. #include "BKE_library.h"
  70. #include "BKE_library_query.h"
  71. #include "BKE_main.h"
  72. #include "BKE_modifier.h"
  73. #include "BKE_node.h"
  74. #include "BKE_particle.h"
  75. #include "BKE_rigidbody.h"
  76. #include "BKE_sca.h"
  77. #include "BKE_sequencer.h"
  78. #include "BKE_tracking.h"
  79. #define FOREACH_FINALIZE _finalize
  80. #define FOREACH_FINALIZE_VOID \
  81. if (0) { goto FOREACH_FINALIZE; } \
  82. FOREACH_FINALIZE: ((void)0)
  83. #define FOREACH_CALLBACK_INVOKE_ID_PP(_data, id_pp, _cb_flag) \
  84. CHECK_TYPE(id_pp, ID **); \
  85. if (!((_data)->status & IDWALK_STOP)) { \
  86. const int _flag = (_data)->flag; \
  87. ID *old_id = *(id_pp); \
  88. const int callback_return = (_data)->callback((_data)->user_data, (_data)->self_id, id_pp, _cb_flag | (_data)->cb_flag); \
  89. if (_flag & IDWALK_READONLY) { \
  90. BLI_assert(*(id_pp) == old_id); \
  91. } \
  92. if (old_id && (_flag & IDWALK_RECURSE)) { \
  93. if (!BLI_gset_haskey((_data)->ids_handled, old_id)) { \
  94. BLI_gset_add((_data)->ids_handled, old_id); \
  95. if (!(callback_return & IDWALK_RET_STOP_RECURSION)) { \
  96. BLI_LINKSTACK_PUSH((_data)->ids_todo, old_id); \
  97. } \
  98. } \
  99. } \
  100. if (callback_return & IDWALK_RET_STOP_ITER) { \
  101. (_data)->status |= IDWALK_STOP; \
  102. goto FOREACH_FINALIZE; \
  103. } \
  104. } \
  105. else { \
  106. goto FOREACH_FINALIZE; \
  107. } ((void)0)
  108. #define FOREACH_CALLBACK_INVOKE_ID(_data, id, cb_flag) \
  109. { \
  110. CHECK_TYPE_ANY(id, ID *, void *); \
  111. FOREACH_CALLBACK_INVOKE_ID_PP(_data, (ID **)&(id), cb_flag); \
  112. } ((void)0)
  113. #define FOREACH_CALLBACK_INVOKE(_data, id_super, cb_flag) \
  114. { \
  115. CHECK_TYPE(&((id_super)->id), ID *); \
  116. FOREACH_CALLBACK_INVOKE_ID_PP(_data, (ID **)&(id_super), cb_flag); \
  117. } ((void)0)
  118. /* status */
  119. enum {
  120. IDWALK_STOP = 1 << 0,
  121. };
  122. typedef struct LibraryForeachIDData {
  123. ID *self_id;
  124. int flag;
  125. int cb_flag;
  126. LibraryIDLinkCallback callback;
  127. void *user_data;
  128. int status;
  129. /* To handle recursion. */
  130. GSet *ids_handled; /* All IDs that are either already done, or still in ids_todo stack. */
  131. BLI_LINKSTACK_DECLARE(ids_todo, ID *);
  132. } LibraryForeachIDData;
  133. static void library_foreach_idproperty_ID_link(LibraryForeachIDData *data, IDProperty *prop, int flag)
  134. {
  135. if (!prop)
  136. return;
  137. switch (prop->type) {
  138. case IDP_GROUP:
  139. {
  140. for (IDProperty *loop = prop->data.group.first; loop; loop = loop->next) {
  141. library_foreach_idproperty_ID_link(data, loop, flag);
  142. }
  143. break;
  144. }
  145. case IDP_IDPARRAY:
  146. {
  147. IDProperty *loop = IDP_Array(prop);
  148. for (int i = 0; i < prop->len; i++) {
  149. library_foreach_idproperty_ID_link(data, &loop[i], flag);
  150. }
  151. break;
  152. }
  153. case IDP_ID:
  154. FOREACH_CALLBACK_INVOKE_ID(data, prop->data.pointer, flag);
  155. break;
  156. default:
  157. break; /* Nothing to do here with other types of IDProperties... */
  158. }
  159. FOREACH_FINALIZE_VOID;
  160. }
  161. static void library_foreach_rigidbodyworldSceneLooper(
  162. struct RigidBodyWorld *UNUSED(rbw), ID **id_pointer, void *user_data, int cb_flag)
  163. {
  164. LibraryForeachIDData *data = (LibraryForeachIDData *) user_data;
  165. FOREACH_CALLBACK_INVOKE_ID_PP(data, id_pointer, cb_flag);
  166. FOREACH_FINALIZE_VOID;
  167. }
  168. static void library_foreach_modifiersForeachIDLink(
  169. void *user_data, Object *UNUSED(object), ID **id_pointer, int cb_flag)
  170. {
  171. LibraryForeachIDData *data = (LibraryForeachIDData *) user_data;
  172. FOREACH_CALLBACK_INVOKE_ID_PP(data, id_pointer, cb_flag);
  173. FOREACH_FINALIZE_VOID;
  174. }
  175. static void library_foreach_constraintObjectLooper(bConstraint *UNUSED(con), ID **id_pointer,
  176. bool is_reference, void *user_data)
  177. {
  178. LibraryForeachIDData *data = (LibraryForeachIDData *) user_data;
  179. const int cb_flag = is_reference ? IDWALK_CB_USER : IDWALK_CB_NOP;
  180. FOREACH_CALLBACK_INVOKE_ID_PP(data, id_pointer, cb_flag);
  181. FOREACH_FINALIZE_VOID;
  182. }
  183. static void library_foreach_particlesystemsObjectLooper(
  184. ParticleSystem *UNUSED(psys), ID **id_pointer, void *user_data, int cb_flag)
  185. {
  186. LibraryForeachIDData *data = (LibraryForeachIDData *) user_data;
  187. FOREACH_CALLBACK_INVOKE_ID_PP(data, id_pointer, cb_flag);
  188. FOREACH_FINALIZE_VOID;
  189. }
  190. static void library_foreach_sensorsObjectLooper(
  191. bSensor *UNUSED(sensor), ID **id_pointer, void *user_data, int cb_flag)
  192. {
  193. LibraryForeachIDData *data = (LibraryForeachIDData *) user_data;
  194. FOREACH_CALLBACK_INVOKE_ID_PP(data, id_pointer, cb_flag);
  195. FOREACH_FINALIZE_VOID;
  196. }
  197. static void library_foreach_controllersObjectLooper(
  198. bController *UNUSED(controller), ID **id_pointer, void *user_data, int cb_flag)
  199. {
  200. LibraryForeachIDData *data = (LibraryForeachIDData *) user_data;
  201. FOREACH_CALLBACK_INVOKE_ID_PP(data, id_pointer, cb_flag);
  202. FOREACH_FINALIZE_VOID;
  203. }
  204. static void library_foreach_actuatorsObjectLooper(
  205. bActuator *UNUSED(actuator), ID **id_pointer, void *user_data, int cb_flag)
  206. {
  207. LibraryForeachIDData *data = (LibraryForeachIDData *) user_data;
  208. FOREACH_CALLBACK_INVOKE_ID_PP(data, id_pointer, cb_flag);
  209. FOREACH_FINALIZE_VOID;
  210. }
  211. static void library_foreach_nla_strip(LibraryForeachIDData *data, NlaStrip *strip)
  212. {
  213. NlaStrip *substrip;
  214. FOREACH_CALLBACK_INVOKE(data, strip->act, IDWALK_CB_USER);
  215. for (substrip = strip->strips.first; substrip; substrip = substrip->next) {
  216. library_foreach_nla_strip(data, substrip);
  217. }
  218. FOREACH_FINALIZE_VOID;
  219. }
  220. static void library_foreach_animationData(LibraryForeachIDData *data, AnimData *adt)
  221. {
  222. FCurve *fcu;
  223. NlaTrack *nla_track;
  224. NlaStrip *nla_strip;
  225. for (fcu = adt->drivers.first; fcu; fcu = fcu->next) {
  226. ChannelDriver *driver = fcu->driver;
  227. DriverVar *dvar;
  228. for (dvar = driver->variables.first; dvar; dvar = dvar->next) {
  229. /* only used targets */
  230. DRIVER_TARGETS_USED_LOOPER(dvar)
  231. {
  232. FOREACH_CALLBACK_INVOKE_ID(data, dtar->id, IDWALK_CB_NOP);
  233. }
  234. DRIVER_TARGETS_LOOPER_END
  235. }
  236. }
  237. FOREACH_CALLBACK_INVOKE(data, adt->action, IDWALK_CB_USER);
  238. FOREACH_CALLBACK_INVOKE(data, adt->tmpact, IDWALK_CB_USER);
  239. for (nla_track = adt->nla_tracks.first; nla_track; nla_track = nla_track->next) {
  240. for (nla_strip = nla_track->strips.first; nla_strip; nla_strip = nla_strip->next) {
  241. library_foreach_nla_strip(data, nla_strip);
  242. }
  243. }
  244. FOREACH_FINALIZE_VOID;
  245. }
  246. static void library_foreach_mtex(LibraryForeachIDData *data, MTex *mtex)
  247. {
  248. FOREACH_CALLBACK_INVOKE(data, mtex->object, IDWALK_CB_NOP);
  249. FOREACH_CALLBACK_INVOKE(data, mtex->tex, IDWALK_CB_USER);
  250. FOREACH_FINALIZE_VOID;
  251. }
  252. static void library_foreach_paint(LibraryForeachIDData *data, Paint *paint)
  253. {
  254. FOREACH_CALLBACK_INVOKE(data, paint->brush, IDWALK_CB_USER);
  255. FOREACH_CALLBACK_INVOKE(data, paint->palette, IDWALK_CB_USER);
  256. FOREACH_FINALIZE_VOID;
  257. }
  258. static void library_foreach_bone(LibraryForeachIDData *data, Bone *bone)
  259. {
  260. library_foreach_idproperty_ID_link(data, bone->prop, IDWALK_CB_USER);
  261. for (Bone *curbone = bone->childbase.first; curbone; curbone = curbone->next) {
  262. library_foreach_bone(data, curbone);
  263. }
  264. FOREACH_FINALIZE_VOID;
  265. }
  266. static void library_foreach_ID_as_subdata_link(
  267. ID **id_pp, LibraryIDLinkCallback callback, void *user_data, int flag, LibraryForeachIDData *data)
  268. {
  269. /* Needed e.g. for callbacks handling relationships... This call shall be absolutely readonly. */
  270. ID *id = *id_pp;
  271. FOREACH_CALLBACK_INVOKE_ID_PP(data, id_pp, IDWALK_CB_PRIVATE);
  272. BLI_assert(id == *id_pp);
  273. if (flag & IDWALK_RECURSE) {
  274. /* Defer handling into main loop, recursively calling BKE_library_foreach_ID_link in IDWALK_RECURSE case is
  275. * troublesome, see T49553. */
  276. if (!BLI_gset_haskey(data->ids_handled, id)) {
  277. BLI_gset_add(data->ids_handled, id);
  278. BLI_LINKSTACK_PUSH(data->ids_todo, id);
  279. }
  280. }
  281. else {
  282. BKE_library_foreach_ID_link(NULL, id, callback, user_data, flag);
  283. }
  284. FOREACH_FINALIZE_VOID;
  285. }
  286. /**
  287. * Loop over all of the ID's this datablock links to.
  288. *
  289. * \note: May be extended to be recursive in the future.
  290. */
  291. void BKE_library_foreach_ID_link(Main *bmain, ID *id, LibraryIDLinkCallback callback, void *user_data, int flag)
  292. {
  293. LibraryForeachIDData data;
  294. int i;
  295. if (flag & IDWALK_RECURSE) {
  296. /* For now, recusion implies read-only. */
  297. flag |= IDWALK_READONLY;
  298. data.ids_handled = BLI_gset_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, __func__);
  299. BLI_LINKSTACK_INIT(data.ids_todo);
  300. BLI_gset_add(data.ids_handled, id);
  301. }
  302. else {
  303. data.ids_handled = NULL;
  304. }
  305. data.flag = flag;
  306. data.status = 0;
  307. data.callback = callback;
  308. data.user_data = user_data;
  309. #define CALLBACK_INVOKE_ID(check_id, cb_flag) \
  310. FOREACH_CALLBACK_INVOKE_ID(&data, check_id, cb_flag)
  311. #define CALLBACK_INVOKE(check_id_super, cb_flag) \
  312. FOREACH_CALLBACK_INVOKE(&data, check_id_super, cb_flag)
  313. for (; id != NULL; id = (flag & IDWALK_RECURSE) ? BLI_LINKSTACK_POP(data.ids_todo) : NULL) {
  314. data.self_id = id;
  315. data.cb_flag = ID_IS_LINKED_DATABLOCK(id) ? IDWALK_CB_INDIRECT_USAGE : 0;
  316. if (bmain != NULL && bmain->relations != NULL && (flag & IDWALK_READONLY)) {
  317. /* Note that this is minor optimization, even in worst cases (like id being an object with lots of
  318. * drivers and constraints and modifiers, or material etc. with huge node tree),
  319. * but we might as well use it (Main->relations is always assumed valid, it's responsability of code
  320. * creating it to free it, especially if/when it starts modifying Main database). */
  321. MainIDRelationsEntry *entry = BLI_ghash_lookup(bmain->relations->id_user_to_used, id);
  322. for (; entry != NULL; entry = entry->next) {
  323. FOREACH_CALLBACK_INVOKE_ID_PP(&data, entry->id_pointer, entry->usage_flag);
  324. }
  325. continue;
  326. }
  327. library_foreach_idproperty_ID_link(&data, id->properties, IDWALK_CB_USER);
  328. AnimData *adt = BKE_animdata_from_id(id);
  329. if (adt) {
  330. library_foreach_animationData(&data, adt);
  331. }
  332. switch ((ID_Type)GS(id->name)) {
  333. case ID_LI:
  334. {
  335. Library *lib = (Library *) id;
  336. CALLBACK_INVOKE(lib->parent, IDWALK_CB_NOP);
  337. break;
  338. }
  339. case ID_SCE:
  340. {
  341. Scene *scene = (Scene *) id;
  342. ToolSettings *toolsett = scene->toolsettings;
  343. SceneRenderLayer *srl;
  344. Base *base;
  345. CALLBACK_INVOKE(scene->camera, IDWALK_CB_NOP);
  346. CALLBACK_INVOKE(scene->world, IDWALK_CB_USER);
  347. CALLBACK_INVOKE(scene->set, IDWALK_CB_NOP);
  348. CALLBACK_INVOKE(scene->clip, IDWALK_CB_USER);
  349. if (scene->nodetree) {
  350. /* nodetree **are owned by IDs**, treat them as mere sub-data and not real ID! */
  351. library_foreach_ID_as_subdata_link((ID **)&scene->nodetree, callback, user_data, flag, &data);
  352. }
  353. /* DO NOT handle scene->basact here, it's doubling with the loop over whole scene->base later,
  354. * since basact is just a pointer to one of those items. */
  355. CALLBACK_INVOKE(scene->obedit, IDWALK_CB_NOP);
  356. for (srl = scene->r.layers.first; srl; srl = srl->next) {
  357. FreestyleModuleConfig *fmc;
  358. FreestyleLineSet *fls;
  359. if (srl->mat_override) {
  360. CALLBACK_INVOKE(srl->mat_override, IDWALK_CB_USER);
  361. }
  362. if (srl->light_override) {
  363. CALLBACK_INVOKE(srl->light_override, IDWALK_CB_USER);
  364. }
  365. for (fmc = srl->freestyleConfig.modules.first; fmc; fmc = fmc->next) {
  366. if (fmc->script) {
  367. CALLBACK_INVOKE(fmc->script, IDWALK_CB_NOP);
  368. }
  369. }
  370. for (fls = srl->freestyleConfig.linesets.first; fls; fls = fls->next) {
  371. if (fls->group) {
  372. CALLBACK_INVOKE(fls->group, IDWALK_CB_USER);
  373. }
  374. if (fls->linestyle) {
  375. CALLBACK_INVOKE(fls->linestyle, IDWALK_CB_USER);
  376. }
  377. }
  378. }
  379. if (scene->ed) {
  380. Sequence *seq;
  381. SEQP_BEGIN(scene->ed, seq)
  382. {
  383. CALLBACK_INVOKE(seq->scene, IDWALK_CB_NOP);
  384. CALLBACK_INVOKE(seq->scene_camera, IDWALK_CB_NOP);
  385. CALLBACK_INVOKE(seq->clip, IDWALK_CB_USER);
  386. CALLBACK_INVOKE(seq->mask, IDWALK_CB_USER);
  387. CALLBACK_INVOKE(seq->sound, IDWALK_CB_USER);
  388. library_foreach_idproperty_ID_link(&data, seq->prop, IDWALK_CB_USER);
  389. for (SequenceModifierData *smd = seq->modifiers.first; smd; smd = smd->next) {
  390. CALLBACK_INVOKE(smd->mask_id, IDWALK_CB_USER);
  391. }
  392. }
  393. SEQ_END
  394. }
  395. CALLBACK_INVOKE(scene->gpd, IDWALK_CB_USER);
  396. for (base = scene->base.first; base; base = base->next) {
  397. CALLBACK_INVOKE(base->object, IDWALK_CB_USER);
  398. }
  399. for (TimeMarker *marker = scene->markers.first; marker; marker = marker->next) {
  400. CALLBACK_INVOKE(marker->camera, IDWALK_CB_NOP);
  401. }
  402. if (toolsett) {
  403. CALLBACK_INVOKE(toolsett->skgen_template, IDWALK_CB_NOP);
  404. CALLBACK_INVOKE(toolsett->particle.scene, IDWALK_CB_NOP);
  405. CALLBACK_INVOKE(toolsett->particle.object, IDWALK_CB_NOP);
  406. CALLBACK_INVOKE(toolsett->particle.shape_object, IDWALK_CB_NOP);
  407. library_foreach_paint(&data, &toolsett->imapaint.paint);
  408. CALLBACK_INVOKE(toolsett->imapaint.stencil, IDWALK_CB_USER);
  409. CALLBACK_INVOKE(toolsett->imapaint.clone, IDWALK_CB_USER);
  410. CALLBACK_INVOKE(toolsett->imapaint.canvas, IDWALK_CB_USER);
  411. if (toolsett->vpaint) {
  412. library_foreach_paint(&data, &toolsett->vpaint->paint);
  413. }
  414. if (toolsett->wpaint) {
  415. library_foreach_paint(&data, &toolsett->wpaint->paint);
  416. }
  417. if (toolsett->sculpt) {
  418. library_foreach_paint(&data, &toolsett->sculpt->paint);
  419. CALLBACK_INVOKE(toolsett->sculpt->gravity_object, IDWALK_CB_NOP);
  420. }
  421. if (toolsett->uvsculpt) {
  422. library_foreach_paint(&data, &toolsett->uvsculpt->paint);
  423. }
  424. }
  425. if (scene->rigidbody_world) {
  426. BKE_rigidbody_world_id_loop(scene->rigidbody_world, library_foreach_rigidbodyworldSceneLooper, &data);
  427. }
  428. CALLBACK_INVOKE(scene->gm.dome.warptext, IDWALK_CB_NOP);
  429. break;
  430. }
  431. case ID_OB:
  432. {
  433. Object *object = (Object *) id;
  434. ParticleSystem *psys;
  435. /* Object is special, proxies make things hard... */
  436. const int data_cb_flag = data.cb_flag;
  437. const int proxy_cb_flag = ((data.flag & IDWALK_NO_INDIRECT_PROXY_DATA_USAGE) == 0 && (object->proxy || object->proxy_group)) ?
  438. IDWALK_CB_INDIRECT_USAGE : 0;
  439. /* object data special case */
  440. data.cb_flag |= proxy_cb_flag;
  441. if (object->type == OB_EMPTY) {
  442. /* empty can have NULL or Image */
  443. CALLBACK_INVOKE_ID(object->data, IDWALK_CB_USER);
  444. }
  445. else {
  446. /* when set, this can't be NULL */
  447. if (object->data) {
  448. CALLBACK_INVOKE_ID(object->data, IDWALK_CB_USER | IDWALK_CB_NEVER_NULL);
  449. }
  450. }
  451. data.cb_flag = data_cb_flag;
  452. CALLBACK_INVOKE(object->parent, IDWALK_CB_NOP);
  453. CALLBACK_INVOKE(object->track, IDWALK_CB_NOP);
  454. /* object->proxy is refcounted, but not object->proxy_group... *sigh* */
  455. CALLBACK_INVOKE(object->proxy, IDWALK_CB_USER);
  456. CALLBACK_INVOKE(object->proxy_group, IDWALK_CB_NOP);
  457. /* Special case!
  458. * Since this field is set/owned by 'user' of this ID (and not ID itself), it is only indirect usage
  459. * if proxy object is linked... Twisted. */
  460. if (object->proxy_from) {
  461. data.cb_flag = ID_IS_LINKED_DATABLOCK(object->proxy_from) ? IDWALK_CB_INDIRECT_USAGE : 0;
  462. }
  463. CALLBACK_INVOKE(object->proxy_from, IDWALK_CB_LOOPBACK);
  464. data.cb_flag = data_cb_flag;
  465. CALLBACK_INVOKE(object->poselib, IDWALK_CB_USER);
  466. data.cb_flag |= proxy_cb_flag;
  467. for (i = 0; i < object->totcol; i++) {
  468. CALLBACK_INVOKE(object->mat[i], IDWALK_CB_USER);
  469. }
  470. data.cb_flag = data_cb_flag;
  471. CALLBACK_INVOKE(object->gpd, IDWALK_CB_USER);
  472. CALLBACK_INVOKE(object->dup_group, IDWALK_CB_USER);
  473. if (object->pd) {
  474. CALLBACK_INVOKE(object->pd->tex, IDWALK_CB_USER);
  475. CALLBACK_INVOKE(object->pd->f_source, IDWALK_CB_NOP);
  476. }
  477. /* Note that ob->effect is deprecated, so no need to handle it here. */
  478. if (object->pose) {
  479. bPoseChannel *pchan;
  480. data.cb_flag |= proxy_cb_flag;
  481. for (pchan = object->pose->chanbase.first; pchan; pchan = pchan->next) {
  482. library_foreach_idproperty_ID_link(&data, pchan->prop, IDWALK_CB_USER);
  483. CALLBACK_INVOKE(pchan->custom, IDWALK_CB_USER);
  484. BKE_constraints_id_loop(&pchan->constraints, library_foreach_constraintObjectLooper, &data);
  485. }
  486. data.cb_flag = data_cb_flag;
  487. }
  488. if (object->rigidbody_constraint) {
  489. CALLBACK_INVOKE(object->rigidbody_constraint->ob1, IDWALK_CB_NOP);
  490. CALLBACK_INVOKE(object->rigidbody_constraint->ob2, IDWALK_CB_NOP);
  491. }
  492. if (object->lodlevels.first) {
  493. LodLevel *level;
  494. for (level = object->lodlevels.first; level; level = level->next) {
  495. CALLBACK_INVOKE(level->source, IDWALK_CB_NOP);
  496. }
  497. }
  498. modifiers_foreachIDLink(object, library_foreach_modifiersForeachIDLink, &data);
  499. BKE_constraints_id_loop(&object->constraints, library_foreach_constraintObjectLooper, &data);
  500. for (psys = object->particlesystem.first; psys; psys = psys->next) {
  501. BKE_particlesystem_id_loop(psys, library_foreach_particlesystemsObjectLooper, &data);
  502. }
  503. if (object->soft) {
  504. CALLBACK_INVOKE(object->soft->collision_group, IDWALK_CB_NOP);
  505. if (object->soft->effector_weights) {
  506. CALLBACK_INVOKE(object->soft->effector_weights->group, IDWALK_CB_NOP);
  507. }
  508. }
  509. BKE_sca_sensors_id_loop(&object->sensors, library_foreach_sensorsObjectLooper, &data);
  510. BKE_sca_controllers_id_loop(&object->controllers, library_foreach_controllersObjectLooper, &data);
  511. BKE_sca_actuators_id_loop(&object->actuators, library_foreach_actuatorsObjectLooper, &data);
  512. break;
  513. }
  514. case ID_AR:
  515. {
  516. bArmature *arm = (bArmature *)id;
  517. for (Bone *bone = arm->bonebase.first; bone; bone = bone->next) {
  518. library_foreach_bone(&data, bone);
  519. }
  520. break;
  521. }
  522. case ID_ME:
  523. {
  524. Mesh *mesh = (Mesh *) id;
  525. CALLBACK_INVOKE(mesh->texcomesh, IDWALK_CB_USER);
  526. CALLBACK_INVOKE(mesh->key, IDWALK_CB_USER);
  527. for (i = 0; i < mesh->totcol; i++) {
  528. CALLBACK_INVOKE(mesh->mat[i], IDWALK_CB_USER);
  529. }
  530. /* XXX Really not happy with this - probably texface should rather use some kind of
  531. * 'texture slots' and just set indices in each poly/face item - would also save some memory.
  532. * Maybe a nice TODO for blender2.8? */
  533. if (mesh->mtface || mesh->mtpoly) {
  534. for (i = 0; i < mesh->pdata.totlayer; i++) {
  535. if (mesh->pdata.layers[i].type == CD_MTEXPOLY) {
  536. MTexPoly *txface = (MTexPoly *)mesh->pdata.layers[i].data;
  537. for (int j = 0; j < mesh->totpoly; j++, txface++) {
  538. CALLBACK_INVOKE(txface->tpage, IDWALK_CB_USER_ONE);
  539. }
  540. }
  541. }
  542. for (i = 0; i < mesh->fdata.totlayer; i++) {
  543. if (mesh->fdata.layers[i].type == CD_MTFACE) {
  544. MTFace *tface = (MTFace *)mesh->fdata.layers[i].data;
  545. for (int j = 0; j < mesh->totface; j++, tface++) {
  546. CALLBACK_INVOKE(tface->tpage, IDWALK_CB_USER_ONE);
  547. }
  548. }
  549. }
  550. }
  551. break;
  552. }
  553. case ID_CU:
  554. {
  555. Curve *curve = (Curve *) id;
  556. CALLBACK_INVOKE(curve->bevobj, IDWALK_CB_NOP);
  557. CALLBACK_INVOKE(curve->taperobj, IDWALK_CB_NOP);
  558. CALLBACK_INVOKE(curve->textoncurve, IDWALK_CB_NOP);
  559. CALLBACK_INVOKE(curve->key, IDWALK_CB_USER);
  560. for (i = 0; i < curve->totcol; i++) {
  561. CALLBACK_INVOKE(curve->mat[i], IDWALK_CB_USER);
  562. }
  563. CALLBACK_INVOKE(curve->vfont, IDWALK_CB_USER);
  564. CALLBACK_INVOKE(curve->vfontb, IDWALK_CB_USER);
  565. CALLBACK_INVOKE(curve->vfonti, IDWALK_CB_USER);
  566. CALLBACK_INVOKE(curve->vfontbi, IDWALK_CB_USER);
  567. break;
  568. }
  569. case ID_MB:
  570. {
  571. MetaBall *metaball = (MetaBall *) id;
  572. for (i = 0; i < metaball->totcol; i++) {
  573. CALLBACK_INVOKE(metaball->mat[i], IDWALK_CB_USER);
  574. }
  575. break;
  576. }
  577. case ID_MA:
  578. {
  579. Material *material = (Material *) id;
  580. for (i = 0; i < MAX_MTEX; i++) {
  581. if (material->mtex[i]) {
  582. library_foreach_mtex(&data, material->mtex[i]);
  583. }
  584. }
  585. if (material->nodetree) {
  586. /* nodetree **are owned by IDs**, treat them as mere sub-data and not real ID! */
  587. library_foreach_ID_as_subdata_link((ID **)&material->nodetree, callback, user_data, flag, &data);
  588. }
  589. CALLBACK_INVOKE(material->group, IDWALK_CB_USER);
  590. break;
  591. }
  592. case ID_TE:
  593. {
  594. Tex *texture = (Tex *) id;
  595. if (texture->nodetree) {
  596. /* nodetree **are owned by IDs**, treat them as mere sub-data and not real ID! */
  597. library_foreach_ID_as_subdata_link((ID **)&texture->nodetree, callback, user_data, flag, &data);
  598. }
  599. CALLBACK_INVOKE(texture->ima, IDWALK_CB_USER);
  600. if (texture->env) {
  601. CALLBACK_INVOKE(texture->env->object, IDWALK_CB_NOP);
  602. CALLBACK_INVOKE(texture->env->ima, IDWALK_CB_USER);
  603. }
  604. if (texture->pd)
  605. CALLBACK_INVOKE(texture->pd->object, IDWALK_CB_NOP);
  606. if (texture->vd)
  607. CALLBACK_INVOKE(texture->vd->object, IDWALK_CB_NOP);
  608. if (texture->ot)
  609. CALLBACK_INVOKE(texture->ot->object, IDWALK_CB_NOP);
  610. break;
  611. }
  612. case ID_LT:
  613. {
  614. Lattice *lattice = (Lattice *) id;
  615. CALLBACK_INVOKE(lattice->key, IDWALK_CB_USER);
  616. break;
  617. }
  618. case ID_LA:
  619. {
  620. Lamp *lamp = (Lamp *) id;
  621. for (i = 0; i < MAX_MTEX; i++) {
  622. if (lamp->mtex[i]) {
  623. library_foreach_mtex(&data, lamp->mtex[i]);
  624. }
  625. }
  626. if (lamp->nodetree) {
  627. /* nodetree **are owned by IDs**, treat them as mere sub-data and not real ID! */
  628. library_foreach_ID_as_subdata_link((ID **)&lamp->nodetree, callback, user_data, flag, &data);
  629. }
  630. break;
  631. }
  632. case ID_CA:
  633. {
  634. Camera *camera = (Camera *) id;
  635. CALLBACK_INVOKE(camera->dof_ob, IDWALK_CB_NOP);
  636. break;
  637. }
  638. case ID_KE:
  639. {
  640. Key *key = (Key *) id;
  641. CALLBACK_INVOKE_ID(key->from, IDWALK_CB_LOOPBACK);
  642. break;
  643. }
  644. case ID_SCR:
  645. {
  646. bScreen *screen = (bScreen *) id;
  647. CALLBACK_INVOKE(screen->scene, IDWALK_CB_USER_ONE);
  648. break;
  649. }
  650. case ID_WO:
  651. {
  652. World *world = (World *) id;
  653. for (i = 0; i < MAX_MTEX; i++) {
  654. if (world->mtex[i]) {
  655. library_foreach_mtex(&data, world->mtex[i]);
  656. }
  657. }
  658. if (world->nodetree) {
  659. /* nodetree **are owned by IDs**, treat them as mere sub-data and not real ID! */
  660. library_foreach_ID_as_subdata_link((ID **)&world->nodetree, callback, user_data, flag, &data);
  661. }
  662. break;
  663. }
  664. case ID_SPK:
  665. {
  666. Speaker *speaker = (Speaker *) id;
  667. CALLBACK_INVOKE(speaker->sound, IDWALK_CB_USER);
  668. break;
  669. }
  670. case ID_GR:
  671. {
  672. Group *group = (Group *) id;
  673. GroupObject *gob;
  674. for (gob = group->gobject.first; gob; gob = gob->next) {
  675. CALLBACK_INVOKE(gob->ob, IDWALK_CB_USER_ONE);
  676. }
  677. break;
  678. }
  679. case ID_NT:
  680. {
  681. bNodeTree *ntree = (bNodeTree *) id;
  682. bNode *node;
  683. bNodeSocket *sock;
  684. CALLBACK_INVOKE(ntree->gpd, IDWALK_CB_USER);
  685. for (node = ntree->nodes.first; node; node = node->next) {
  686. CALLBACK_INVOKE_ID(node->id, IDWALK_CB_USER);
  687. library_foreach_idproperty_ID_link(&data, node->prop, IDWALK_CB_USER);
  688. for (sock = node->inputs.first; sock; sock = sock->next) {
  689. library_foreach_idproperty_ID_link(&data, sock->prop, IDWALK_CB_USER);
  690. }
  691. for (sock = node->outputs.first; sock; sock = sock->next) {
  692. library_foreach_idproperty_ID_link(&data, sock->prop, IDWALK_CB_USER);
  693. }
  694. }
  695. for (sock = ntree->inputs.first; sock; sock = sock->next) {
  696. library_foreach_idproperty_ID_link(&data, sock->prop, IDWALK_CB_USER);
  697. }
  698. for (sock = ntree->outputs.first; sock; sock = sock->next) {
  699. library_foreach_idproperty_ID_link(&data, sock->prop, IDWALK_CB_USER);
  700. }
  701. break;
  702. }
  703. case ID_BR:
  704. {
  705. Brush *brush = (Brush *) id;
  706. CALLBACK_INVOKE(brush->toggle_brush, IDWALK_CB_NOP);
  707. CALLBACK_INVOKE(brush->clone.image, IDWALK_CB_NOP);
  708. CALLBACK_INVOKE(brush->paint_curve, IDWALK_CB_USER);
  709. library_foreach_mtex(&data, &brush->mtex);
  710. library_foreach_mtex(&data, &brush->mask_mtex);
  711. break;
  712. }
  713. case ID_PA:
  714. {
  715. ParticleSettings *psett = (ParticleSettings *) id;
  716. CALLBACK_INVOKE(psett->dup_group, IDWALK_CB_NOP);
  717. CALLBACK_INVOKE(psett->dup_ob, IDWALK_CB_NOP);
  718. CALLBACK_INVOKE(psett->bb_ob, IDWALK_CB_NOP);
  719. CALLBACK_INVOKE(psett->collision_group, IDWALK_CB_NOP);
  720. for (i = 0; i < MAX_MTEX; i++) {
  721. if (psett->mtex[i]) {
  722. library_foreach_mtex(&data, psett->mtex[i]);
  723. }
  724. }
  725. if (psett->effector_weights) {
  726. CALLBACK_INVOKE(psett->effector_weights->group, IDWALK_CB_NOP);
  727. }
  728. if (psett->pd) {
  729. CALLBACK_INVOKE(psett->pd->tex, IDWALK_CB_USER);
  730. CALLBACK_INVOKE(psett->pd->f_source, IDWALK_CB_NOP);
  731. }
  732. if (psett->pd2) {
  733. CALLBACK_INVOKE(psett->pd2->tex, IDWALK_CB_USER);
  734. CALLBACK_INVOKE(psett->pd2->f_source, IDWALK_CB_NOP);
  735. }
  736. if (psett->boids) {
  737. BoidState *state;
  738. BoidRule *rule;
  739. for (state = psett->boids->states.first; state; state = state->next) {
  740. for (rule = state->rules.first; rule; rule = rule->next) {
  741. if (rule->type == eBoidRuleType_Avoid) {
  742. BoidRuleGoalAvoid *gabr = (BoidRuleGoalAvoid *)rule;
  743. CALLBACK_INVOKE(gabr->ob, IDWALK_CB_NOP);
  744. }
  745. else if (rule->type == eBoidRuleType_FollowLeader) {
  746. BoidRuleFollowLeader *flbr = (BoidRuleFollowLeader *)rule;
  747. CALLBACK_INVOKE(flbr->ob, IDWALK_CB_NOP);
  748. }
  749. }
  750. }
  751. }
  752. break;
  753. }
  754. case ID_MC:
  755. {
  756. MovieClip *clip = (MovieClip *) id;
  757. MovieTracking *tracking = &clip->tracking;
  758. MovieTrackingObject *object;
  759. MovieTrackingTrack *track;
  760. MovieTrackingPlaneTrack *plane_track;
  761. CALLBACK_INVOKE(clip->gpd, IDWALK_CB_USER);
  762. for (track = tracking->tracks.first; track; track = track->next) {
  763. CALLBACK_INVOKE(track->gpd, IDWALK_CB_USER);
  764. }
  765. for (object = tracking->objects.first; object; object = object->next) {
  766. for (track = object->tracks.first; track; track = track->next) {
  767. CALLBACK_INVOKE(track->gpd, IDWALK_CB_USER);
  768. }
  769. }
  770. for (plane_track = tracking->plane_tracks.first; plane_track; plane_track = plane_track->next) {
  771. CALLBACK_INVOKE(plane_track->image, IDWALK_CB_USER);
  772. }
  773. break;
  774. }
  775. case ID_MSK:
  776. {
  777. Mask *mask = (Mask *) id;
  778. MaskLayer *mask_layer;
  779. for (mask_layer = mask->masklayers.first; mask_layer; mask_layer = mask_layer->next) {
  780. MaskSpline *mask_spline;
  781. for (mask_spline = mask_layer->splines.first; mask_spline; mask_spline = mask_spline->next) {
  782. for (i = 0; i < mask_spline->tot_point; i++) {
  783. MaskSplinePoint *point = &mask_spline->points[i];
  784. CALLBACK_INVOKE_ID(point->parent.id, IDWALK_CB_USER);
  785. }
  786. }
  787. }
  788. break;
  789. }
  790. case ID_LS:
  791. {
  792. FreestyleLineStyle *linestyle = (FreestyleLineStyle *) id;
  793. LineStyleModifier *lsm;
  794. for (i = 0; i < MAX_MTEX; i++) {
  795. if (linestyle->mtex[i]) {
  796. library_foreach_mtex(&data, linestyle->mtex[i]);
  797. }
  798. }
  799. if (linestyle->nodetree) {
  800. /* nodetree **are owned by IDs**, treat them as mere sub-data and not real ID! */
  801. library_foreach_ID_as_subdata_link((ID **)&linestyle->nodetree, callback, user_data, flag, &data);
  802. }
  803. for (lsm = linestyle->color_modifiers.first; lsm; lsm = lsm->next) {
  804. if (lsm->type == LS_MODIFIER_DISTANCE_FROM_OBJECT) {
  805. LineStyleColorModifier_DistanceFromObject *p = (LineStyleColorModifier_DistanceFromObject *)lsm;
  806. if (p->target) {
  807. CALLBACK_INVOKE(p->target, IDWALK_CB_NOP);
  808. }
  809. }
  810. }
  811. for (lsm = linestyle->alpha_modifiers.first; lsm; lsm = lsm->next) {
  812. if (lsm->type == LS_MODIFIER_DISTANCE_FROM_OBJECT) {
  813. LineStyleAlphaModifier_DistanceFromObject *p = (LineStyleAlphaModifier_DistanceFromObject *)lsm;
  814. if (p->target) {
  815. CALLBACK_INVOKE(p->target, IDWALK_CB_NOP);
  816. }
  817. }
  818. }
  819. for (lsm = linestyle->thickness_modifiers.first; lsm; lsm = lsm->next) {
  820. if (lsm->type == LS_MODIFIER_DISTANCE_FROM_OBJECT) {
  821. LineStyleThicknessModifier_DistanceFromObject *p = (LineStyleThicknessModifier_DistanceFromObject *)lsm;
  822. if (p->target) {
  823. CALLBACK_INVOKE(p->target, IDWALK_CB_NOP);
  824. }
  825. }
  826. }
  827. break;
  828. }
  829. case ID_AC:
  830. {
  831. bAction *act = (bAction *) id;
  832. for (TimeMarker *marker = act->markers.first; marker; marker = marker->next) {
  833. CALLBACK_INVOKE(marker->camera, IDWALK_CB_NOP);
  834. }
  835. break;
  836. }
  837. case ID_GD:
  838. {
  839. bGPdata *gpencil = (bGPdata *) id;
  840. for (bGPDlayer *gp_layer = gpencil->layers.first; gp_layer; gp_layer = gp_layer->next) {
  841. CALLBACK_INVOKE(gp_layer->parent, IDWALK_CB_NOP);
  842. }
  843. break;
  844. }
  845. /* Nothing needed for those... */
  846. case ID_IM:
  847. case ID_VF:
  848. case ID_TXT:
  849. case ID_SO:
  850. case ID_WM:
  851. case ID_PAL:
  852. case ID_PC:
  853. case ID_CF:
  854. break;
  855. /* Deprecated. */
  856. case ID_IP:
  857. break;
  858. }
  859. }
  860. FOREACH_FINALIZE:
  861. if (data.ids_handled) {
  862. BLI_gset_free(data.ids_handled, NULL);
  863. BLI_LINKSTACK_FREE(data.ids_todo);
  864. }
  865. #undef CALLBACK_INVOKE_ID
  866. #undef CALLBACK_INVOKE
  867. }
  868. #undef FOREACH_CALLBACK_INVOKE_ID
  869. #undef FOREACH_CALLBACK_INVOKE
  870. /**
  871. * re-usable function, use when replacing ID's
  872. */
  873. void BKE_library_update_ID_link_user(ID *id_dst, ID *id_src, const int cb_flag)
  874. {
  875. if (cb_flag & IDWALK_CB_USER) {
  876. id_us_min(id_src);
  877. id_us_plus(id_dst);
  878. }
  879. else if (cb_flag & IDWALK_CB_USER_ONE) {
  880. id_us_ensure_real(id_dst);
  881. }
  882. }
  883. /**
  884. * Say whether given \a id_type_owner can use (in any way) a datablock of \a id_type_used.
  885. *
  886. * This is a 'simplified' abstract version of #BKE_library_foreach_ID_link() above, quite useful to reduce
  887. * useless iterations in some cases.
  888. */
  889. /* XXX This has to be fully rethink, basing check on ID type is not really working anymore (and even worth once
  890. * IDProps will support ID pointers), we'll have to do some quick checks on IDs themselves... */
  891. bool BKE_library_id_can_use_idtype(ID *id_owner, const short id_type_used)
  892. {
  893. /* any type of ID can be used in custom props. */
  894. if (id_owner->properties) {
  895. return true;
  896. }
  897. const short id_type_owner = GS(id_owner->name);
  898. /* IDProps of armature bones and nodes, and bNode->id can use virtually any type of ID. */
  899. if (ELEM(id_type_owner, ID_NT, ID_AR)) {
  900. return true;
  901. }
  902. if (ntreeFromID(id_owner)) {
  903. return true;
  904. }
  905. if (BKE_animdata_from_id(id_owner)) {
  906. return true; /* AnimationData can use virtually any kind of datablocks, through drivers especially. */
  907. }
  908. switch ((ID_Type)id_type_owner) {
  909. case ID_LI:
  910. return ELEM(id_type_used, ID_LI);
  911. case ID_SCE:
  912. return (ELEM(id_type_used, ID_OB, ID_WO, ID_SCE, ID_MC, ID_MA, ID_GR, ID_TXT,
  913. ID_LS, ID_MSK, ID_SO, ID_GD, ID_BR, ID_PAL, ID_IM, ID_NT));
  914. case ID_OB:
  915. /* Could be the following, but simpler to just always say 'yes' here. */
  916. #if 0
  917. return ELEM(id_type_used, ID_ME, ID_CU, ID_MB, ID_LT, ID_SPK, ID_AR, ID_LA, ID_CA, /* obdata */
  918. ID_OB, ID_MA, ID_GD, ID_GR, ID_TE, ID_PA, ID_TXT, ID_SO, ID_MC, ID_IM, ID_AC
  919. /* + constraints, modifiers and game logic ID types... */);
  920. #else
  921. return true;
  922. #endif
  923. case ID_ME:
  924. return ELEM(id_type_used, ID_ME, ID_KE, ID_MA, ID_IM);
  925. case ID_CU:
  926. return ELEM(id_type_used, ID_OB, ID_KE, ID_MA, ID_VF);
  927. case ID_MB:
  928. return ELEM(id_type_used, ID_MA);
  929. case ID_MA:
  930. return (ELEM(id_type_used, ID_TE, ID_GR));
  931. case ID_TE:
  932. return (ELEM(id_type_used, ID_IM, ID_OB));
  933. case ID_LT:
  934. return ELEM(id_type_used, ID_KE);
  935. case ID_LA:
  936. return (ELEM(id_type_used, ID_TE));
  937. case ID_CA:
  938. return ELEM(id_type_used, ID_OB);
  939. case ID_KE:
  940. return ELEM(id_type_used, ID_ME, ID_CU, ID_LT); /* Warning! key->from, could be more types in future? */
  941. case ID_SCR:
  942. return ELEM(id_type_used, ID_SCE);
  943. case ID_WO:
  944. return (ELEM(id_type_used, ID_TE));
  945. case ID_SPK:
  946. return ELEM(id_type_used, ID_SO);
  947. case ID_GR:
  948. return ELEM(id_type_used, ID_OB);
  949. case ID_NT:
  950. /* Could be the following, but node.id has no type restriction... */
  951. #if 0
  952. return ELEM(id_type_used, ID_GD /* + node.id types... */);
  953. #else
  954. return true;
  955. #endif
  956. case ID_BR:
  957. return ELEM(id_type_used, ID_BR, ID_IM, ID_PC, ID_TE);
  958. case ID_PA:
  959. return ELEM(id_type_used, ID_OB, ID_GR, ID_TE);
  960. case ID_MC:
  961. return ELEM(id_type_used, ID_GD, ID_IM);
  962. case ID_MSK:
  963. return ELEM(id_type_used, ID_MC); /* WARNING! mask->parent.id, not typed. */
  964. case ID_LS:
  965. return (ELEM(id_type_used, ID_TE, ID_OB));
  966. case ID_IM:
  967. case ID_VF:
  968. case ID_TXT:
  969. case ID_SO:
  970. case ID_AR:
  971. case ID_AC:
  972. case ID_GD:
  973. case ID_WM:
  974. case ID_PAL:
  975. case ID_PC:
  976. case ID_CF:
  977. /* Those types never use/reference other IDs... */
  978. return false;
  979. case ID_IP:
  980. /* Deprecated... */
  981. return false;
  982. }
  983. return false;
  984. }
  985. /* ***** ID users iterator. ***** */
  986. typedef struct IDUsersIter {
  987. ID *id;
  988. ListBase *lb_array[MAX_LIBARRAY];
  989. int lb_idx;
  990. ID *curr_id;
  991. int count_direct, count_indirect; /* Set by callback. */
  992. } IDUsersIter;
  993. static int foreach_libblock_id_users_callback(void *user_data, ID *UNUSED(self_id), ID **id_p, int cb_flag)
  994. {
  995. IDUsersIter *iter = user_data;
  996. if (*id_p) {
  997. /* 'Loopback' ID pointers (the ugly 'from' ones, Object->proxy_from and Key->from).
  998. * Those are not actually ID usage, we can ignore them here.
  999. */
  1000. if (cb_flag & IDWALK_CB_LOOPBACK) {
  1001. return IDWALK_RET_NOP;
  1002. }
  1003. if (*id_p == iter->id) {
  1004. #if 0
  1005. printf("%s uses %s (refcounted: %d, userone: %d, used_one: %d, used_one_active: %d, indirect_usage: %d)\n",
  1006. iter->curr_id->name, iter->id->name, (cb_flag & IDWALK_USER) ? 1 : 0, (cb_flag & IDWALK_USER_ONE) ? 1 : 0,
  1007. (iter->id->tag & LIB_TAG_EXTRAUSER) ? 1 : 0, (iter->id->tag & LIB_TAG_EXTRAUSER_SET) ? 1 : 0,
  1008. (cb_flag & IDWALK_INDIRECT_USAGE) ? 1 : 0);
  1009. #endif
  1010. if (cb_flag & IDWALK_CB_INDIRECT_USAGE) {
  1011. iter->count_indirect++;
  1012. }
  1013. else {
  1014. iter->count_direct++;
  1015. }
  1016. }
  1017. }
  1018. return IDWALK_RET_NOP;
  1019. }
  1020. /**
  1021. * Return the number of times given \a id_user uses/references \a id_used.
  1022. *
  1023. * \note This only checks for pointer references of an ID, shallow usages (like e.g. by RNA paths, as done
  1024. * for FCurves) are not detected at all.
  1025. *
  1026. * \param id_user the ID which is supposed to use (reference) \a id_used.
  1027. * \param id_used the ID which is supposed to be used (referenced) by \a id_user.
  1028. * \return the number of direct usages/references of \a id_used by \a id_user.
  1029. */
  1030. int BKE_library_ID_use_ID(ID *id_user, ID *id_used)
  1031. {
  1032. IDUsersIter iter;
  1033. /* We do not care about iter.lb_array/lb_idx here... */
  1034. iter.id = id_used;
  1035. iter.curr_id = id_user;
  1036. iter.count_direct = iter.count_indirect = 0;
  1037. BKE_library_foreach_ID_link(NULL, iter.curr_id, foreach_libblock_id_users_callback, (void *)&iter, IDWALK_READONLY);
  1038. return iter.count_direct + iter.count_indirect;
  1039. }
  1040. static bool library_ID_is_used(Main *bmain, void *idv, const bool check_linked)
  1041. {
  1042. IDUsersIter iter;
  1043. ListBase *lb_array[MAX_LIBARRAY];
  1044. ID *id = idv;
  1045. int i = set_listbasepointers(bmain, lb_array);
  1046. bool is_defined = false;
  1047. iter.id = id;
  1048. iter.count_direct = iter.count_indirect = 0;
  1049. while (i-- && !is_defined) {
  1050. ID *id_curr = lb_array[i]->first;
  1051. if (!id_curr || !BKE_library_id_can_use_idtype(id_curr, GS(id->name))) {
  1052. continue;
  1053. }
  1054. for (; id_curr && !is_defined; id_curr = id_curr->next) {
  1055. if (id_curr == id) {
  1056. /* We are not interested in self-usages (mostly from drivers or bone constraints...). */
  1057. continue;
  1058. }
  1059. iter.curr_id = id_curr;
  1060. BKE_library_foreach_ID_link(
  1061. bmain, id_curr, foreach_libblock_id_users_callback, &iter, IDWALK_READONLY);
  1062. is_defined = ((check_linked ? iter.count_indirect : iter.count_direct) != 0);
  1063. }
  1064. }
  1065. return is_defined;
  1066. }
  1067. /**
  1068. * Check whether given ID is used locally (i.e. by another non-linked ID).
  1069. */
  1070. bool BKE_library_ID_is_locally_used(Main *bmain, void *idv)
  1071. {
  1072. return library_ID_is_used(bmain, idv, false);
  1073. }
  1074. /**
  1075. * Check whether given ID is used indirectly (i.e. by another linked ID).
  1076. */
  1077. bool BKE_library_ID_is_indirectly_used(Main *bmain, void *idv)
  1078. {
  1079. return library_ID_is_used(bmain, idv, true);
  1080. }
  1081. /**
  1082. * Combine \a BKE_library_ID_is_locally_used() and \a BKE_library_ID_is_indirectly_used() in a single call.
  1083. */
  1084. void BKE_library_ID_test_usages(Main *bmain, void *idv, bool *is_used_local, bool *is_used_linked)
  1085. {
  1086. IDUsersIter iter;
  1087. ListBase *lb_array[MAX_LIBARRAY];
  1088. ID *id = idv;
  1089. int i = set_listbasepointers(bmain, lb_array);
  1090. bool is_defined = false;
  1091. iter.id = id;
  1092. iter.count_direct = iter.count_indirect = 0;
  1093. while (i-- && !is_defined) {
  1094. ID *id_curr = lb_array[i]->first;
  1095. if (!id_curr || !BKE_library_id_can_use_idtype(id_curr, GS(id->name))) {
  1096. continue;
  1097. }
  1098. for (; id_curr && !is_defined; id_curr = id_curr->next) {
  1099. if (id_curr == id) {
  1100. /* We are not interested in self-usages (mostly from drivers or bone constraints...). */
  1101. continue;
  1102. }
  1103. iter.curr_id = id_curr;
  1104. BKE_library_foreach_ID_link(bmain, id_curr, foreach_libblock_id_users_callback, &iter, IDWALK_READONLY);
  1105. is_defined = (iter.count_direct != 0 && iter.count_indirect != 0);
  1106. }
  1107. }
  1108. *is_used_local = (iter.count_direct != 0);
  1109. *is_used_linked = (iter.count_indirect != 0);
  1110. }
  1111. /* ***** IDs usages.checking/tagging. ***** */
  1112. static int foreach_libblock_used_linked_data_tag_clear_cb(
  1113. void *user_data, ID *self_id, ID **id_p, int UNUSED(cb_flag))
  1114. {
  1115. bool *is_changed = user_data;
  1116. if (*id_p) {
  1117. /* XXX This is actually some kind of hack...
  1118. * Issue is, shapekeys' 'from' ID pointer is not actually ID usage.
  1119. * Maybe we should even nuke it from BKE_library_foreach_ID_link, not 100% sure yet...
  1120. */
  1121. if ((GS(self_id->name) == ID_KE) && (((Key *)self_id)->from == *id_p)) {
  1122. return IDWALK_RET_NOP;
  1123. }
  1124. /* XXX another hack, for similar reasons as above one. */
  1125. if ((GS(self_id->name) == ID_OB) && (((Object *)self_id)->proxy_from == (Object *)*id_p)) {
  1126. return IDWALK_RET_NOP;
  1127. }
  1128. /* If checked id is used by an assumed used ID, then it is also used and not part of any linked archipelago. */
  1129. if (!(self_id->tag & LIB_TAG_DOIT) && ((*id_p)->tag & LIB_TAG_DOIT)) {
  1130. (*id_p)->tag &= ~LIB_TAG_DOIT;
  1131. *is_changed = true;
  1132. }
  1133. }
  1134. return IDWALK_RET_NOP;
  1135. }
  1136. /**
  1137. * Detect orphaned linked data blocks (i.e. linked data not used (directly or indirectly) in any way by any local data),
  1138. * including complex cases like 'linked archipelagoes', i.e. linked datablocks that use each other in loops,
  1139. * which prevents their deletion by 'basic' usage checks...
  1140. *
  1141. * \param do_init_tag if \a true, all linked data are checked, if \a false, only linked datablocks already tagged with
  1142. * LIB_TAG_DOIT are checked.
  1143. */
  1144. void BKE_library_unused_linked_data_set_tag(Main *bmain, const bool do_init_tag)
  1145. {
  1146. ListBase *lb_array[MAX_LIBARRAY];
  1147. if (do_init_tag) {
  1148. int i = set_listbasepointers(bmain, lb_array);
  1149. while (i--) {
  1150. for (ID *id = lb_array[i]->first; id; id = id->next) {
  1151. if (id->lib && (id->tag & LIB_TAG_INDIRECT) != 0) {
  1152. id->tag |= LIB_TAG_DOIT;
  1153. }
  1154. else {
  1155. id->tag &= ~LIB_TAG_DOIT;
  1156. }
  1157. }
  1158. }
  1159. }
  1160. bool do_loop = true;
  1161. while (do_loop) {
  1162. int i = set_listbasepointers(bmain, lb_array);
  1163. do_loop = false;
  1164. while (i--) {
  1165. for (ID *id = lb_array[i]->first; id; id = id->next) {
  1166. if (id->tag & LIB_TAG_DOIT) {
  1167. /* Unused ID (so far), no need to check it further. */
  1168. continue;
  1169. }
  1170. BKE_library_foreach_ID_link(
  1171. bmain, id, foreach_libblock_used_linked_data_tag_clear_cb, &do_loop, IDWALK_READONLY);
  1172. }
  1173. }
  1174. }
  1175. }
  1176. /**
  1177. * Untag linked data blocks used by other untagged linked datablocks.
  1178. * Used to detect datablocks that we can forcefully make local (instead of copying them to later get rid of original):
  1179. * All datablocks we want to make local are tagged by caller, after this function has ran caller knows datablocks still
  1180. * tagged can directly be made local, since they are only used by other datablocks that will also be made fully local.
  1181. */
  1182. void BKE_library_indirectly_used_data_tag_clear(Main *bmain)
  1183. {
  1184. ListBase *lb_array[MAX_LIBARRAY];
  1185. bool do_loop = true;
  1186. while (do_loop) {
  1187. int i = set_listbasepointers(bmain, lb_array);
  1188. do_loop = false;
  1189. while (i--) {
  1190. for (ID *id = lb_array[i]->first; id; id = id->next) {
  1191. if (id->lib == NULL || id->tag & LIB_TAG_DOIT) {
  1192. /* Local or non-indirectly-used ID (so far), no need to check it further. */
  1193. continue;
  1194. }
  1195. BKE_library_foreach_ID_link(
  1196. bmain, id, foreach_libblock_used_linked_data_tag_clear_cb, &do_loop, IDWALK_READONLY);
  1197. }
  1198. }
  1199. }
  1200. }