library_remap.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985
  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. * ***** END GPL LICENSE BLOCK *****
  19. */
  20. /** \file blender/blenkernel/intern/library_remap.c
  21. * \ingroup bke
  22. *
  23. * Contains management of ID's and libraries remap, unlink and free logic.
  24. */
  25. #include <stdio.h>
  26. #include <ctype.h>
  27. #include <string.h>
  28. #include <stdlib.h>
  29. #include <stddef.h>
  30. #include <assert.h>
  31. #include "MEM_guardedalloc.h"
  32. /* all types are needed here, in order to do memory operations */
  33. #include "DNA_anim_types.h"
  34. #include "DNA_armature_types.h"
  35. #include "DNA_brush_types.h"
  36. #include "DNA_camera_types.h"
  37. #include "DNA_cachefile_types.h"
  38. #include "DNA_group_types.h"
  39. #include "DNA_gpencil_types.h"
  40. #include "DNA_ipo_types.h"
  41. #include "DNA_key_types.h"
  42. #include "DNA_lamp_types.h"
  43. #include "DNA_lattice_types.h"
  44. #include "DNA_linestyle_types.h"
  45. #include "DNA_material_types.h"
  46. #include "DNA_mesh_types.h"
  47. #include "DNA_meta_types.h"
  48. #include "DNA_movieclip_types.h"
  49. #include "DNA_mask_types.h"
  50. #include "DNA_node_types.h"
  51. #include "DNA_object_types.h"
  52. #include "DNA_scene_types.h"
  53. #include "DNA_screen_types.h"
  54. #include "DNA_speaker_types.h"
  55. #include "DNA_sound_types.h"
  56. #include "DNA_text_types.h"
  57. #include "DNA_vfont_types.h"
  58. #include "DNA_windowmanager_types.h"
  59. #include "DNA_world_types.h"
  60. #include "BLI_blenlib.h"
  61. #include "BLI_utildefines.h"
  62. #include "BKE_action.h"
  63. #include "BKE_animsys.h"
  64. #include "BKE_armature.h"
  65. #include "BKE_brush.h"
  66. #include "BKE_camera.h"
  67. #include "BKE_cachefile.h"
  68. #include "BKE_curve.h"
  69. #include "BKE_depsgraph.h"
  70. #include "BKE_fcurve.h"
  71. #include "BKE_font.h"
  72. #include "BKE_group.h"
  73. #include "BKE_gpencil.h"
  74. #include "BKE_idprop.h"
  75. #include "BKE_image.h"
  76. #include "BKE_ipo.h"
  77. #include "BKE_key.h"
  78. #include "BKE_lamp.h"
  79. #include "BKE_lattice.h"
  80. #include "BKE_library.h"
  81. #include "BKE_library_query.h"
  82. #include "BKE_library_remap.h"
  83. #include "BKE_linestyle.h"
  84. #include "BKE_mesh.h"
  85. #include "BKE_material.h"
  86. #include "BKE_main.h"
  87. #include "BKE_mask.h"
  88. #include "BKE_mball.h"
  89. #include "BKE_modifier.h"
  90. #include "BKE_movieclip.h"
  91. #include "BKE_multires.h"
  92. #include "BKE_node.h"
  93. #include "BKE_object.h"
  94. #include "BKE_paint.h"
  95. #include "BKE_particle.h"
  96. #include "BKE_sca.h"
  97. #include "BKE_speaker.h"
  98. #include "BKE_sound.h"
  99. #include "BKE_screen.h"
  100. #include "BKE_scene.h"
  101. #include "BKE_text.h"
  102. #include "BKE_texture.h"
  103. #include "BKE_world.h"
  104. #ifdef WITH_PYTHON
  105. #include "BPY_extern.h"
  106. #endif
  107. static BKE_library_free_window_manager_cb free_windowmanager_cb = NULL;
  108. void BKE_library_callback_free_window_manager_set(BKE_library_free_window_manager_cb func)
  109. {
  110. free_windowmanager_cb = func;
  111. }
  112. static BKE_library_free_notifier_reference_cb free_notifier_reference_cb = NULL;
  113. void BKE_library_callback_free_notifier_reference_set(BKE_library_free_notifier_reference_cb func)
  114. {
  115. free_notifier_reference_cb = func;
  116. }
  117. static BKE_library_remap_editor_id_reference_cb remap_editor_id_reference_cb = NULL;
  118. void BKE_library_callback_remap_editor_id_reference_set(BKE_library_remap_editor_id_reference_cb func)
  119. {
  120. remap_editor_id_reference_cb = func;
  121. }
  122. typedef struct IDRemap {
  123. Main *bmain; /* Only used to trigger depsgraph updates in the right bmain. */
  124. ID *old_id;
  125. ID *new_id;
  126. ID *id; /* The ID in which we are replacing old_id by new_id usages. */
  127. short flag;
  128. /* 'Output' data. */
  129. short status;
  130. int skipped_direct; /* Number of direct usecases that could not be remapped (e.g.: obdata when in edit mode). */
  131. int skipped_indirect; /* Number of indirect usecases that could not be remapped. */
  132. int skipped_refcounted; /* Number of skipped usecases that refcount the datablock. */
  133. } IDRemap;
  134. /* IDRemap->flag enums defined in BKE_library.h */
  135. /* IDRemap->status */
  136. enum {
  137. /* *** Set by callback. *** */
  138. ID_REMAP_IS_LINKED_DIRECT = 1 << 0, /* new_id is directly linked in current .blend. */
  139. ID_REMAP_IS_USER_ONE_SKIPPED = 1 << 1, /* There was some skipped 'user_one' usages of old_id. */
  140. };
  141. static int foreach_libblock_remap_callback(void *user_data, ID *id_self, ID **id_p, int cb_flag)
  142. {
  143. if (cb_flag & IDWALK_CB_PRIVATE) {
  144. return IDWALK_RET_NOP;
  145. }
  146. IDRemap *id_remap_data = user_data;
  147. ID *old_id = id_remap_data->old_id;
  148. ID *new_id = id_remap_data->new_id;
  149. ID *id = id_remap_data->id;
  150. if (!old_id) { /* Used to cleanup all IDs used by a specific one. */
  151. BLI_assert(!new_id);
  152. old_id = *id_p;
  153. }
  154. if (*id_p && (*id_p == old_id)) {
  155. const bool is_indirect = (cb_flag & IDWALK_CB_INDIRECT_USAGE) != 0;
  156. const bool skip_indirect = (id_remap_data->flag & ID_REMAP_SKIP_INDIRECT_USAGE) != 0;
  157. /* Note: proxy usage implies LIB_TAG_EXTERN, so on this aspect it is direct,
  158. * on the other hand since they get reset to lib data on file open/reload it is indirect too...
  159. * Edit Mode is also a 'skip direct' case. */
  160. const bool is_obj = (GS(id->name) == ID_OB);
  161. const bool is_obj_proxy = (is_obj && (((Object *)id)->proxy || ((Object *)id)->proxy_group));
  162. const bool is_obj_editmode = (is_obj && BKE_object_is_in_editmode((Object *)id));
  163. const bool is_never_null = ((cb_flag & IDWALK_CB_NEVER_NULL) && (new_id == NULL) &&
  164. (id_remap_data->flag & ID_REMAP_FORCE_NEVER_NULL_USAGE) == 0);
  165. const bool skip_never_null = (id_remap_data->flag & ID_REMAP_SKIP_NEVER_NULL_USAGE) != 0;
  166. #ifdef DEBUG_PRINT
  167. printf("In %s: Remapping %s (%p) to %s (%p) (is_indirect: %d, skip_indirect: %d)\n",
  168. id->name, old_id->name, old_id, new_id ? new_id->name : "<NONE>", new_id, is_indirect, skip_indirect);
  169. #endif
  170. if ((id_remap_data->flag & ID_REMAP_FLAG_NEVER_NULL_USAGE) && (cb_flag & IDWALK_CB_NEVER_NULL)) {
  171. id->tag |= LIB_TAG_DOIT;
  172. }
  173. /* Special hack in case it's Object->data and we are in edit mode (skipped_direct too). */
  174. if ((is_never_null && skip_never_null) ||
  175. (is_obj_editmode && (((Object *)id)->data == *id_p)) ||
  176. (skip_indirect && is_indirect))
  177. {
  178. if (is_indirect) {
  179. id_remap_data->skipped_indirect++;
  180. if (is_obj) {
  181. Object *ob = (Object *)id;
  182. if (ob->data == *id_p && ob->proxy != NULL) {
  183. /* And another 'Proudly brought to you by Proxy Hell' hack!
  184. * This will allow us to avoid clearing 'LIB_EXTERN' flag of obdata of proxies... */
  185. id_remap_data->skipped_direct++;
  186. }
  187. }
  188. }
  189. else if (is_never_null || is_obj_editmode) {
  190. id_remap_data->skipped_direct++;
  191. }
  192. else {
  193. BLI_assert(0);
  194. }
  195. if (cb_flag & IDWALK_CB_USER) {
  196. id_remap_data->skipped_refcounted++;
  197. }
  198. else if (cb_flag & IDWALK_CB_USER_ONE) {
  199. /* No need to count number of times this happens, just a flag is enough. */
  200. id_remap_data->status |= ID_REMAP_IS_USER_ONE_SKIPPED;
  201. }
  202. }
  203. else {
  204. if (!is_never_null) {
  205. *id_p = new_id;
  206. DAG_id_tag_update_ex(id_remap_data->bmain, id_self, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
  207. }
  208. if (cb_flag & IDWALK_CB_USER) {
  209. id_us_min(old_id);
  210. /* We do not want to handle LIB_TAG_INDIRECT/LIB_TAG_EXTERN here. */
  211. if (new_id)
  212. new_id->us++;
  213. }
  214. else if (cb_flag & IDWALK_CB_USER_ONE) {
  215. id_us_ensure_real(new_id);
  216. /* We cannot affect old_id->us directly, LIB_TAG_EXTRAUSER(_SET) are assumed to be set as needed,
  217. * that extra user is processed in final handling... */
  218. }
  219. if (!is_indirect || is_obj_proxy) {
  220. id_remap_data->status |= ID_REMAP_IS_LINKED_DIRECT;
  221. }
  222. }
  223. }
  224. return IDWALK_RET_NOP;
  225. }
  226. /* Some remapping unfortunately require extra and/or specific handling, tackle those here. */
  227. static void libblock_remap_data_preprocess_scene_base_unlink(
  228. IDRemap *r_id_remap_data, Scene *sce, Base *base, const bool skip_indirect, const bool is_indirect)
  229. {
  230. if (skip_indirect && is_indirect) {
  231. r_id_remap_data->skipped_indirect++;
  232. r_id_remap_data->skipped_refcounted++;
  233. }
  234. else {
  235. id_us_min((ID *)base->object);
  236. BKE_scene_base_unlink(sce, base);
  237. MEM_freeN(base);
  238. if (!is_indirect) {
  239. r_id_remap_data->status |= ID_REMAP_IS_LINKED_DIRECT;
  240. }
  241. }
  242. }
  243. static void libblock_remap_data_preprocess(IDRemap *r_id_remap_data)
  244. {
  245. switch (GS(r_id_remap_data->id->name)) {
  246. case ID_SCE:
  247. {
  248. Scene *sce = (Scene *)r_id_remap_data->id;
  249. if (!r_id_remap_data->new_id) {
  250. const bool is_indirect = (sce->id.lib != NULL);
  251. const bool skip_indirect = (r_id_remap_data->flag & ID_REMAP_SKIP_INDIRECT_USAGE) != 0;
  252. /* In case we are unlinking... */
  253. if (!r_id_remap_data->old_id) {
  254. /* ... everything from scene. */
  255. Base *base, *base_next;
  256. for (base = sce->base.first; base; base = base_next) {
  257. base_next = base->next;
  258. libblock_remap_data_preprocess_scene_base_unlink(
  259. r_id_remap_data, sce, base, skip_indirect, is_indirect);
  260. }
  261. }
  262. else if (GS(r_id_remap_data->old_id->name) == ID_OB) {
  263. /* ... a specific object from scene. */
  264. Object *old_ob = (Object *)r_id_remap_data->old_id;
  265. Base *base = BKE_scene_base_find(sce, old_ob);
  266. if (base) {
  267. libblock_remap_data_preprocess_scene_base_unlink(
  268. r_id_remap_data, sce, base, skip_indirect, is_indirect);
  269. }
  270. }
  271. }
  272. break;
  273. }
  274. case ID_OB:
  275. {
  276. ID *old_id = r_id_remap_data->old_id;
  277. if (!old_id || GS(old_id->name) == ID_AR) {
  278. Object *ob = (Object *)r_id_remap_data->id;
  279. /* Object's pose holds reference to armature bones... sic */
  280. /* Note that in theory, we should have to bother about linked/non-linked/never-null/etc. flags/states.
  281. * Fortunately, this is just a tag, so we can accept to 'over-tag' a bit for pose recalc, and avoid
  282. * another complex and risky condition nightmare like the one we have in
  283. * foreach_libblock_remap_callback()... */
  284. if (ob->pose && (!old_id || ob->data == old_id)) {
  285. BLI_assert(ob->type == OB_ARMATURE);
  286. ob->pose->flag |= POSE_RECALC;
  287. /* We need to clear pose bone pointers immediately, things like undo writefile may be called
  288. * before pose is actually recomputed, can lead to segfault... */
  289. BKE_pose_clear_pointers(ob->pose);
  290. }
  291. }
  292. break;
  293. }
  294. default:
  295. break;
  296. }
  297. }
  298. static void libblock_remap_data_postprocess_object_update(Main *bmain, Object *old_ob, Object *new_ob)
  299. {
  300. if (old_ob->flag & OB_FROMGROUP) {
  301. /* Note that for Scene's BaseObject->flag, either we:
  302. * - unlinked old_ob (i.e. new_ob is NULL), in which case scenes' bases have been removed already.
  303. * - remapped old_ob by new_ob, in which case scenes' bases are still valid as is.
  304. * So in any case, no need to update them here. */
  305. if (BKE_group_object_find(NULL, old_ob) == NULL) {
  306. old_ob->flag &= ~OB_FROMGROUP;
  307. }
  308. if (new_ob == NULL) { /* We need to remove NULL-ified groupobjects... */
  309. for (Group *group = bmain->group.first; group; group = group->id.next) {
  310. BKE_group_object_unlink(group, NULL, NULL, NULL);
  311. }
  312. }
  313. else {
  314. new_ob->flag |= OB_FROMGROUP;
  315. }
  316. }
  317. if (old_ob->type == OB_MBALL) {
  318. for (Object *ob = bmain->object.first; ob; ob = ob->id.next) {
  319. if (ob->type == OB_MBALL && BKE_mball_is_basis_for(ob, old_ob)) {
  320. DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
  321. }
  322. }
  323. }
  324. }
  325. static void libblock_remap_data_postprocess_group_scene_unlink(Main *UNUSED(bmain), Scene *sce, ID *old_id)
  326. {
  327. /* Note that here we assume no object has no base (i.e. all objects are assumed instanced
  328. * in one scene...). */
  329. for (Base *base = sce->base.first; base; base = base->next) {
  330. if (base->flag & OB_FROMGROUP) {
  331. Object *ob = base->object;
  332. if (ob->flag & OB_FROMGROUP) {
  333. Group *grp = BKE_group_object_find(NULL, ob);
  334. /* Unlinked group (old_id) is still in bmain... */
  335. if (grp && (&grp->id == old_id || grp->id.us == 0)) {
  336. grp = BKE_group_object_find(grp, ob);
  337. }
  338. if (!grp) {
  339. ob->flag &= ~OB_FROMGROUP;
  340. }
  341. }
  342. if (!(ob->flag & OB_FROMGROUP)) {
  343. base->flag &= ~OB_FROMGROUP;
  344. }
  345. }
  346. }
  347. }
  348. static void libblock_remap_data_postprocess_obdata_relink(Main *UNUSED(bmain), Object *ob, ID *new_id)
  349. {
  350. if (ob->data == new_id) {
  351. switch (GS(new_id->name)) {
  352. case ID_ME:
  353. multires_force_update(ob);
  354. break;
  355. case ID_CU:
  356. BKE_curve_type_test(ob);
  357. break;
  358. default:
  359. break;
  360. }
  361. test_object_modifiers(ob);
  362. test_object_materials(ob, new_id);
  363. }
  364. }
  365. static void libblock_remap_data_postprocess_nodetree_update(Main *bmain, ID *new_id)
  366. {
  367. /* Verify all nodetree user nodes. */
  368. ntreeVerifyNodes(bmain, new_id);
  369. /* Update node trees as necessary. */
  370. FOREACH_NODETREE(bmain, ntree, id) {
  371. /* make an update call for the tree */
  372. ntreeUpdateTree(bmain, ntree);
  373. } FOREACH_NODETREE_END
  374. }
  375. /**
  376. * Execute the 'data' part of the remapping (that is, all ID pointers from other ID datablocks).
  377. *
  378. * Behavior differs depending on whether given \a id is NULL or not:
  379. * - \a id NULL: \a old_id must be non-NULL, \a new_id may be NULL (unlinking \a old_id) or not
  380. * (remapping \a old_id to \a new_id). The whole \a bmain database is checked, and all pointers to \a old_id
  381. * are remapped to \a new_id.
  382. * - \a id is non-NULL:
  383. * + If \a old_id is NULL, \a new_id must also be NULL, and all ID pointers from \a id are cleared (i.e. \a id
  384. * does not references any other datablock anymore).
  385. * + If \a old_id is non-NULL, behavior is as with a NULL \a id, but only within given \a id.
  386. *
  387. * \param bmain: the Main data storage to operate on (must never be NULL).
  388. * \param id: the datablock to operate on (can be NULL, in which case we operate over all IDs from given bmain).
  389. * \param old_id: the datablock to dereference (may be NULL if \a id is non-NULL).
  390. * \param new_id: the new datablock to replace \a old_id references with (may be NULL).
  391. * \param r_id_remap_data: if non-NULL, the IDRemap struct to use (uselful to retrieve info about remapping process).
  392. */
  393. ATTR_NONNULL(1) static void libblock_remap_data(
  394. Main *bmain, ID *id, ID *old_id, ID *new_id, const short remap_flags, IDRemap *r_id_remap_data)
  395. {
  396. IDRemap id_remap_data;
  397. ListBase *lb_array[MAX_LIBARRAY];
  398. int i;
  399. const int foreach_id_flags = (remap_flags & ID_REMAP_NO_INDIRECT_PROXY_DATA_USAGE) != 0 ? IDWALK_NO_INDIRECT_PROXY_DATA_USAGE : IDWALK_NOP;
  400. if (r_id_remap_data == NULL) {
  401. r_id_remap_data = &id_remap_data;
  402. }
  403. r_id_remap_data->bmain = bmain;
  404. r_id_remap_data->old_id = old_id;
  405. r_id_remap_data->new_id = new_id;
  406. r_id_remap_data->id = NULL;
  407. r_id_remap_data->flag = remap_flags;
  408. r_id_remap_data->status = 0;
  409. r_id_remap_data->skipped_direct = 0;
  410. r_id_remap_data->skipped_indirect = 0;
  411. r_id_remap_data->skipped_refcounted = 0;
  412. if (id) {
  413. #ifdef DEBUG_PRINT
  414. printf("\tchecking id %s (%p, %p)\n", id->name, id, id->lib);
  415. #endif
  416. r_id_remap_data->id = id;
  417. libblock_remap_data_preprocess(r_id_remap_data);
  418. BKE_library_foreach_ID_link(NULL, id, foreach_libblock_remap_callback, (void *)r_id_remap_data, foreach_id_flags);
  419. }
  420. else {
  421. i = set_listbasepointers(bmain, lb_array);
  422. /* Note that this is a very 'bruteforce' approach, maybe we could use some depsgraph to only process
  423. * objects actually using given old_id... sounds rather unlikely currently, though, so this will do for now. */
  424. while (i--) {
  425. for (ID *id_curr = lb_array[i]->first; id_curr; id_curr = id_curr->next) {
  426. if (BKE_library_id_can_use_idtype(id_curr, GS(old_id->name))) {
  427. /* Note that we cannot skip indirect usages of old_id here (if requested), we still need to check it for
  428. * the user count handling...
  429. * XXX No more true (except for debug usage of those skipping counters). */
  430. r_id_remap_data->id = id_curr;
  431. libblock_remap_data_preprocess(r_id_remap_data);
  432. BKE_library_foreach_ID_link(
  433. NULL, id_curr, foreach_libblock_remap_callback, (void *)r_id_remap_data, foreach_id_flags);
  434. }
  435. }
  436. }
  437. }
  438. if (old_id && GS(old_id->name) == ID_OB) {
  439. BKE_sca_logic_links_remap(bmain, (Object *)old_id, (Object *)new_id);
  440. }
  441. /* XXX We may not want to always 'transfer' fakeuser from old to new id... Think for now it's desired behavior
  442. * though, we can always add an option (flag) to control this later if needed. */
  443. if (old_id && (old_id->flag & LIB_FAKEUSER)) {
  444. id_fake_user_clear(old_id);
  445. id_fake_user_set(new_id);
  446. }
  447. id_us_clear_real(old_id);
  448. if (new_id && (new_id->tag & LIB_TAG_INDIRECT) && (r_id_remap_data->status & ID_REMAP_IS_LINKED_DIRECT)) {
  449. new_id->tag &= ~LIB_TAG_INDIRECT;
  450. new_id->tag |= LIB_TAG_EXTERN;
  451. }
  452. #ifdef DEBUG_PRINT
  453. printf("%s: %d occurences skipped (%d direct and %d indirect ones)\n", __func__,
  454. r_id_remap_data->skipped_direct + r_id_remap_data->skipped_indirect,
  455. r_id_remap_data->skipped_direct, r_id_remap_data->skipped_indirect);
  456. #endif
  457. }
  458. /**
  459. * Replace all references in given Main to \a old_id by \a new_id
  460. * (if \a new_id is NULL, it unlinks \a old_id).
  461. */
  462. void BKE_libblock_remap_locked(
  463. Main *bmain, void *old_idv, void *new_idv,
  464. const short remap_flags)
  465. {
  466. IDRemap id_remap_data;
  467. ID *old_id = old_idv;
  468. ID *new_id = new_idv;
  469. int skipped_direct, skipped_refcounted;
  470. BLI_assert(old_id != NULL);
  471. BLI_assert((new_id == NULL) || GS(old_id->name) == GS(new_id->name));
  472. BLI_assert(old_id != new_id);
  473. libblock_remap_data(bmain, NULL, old_id, new_id, remap_flags, &id_remap_data);
  474. if (free_notifier_reference_cb) {
  475. free_notifier_reference_cb(old_id);
  476. }
  477. /* We assume editors do not hold references to their IDs... This is false in some cases
  478. * (Image is especially tricky here), editors' code is to handle refcount (id->us) itself then. */
  479. if (remap_editor_id_reference_cb) {
  480. remap_editor_id_reference_cb(old_id, new_id);
  481. }
  482. skipped_direct = id_remap_data.skipped_direct;
  483. skipped_refcounted = id_remap_data.skipped_refcounted;
  484. /* If old_id was used by some ugly 'user_one' stuff (like Image or Clip editors...), and user count has actually
  485. * been incremented for that, we have to decrease once more its user count... unless we had to skip
  486. * some 'user_one' cases. */
  487. if ((old_id->tag & LIB_TAG_EXTRAUSER_SET) && !(id_remap_data.status & ID_REMAP_IS_USER_ONE_SKIPPED)) {
  488. id_us_clear_real(old_id);
  489. }
  490. if (old_id->us - skipped_refcounted < 0) {
  491. printf("Error in remapping process from '%s' (%p) to '%s' (%p): "
  492. "wrong user count in old ID after process (summing up to %d)\n",
  493. old_id->name, old_id, new_id ? new_id->name : "<NULL>", new_id, old_id->us - skipped_refcounted);
  494. BLI_assert(0);
  495. }
  496. if (skipped_direct == 0) {
  497. /* old_id is assumed to not be used directly anymore... */
  498. if (old_id->lib && (old_id->tag & LIB_TAG_EXTERN)) {
  499. old_id->tag &= ~LIB_TAG_EXTERN;
  500. old_id->tag |= LIB_TAG_INDIRECT;
  501. }
  502. }
  503. /* Some after-process updates.
  504. * This is a bit ugly, but cannot see a way to avoid it. Maybe we should do a per-ID callback for this instead?
  505. */
  506. switch (GS(old_id->name)) {
  507. case ID_OB:
  508. libblock_remap_data_postprocess_object_update(bmain, (Object *)old_id, (Object *)new_id);
  509. break;
  510. case ID_GR:
  511. if (!new_id) { /* Only affects us in case group was unlinked. */
  512. for (Scene *sce = bmain->scene.first; sce; sce = sce->id.next) {
  513. libblock_remap_data_postprocess_group_scene_unlink(bmain, sce, old_id);
  514. }
  515. }
  516. break;
  517. case ID_ME:
  518. case ID_CU:
  519. case ID_MB:
  520. if (new_id) { /* Only affects us in case obdata was relinked (changed). */
  521. for (Object *ob = bmain->object.first; ob; ob = ob->id.next) {
  522. libblock_remap_data_postprocess_obdata_relink(bmain, ob, new_id);
  523. }
  524. }
  525. break;
  526. default:
  527. break;
  528. }
  529. /* Node trees may virtually use any kind of data-block... */
  530. /* XXX Yuck!!!! nodetree update can do pretty much any thing when talking about py nodes,
  531. * including creating new data-blocks (see T50385), so we need to unlock main here. :(
  532. * Why can't we have re-entrent locks? */
  533. BKE_main_unlock(bmain);
  534. libblock_remap_data_postprocess_nodetree_update(bmain, new_id);
  535. BKE_main_lock(bmain);
  536. /* Full rebuild of DAG! */
  537. DAG_relations_tag_update(bmain);
  538. }
  539. void BKE_libblock_remap(Main *bmain, void *old_idv, void *new_idv, const short remap_flags)
  540. {
  541. BKE_main_lock(bmain);
  542. BKE_libblock_remap_locked(bmain, old_idv, new_idv, remap_flags);
  543. BKE_main_unlock(bmain);
  544. }
  545. /**
  546. * Unlink given \a id from given \a bmain (does not touch to indirect, i.e. library, usages of the ID).
  547. *
  548. * \param do_flag_never_null: If true, all IDs using \a idv in a 'non-NULL' way are flagged by \a LIB_TAG_DOIT flag
  549. * (quite obviously, 'non-NULL' usages can never be unlinked by this function...).
  550. */
  551. void BKE_libblock_unlink(Main *bmain, void *idv, const bool do_flag_never_null, const bool do_skip_indirect)
  552. {
  553. const short remap_flags = (do_skip_indirect ? ID_REMAP_SKIP_INDIRECT_USAGE : 0) |
  554. (do_flag_never_null ? ID_REMAP_FLAG_NEVER_NULL_USAGE : 0);
  555. BKE_main_lock(bmain);
  556. BKE_libblock_remap_locked(bmain, idv, NULL, remap_flags);
  557. BKE_main_unlock(bmain);
  558. }
  559. /** Similar to libblock_remap, but only affects IDs used by given \a idv ID.
  560. *
  561. * \param old_idv: Unlike BKE_libblock_remap, can be NULL,
  562. * in which case all ID usages by given \a idv will be cleared.
  563. * \param us_min_never_null: If \a true and new_id is NULL,
  564. * 'NEVER_NULL' ID usages keep their old id, but this one still gets its user count decremented
  565. * (needed when given \a idv is going to be deleted right after being unlinked).
  566. */
  567. /* Should be able to replace all _relink() funcs (constraints, rigidbody, etc.) ? */
  568. /* XXX Arg! Naming... :(
  569. * _relink? avoids confusion with _remap, but is confusing with _unlink
  570. * _remap_used_ids?
  571. * _remap_datablocks?
  572. * BKE_id_remap maybe?
  573. * ... sigh
  574. */
  575. void BKE_libblock_relink_ex(
  576. Main *bmain, void *idv, void *old_idv, void *new_idv, const bool us_min_never_null)
  577. {
  578. ID *id = idv;
  579. ID *old_id = old_idv;
  580. ID *new_id = new_idv;
  581. int remap_flags = us_min_never_null ? 0 : ID_REMAP_SKIP_NEVER_NULL_USAGE;
  582. /* No need to lock here, we are only affecting given ID, not bmain database. */
  583. BLI_assert(id);
  584. if (old_id) {
  585. BLI_assert((new_id == NULL) || GS(old_id->name) == GS(new_id->name));
  586. BLI_assert(old_id != new_id);
  587. }
  588. else {
  589. BLI_assert(new_id == NULL);
  590. }
  591. libblock_remap_data(bmain, id, old_id, new_id, remap_flags, NULL);
  592. /* Some after-process updates.
  593. * This is a bit ugly, but cannot see a way to avoid it. Maybe we should do a per-ID callback for this instead?
  594. */
  595. switch (GS(id->name)) {
  596. case ID_SCE:
  597. {
  598. Scene *sce = (Scene *)id;
  599. if (old_id) {
  600. switch (GS(old_id->name)) {
  601. case ID_OB:
  602. {
  603. libblock_remap_data_postprocess_object_update(bmain, (Object *)old_id, (Object *)new_id);
  604. break;
  605. }
  606. case ID_GR:
  607. if (!new_id) { /* Only affects us in case group was unlinked. */
  608. libblock_remap_data_postprocess_group_scene_unlink(bmain, sce, old_id);
  609. }
  610. break;
  611. default:
  612. break;
  613. }
  614. }
  615. else {
  616. /* No choice but to check whole objects/groups. */
  617. for (Object *ob = bmain->object.first; ob; ob = ob->id.next) {
  618. libblock_remap_data_postprocess_object_update(bmain, ob, NULL);
  619. }
  620. for (Group *grp = bmain->group.first; grp; grp = grp->id.next) {
  621. libblock_remap_data_postprocess_group_scene_unlink(bmain, sce, NULL);
  622. }
  623. }
  624. break;
  625. }
  626. case ID_OB:
  627. if (new_id) { /* Only affects us in case obdata was relinked (changed). */
  628. libblock_remap_data_postprocess_obdata_relink(bmain, (Object *)id, new_id);
  629. }
  630. break;
  631. default:
  632. break;
  633. }
  634. }
  635. static int id_relink_to_newid_looper(void *UNUSED(user_data), ID *UNUSED(self_id), ID **id_pointer, const int cb_flag)
  636. {
  637. if (cb_flag & IDWALK_CB_PRIVATE) {
  638. return IDWALK_RET_NOP;
  639. }
  640. ID *id = *id_pointer;
  641. if (id) {
  642. /* See: NEW_ID macro */
  643. if (id->newid) {
  644. BKE_library_update_ID_link_user(id->newid, id, cb_flag);
  645. *id_pointer = id->newid;
  646. }
  647. else if (id->tag & LIB_TAG_NEW) {
  648. id->tag &= ~LIB_TAG_NEW;
  649. BKE_libblock_relink_to_newid(id);
  650. }
  651. }
  652. return IDWALK_RET_NOP;
  653. }
  654. /** Similar to libblock_relink_ex, but is remapping IDs to their newid value if non-NULL, in given \a id.
  655. *
  656. * Very specific usage, not sure we'll keep it on the long run, currently only used in Object duplication code...
  657. */
  658. void BKE_libblock_relink_to_newid(ID *id)
  659. {
  660. if (ID_IS_LINKED_DATABLOCK(id))
  661. return;
  662. BKE_library_foreach_ID_link(NULL, id, id_relink_to_newid_looper, NULL, 0);
  663. }
  664. void BKE_libblock_free_data(ID *id, const bool do_id_user)
  665. {
  666. if (id->properties) {
  667. IDP_FreeProperty_ex(id->properties, do_id_user);
  668. MEM_freeN(id->properties);
  669. }
  670. }
  671. void BKE_libblock_free_datablock(ID *id)
  672. {
  673. const short type = GS(id->name);
  674. switch (type) {
  675. case ID_SCE:
  676. BKE_scene_free((Scene *)id);
  677. break;
  678. case ID_LI:
  679. BKE_library_free((Library *)id);
  680. break;
  681. case ID_OB:
  682. BKE_object_free((Object *)id);
  683. break;
  684. case ID_ME:
  685. BKE_mesh_free((Mesh *)id);
  686. break;
  687. case ID_CU:
  688. BKE_curve_free((Curve *)id);
  689. break;
  690. case ID_MB:
  691. BKE_mball_free((MetaBall *)id);
  692. break;
  693. case ID_MA:
  694. BKE_material_free((Material *)id);
  695. break;
  696. case ID_TE:
  697. BKE_texture_free((Tex *)id);
  698. break;
  699. case ID_IM:
  700. BKE_image_free((Image *)id);
  701. break;
  702. case ID_LT:
  703. BKE_lattice_free((Lattice *)id);
  704. break;
  705. case ID_LA:
  706. BKE_lamp_free((Lamp *)id);
  707. break;
  708. case ID_CA:
  709. BKE_camera_free((Camera *) id);
  710. break;
  711. case ID_IP: /* Deprecated. */
  712. BKE_ipo_free((Ipo *)id);
  713. break;
  714. case ID_KE:
  715. BKE_key_free((Key *)id);
  716. break;
  717. case ID_WO:
  718. BKE_world_free((World *)id);
  719. break;
  720. case ID_SCR:
  721. BKE_screen_free((bScreen *)id);
  722. break;
  723. case ID_VF:
  724. BKE_vfont_free((VFont *)id);
  725. break;
  726. case ID_TXT:
  727. BKE_text_free((Text *)id);
  728. break;
  729. case ID_SPK:
  730. BKE_speaker_free((Speaker *)id);
  731. break;
  732. case ID_SO:
  733. BKE_sound_free((bSound *)id);
  734. break;
  735. case ID_GR:
  736. BKE_group_free((Group *)id);
  737. break;
  738. case ID_AR:
  739. BKE_armature_free((bArmature *)id);
  740. break;
  741. case ID_AC:
  742. BKE_action_free((bAction *)id);
  743. break;
  744. case ID_NT:
  745. ntreeFreeTree((bNodeTree *)id);
  746. break;
  747. case ID_BR:
  748. BKE_brush_free((Brush *)id);
  749. break;
  750. case ID_PA:
  751. BKE_particlesettings_free((ParticleSettings *)id);
  752. break;
  753. case ID_WM:
  754. if (free_windowmanager_cb)
  755. free_windowmanager_cb(NULL, (wmWindowManager *)id);
  756. break;
  757. case ID_GD:
  758. BKE_gpencil_free((bGPdata *)id, true);
  759. break;
  760. case ID_MC:
  761. BKE_movieclip_free((MovieClip *)id);
  762. break;
  763. case ID_MSK:
  764. BKE_mask_free((Mask *)id);
  765. break;
  766. case ID_LS:
  767. BKE_linestyle_free((FreestyleLineStyle *)id);
  768. break;
  769. case ID_PAL:
  770. BKE_palette_free((Palette *)id);
  771. break;
  772. case ID_PC:
  773. BKE_paint_curve_free((PaintCurve *)id);
  774. break;
  775. case ID_CF:
  776. BKE_cachefile_free((CacheFile *)id);
  777. break;
  778. }
  779. }
  780. /**
  781. * used in headerbuttons.c image.c mesh.c screen.c sound.c and library.c
  782. *
  783. * \param do_id_user: if \a true, try to release other ID's 'references' hold by \a idv.
  784. * (only applies to main database)
  785. * \param do_ui_user: similar to do_id_user but makes sure UI does not hold references to
  786. * \a id.
  787. */
  788. void BKE_libblock_free_ex(Main *bmain, void *idv, const bool do_id_user, const bool do_ui_user)
  789. {
  790. ID *id = idv;
  791. short type = GS(id->name);
  792. ListBase *lb = which_libbase(bmain, type);
  793. DAG_id_type_tag(bmain, type);
  794. #ifdef WITH_PYTHON
  795. BPY_id_release(id);
  796. #endif
  797. if (do_id_user) {
  798. BKE_libblock_relink_ex(bmain, id, NULL, NULL, true);
  799. }
  800. BKE_libblock_free_datablock(id);
  801. /* avoid notifying on removed data */
  802. BKE_main_lock(bmain);
  803. if (do_ui_user) {
  804. if (free_notifier_reference_cb) {
  805. free_notifier_reference_cb(id);
  806. }
  807. if (remap_editor_id_reference_cb) {
  808. remap_editor_id_reference_cb(id, NULL);
  809. }
  810. }
  811. BLI_remlink(lb, id);
  812. BKE_libblock_free_data(id, do_id_user);
  813. BKE_main_unlock(bmain);
  814. MEM_freeN(id);
  815. }
  816. void BKE_libblock_free(Main *bmain, void *idv)
  817. {
  818. BKE_libblock_free_ex(bmain, idv, true, true);
  819. }
  820. void BKE_libblock_free_us(Main *bmain, void *idv) /* test users */
  821. {
  822. ID *id = idv;
  823. id_us_min(id);
  824. /* XXX This is a temp (2.77) hack so that we keep same behavior as in 2.76 regarding groups when deleting an object.
  825. * Since only 'user_one' usage of objects is groups, and only 'real user' usage of objects is scenes,
  826. * removing that 'user_one' tag when there is no more real (scene) users of an object ensures it gets
  827. * fully unlinked.
  828. * But only for local objects, not linked ones!
  829. * Otherwise, there is no real way to get rid of an object anymore - better handling of this is TODO.
  830. */
  831. if ((GS(id->name) == ID_OB) && (id->us == 1) && (id->lib == NULL)) {
  832. id_us_clear_real(id);
  833. }
  834. if (id->us == 0) {
  835. BKE_libblock_unlink(bmain, id, false, false);
  836. BKE_libblock_free(bmain, id);
  837. }
  838. }
  839. void BKE_libblock_delete(Main *bmain, void *idv)
  840. {
  841. ListBase *lbarray[MAX_LIBARRAY];
  842. int base_count, i;
  843. base_count = set_listbasepointers(bmain, lbarray);
  844. BKE_main_id_tag_all(bmain, LIB_TAG_DOIT, false);
  845. /* First tag all datablocks directly from target lib.
  846. * Note that we go forward here, since we want to check dependencies before users (e.g. meshes before objects).
  847. * Avoids to have to loop twice. */
  848. for (i = 0; i < base_count; i++) {
  849. ListBase *lb = lbarray[i];
  850. ID *id;
  851. for (id = lb->first; id; id = id->next) {
  852. /* Note: in case we delete a library, we also delete all its datablocks! */
  853. if ((id == (ID *)idv) || (id->lib == (Library *)idv) || (id->tag & LIB_TAG_DOIT)) {
  854. id->tag |= LIB_TAG_DOIT;
  855. /* Will tag 'never NULL' users of this ID too.
  856. * Note that we cannot use BKE_libblock_unlink() here, since it would ignore indirect (and proxy!)
  857. * links, this can lead to nasty crashing here in second, actual deleting loop.
  858. * Also, this will also flag users of deleted data that cannot be unlinked
  859. * (object using deleted obdata, etc.), so that they also get deleted. */
  860. BKE_libblock_remap(bmain, id, NULL, ID_REMAP_FLAG_NEVER_NULL_USAGE | ID_REMAP_FORCE_NEVER_NULL_USAGE);
  861. }
  862. }
  863. }
  864. /* In usual reversed order, such that all usage of a given ID, even 'never NULL' ones, have been already cleared
  865. * when we reach it (e.g. Objects being processed before meshes, they'll have already released their 'reference'
  866. * over meshes when we come to freeing obdata). */
  867. for (i = base_count; i--; ) {
  868. ListBase *lb = lbarray[i];
  869. ID *id, *id_next;
  870. for (id = lb->first; id; id = id_next) {
  871. id_next = id->next;
  872. if (id->tag & LIB_TAG_DOIT) {
  873. if (id->us != 0) {
  874. #ifdef DEBUG_PRINT
  875. printf("%s: deleting %s (%d)\n", __func__, id->name, id->us);
  876. #endif
  877. BLI_assert(id->us == 0);
  878. }
  879. BKE_libblock_free(bmain, id);
  880. }
  881. }
  882. }
  883. }