Blender  V2.93
lib_id.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
17  * All rights reserved.
18  */
19 
27 #include <ctype.h>
28 #include <stddef.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 
33 #include "CLG_log.h"
34 
35 #include "MEM_guardedalloc.h"
36 
37 /* all types are needed here, in order to do memory operations */
38 #include "DNA_ID.h"
39 #include "DNA_anim_types.h"
40 #include "DNA_collection_types.h"
41 #include "DNA_gpencil_types.h"
42 #include "DNA_key_types.h"
43 #include "DNA_node_types.h"
44 #include "DNA_workspace_types.h"
45 
46 #include "BLI_utildefines.h"
47 
48 #include "BLI_alloca.h"
49 #include "BLI_blenlib.h"
50 #include "BLI_ghash.h"
51 #include "BLI_linklist.h"
52 #include "BLI_memarena.h"
53 #include "BLI_string_utils.h"
54 
55 #include "BLT_translation.h"
56 
57 #include "BKE_anim_data.h"
58 #include "BKE_armature.h"
59 #include "BKE_asset.h"
60 #include "BKE_bpath.h"
61 #include "BKE_context.h"
62 #include "BKE_global.h"
63 #include "BKE_gpencil.h"
64 #include "BKE_idprop.h"
65 #include "BKE_idtype.h"
66 #include "BKE_key.h"
67 #include "BKE_lib_id.h"
68 #include "BKE_lib_override.h"
69 #include "BKE_lib_query.h"
70 #include "BKE_lib_remap.h"
71 #include "BKE_main.h"
72 #include "BKE_node.h"
73 #include "BKE_rigidbody.h"
74 
75 #include "DEG_depsgraph.h"
76 #include "DEG_depsgraph_build.h"
77 
78 #include "RNA_access.h"
79 
80 #include "BLO_read_write.h"
81 
82 #include "atomic_ops.h"
83 
84 //#define DEBUG_TIME
85 
86 #ifdef DEBUG_TIME
87 # include "PIL_time_utildefines.h"
88 #endif
89 
90 static CLG_LogRef LOG = {.identifier = "bke.lib_id"};
91 
92 /* Empty shell mostly, but needed for read code. */
95  .id_filter = 0,
96  .main_listbase_index = INDEX_ID_NULL,
97  .struct_size = sizeof(ID),
98  .name = "LinkPlaceholder",
99  .name_plural = "link_placeholders",
100  .translation_context = BLT_I18NCONTEXT_ID_ID,
102 
103  .init_data = NULL,
104  .copy_data = NULL,
105  .free_data = NULL,
106  .make_local = NULL,
107  .foreach_id = NULL,
108  .foreach_cache = NULL,
109 
110  .blend_write = NULL,
111  .blend_read_data = NULL,
112  .blend_read_lib = NULL,
113  .blend_read_expand = NULL,
114 
115  .blend_read_undo_preserve = NULL,
116 
117  .lib_override_apply_post = NULL,
118 };
119 
120 /* GS reads the memory pointed at in a specific ordering.
121  * only use this definition, makes little and big endian systems
122  * work fine, in conjunction with MAKE_ID */
123 
124 /* ************* general ************************ */
125 
130 static void lib_id_library_local_paths(Main *bmain, Library *lib, ID *id)
131 {
132  const char *bpath_user_data[2] = {BKE_main_blendfile_path(bmain), lib->filepath_abs};
133 
134  BKE_bpath_traverse_id(bmain,
135  id,
138  (void *)bpath_user_data);
139 }
140 
142 {
143  ID *id = cb_data->user_data;
144  if (*cb_data->id_pointer == id) {
147  return IDWALK_RET_STOP_ITER;
148  }
149  return IDWALK_RET_NOP;
150 }
151 
156 static void lib_id_clear_library_data_ex(Main *bmain, ID *id)
157 {
158  const bool id_in_mainlist = (id->tag & LIB_TAG_NO_MAIN) == 0 &&
159  (id->flag & LIB_EMBEDDED_DATA) == 0;
160 
161  lib_id_library_local_paths(bmain, id->lib, id);
162 
163  id_fake_user_clear(id);
164 
165  id->lib = NULL;
166  id->tag &= ~(LIB_TAG_INDIRECT | LIB_TAG_EXTERN);
167  id->flag &= ~LIB_INDIRECT_WEAK_LINK;
168  if (id_in_mainlist) {
169  if (BKE_id_new_name_validate(which_libbase(bmain, GS(id->name)), id, NULL)) {
170  bmain->is_memfile_undo_written = false;
171  }
172  }
173 
174  /* Conceptually, an ID made local is not the same as the linked one anymore. Reflect that by
175  * regenerating its session UUID. */
176  if ((id->tag & LIB_TAG_TEMP_MAIN) == 0) {
178  }
179 
180  /* We need to tag this IDs and all of its users, conceptually new local ID and original linked
181  * ones are two completely different data-blocks that were virtually remapped, even though in
182  * reality they remain the same data. For undo this info is critical now. */
184  ID *id_iter;
185  FOREACH_MAIN_ID_BEGIN (bmain, id_iter) {
188  }
190 
191  /* Internal shape key blocks inside data-blocks also stores id->lib,
192  * make sure this stays in sync (note that we do not need any explicit handling for real EMBEDDED
193  * IDs here, this is down automatically in `lib_id_expand_local_cb()`. */
194  Key *key = BKE_key_from_id(id);
195  if (key != NULL) {
196  lib_id_clear_library_data_ex(bmain, &key->id);
197  }
198 
200 }
201 
203 {
204  lib_id_clear_library_data_ex(bmain, id);
205 }
206 
207 void id_lib_extern(ID *id)
208 {
209  if (id && ID_IS_LINKED(id)) {
211  if (id->tag & LIB_TAG_INDIRECT) {
212  id->tag &= ~LIB_TAG_INDIRECT;
213  id->flag &= ~LIB_INDIRECT_WEAK_LINK;
214  id->tag |= LIB_TAG_EXTERN;
215  id->lib->parent = NULL;
216  }
217  }
218 }
219 
221 {
222  if (id && ID_IS_LINKED(id)) {
224  if (id->tag & LIB_TAG_INDIRECT) {
225  id->flag |= LIB_INDIRECT_WEAK_LINK;
226  }
227  }
228 }
229 
239 {
240  if (id) {
241  const int limit = ID_FAKE_USERS(id);
242  id->tag |= LIB_TAG_EXTRAUSER;
243  if (id->us <= limit) {
244  if (id->us < limit || ((id->us == limit) && (id->tag & LIB_TAG_EXTRAUSER_SET))) {
245  CLOG_ERROR(&LOG,
246  "ID user count error: %s (from '%s')",
247  id->name,
248  id->lib ? id->lib->filepath_abs : "[Main]");
249  BLI_assert(0);
250  }
251  id->us = limit + 1;
252  id->tag |= LIB_TAG_EXTRAUSER_SET;
253  }
254  }
255 }
256 
258 {
259  if (id && (id->tag & LIB_TAG_EXTRAUSER)) {
260  if (id->tag & LIB_TAG_EXTRAUSER_SET) {
261  id->us--;
262  BLI_assert(id->us >= ID_FAKE_USERS(id));
263  }
265  }
266 }
267 
273 {
274  if (id) {
275  if ((id->tag & LIB_TAG_EXTRAUSER) && (id->tag & LIB_TAG_EXTRAUSER_SET)) {
276  BLI_assert(id->us >= 1);
277  /* No need to increase count, just tag extra user as no more set.
278  * Avoids annoying & inconsistent +1 in user count. */
279  id->tag &= ~LIB_TAG_EXTRAUSER_SET;
280  }
281  else {
282  BLI_assert(id->us >= 0);
283  id->us++;
284  }
285  }
286 }
287 
288 void id_us_plus(ID *id)
289 {
290  if (id) {
291  id_us_plus_no_lib(id);
292  id_lib_extern(id);
293  }
294 }
295 
296 /* decrements the user count for *id. */
297 void id_us_min(ID *id)
298 {
299  if (id) {
300  const int limit = ID_FAKE_USERS(id);
301 
302  if (id->us <= limit) {
303  if (GS(id->name) != ID_IP) {
304  /* Do not assert on deprecated ID types, we cannot really ensure that their ID refcounting
305  * is valid... */
306  CLOG_ERROR(&LOG,
307  "ID user decrement error: %s (from '%s'): %d <= %d",
308  id->name,
309  id->lib ? id->lib->filepath_abs : "[Main]",
310  id->us,
311  limit);
312  BLI_assert(0);
313  }
314  id->us = limit;
315  }
316  else {
317  id->us--;
318  }
319 
320  if ((id->us == limit) && (id->tag & LIB_TAG_EXTRAUSER)) {
321  /* We need an extra user here, but never actually incremented user count for it so far,
322  * do it now. */
323  id_us_ensure_real(id);
324  }
325  }
326 }
327 
329 {
330  if (id && !(id->flag & LIB_FAKEUSER)) {
331  id->flag |= LIB_FAKEUSER;
332  id_us_plus(id);
333  }
334 }
335 
337 {
338  if (id && (id->flag & LIB_FAKEUSER)) {
339  id->flag &= ~LIB_FAKEUSER;
340  id_us_min(id);
341  }
342 }
343 
345 {
346  if (id->newid) {
347  id->newid->tag &= ~LIB_TAG_NEW;
348  }
349  id->newid = NULL;
350 }
351 
353 {
354  Main *bmain = cb_data->bmain;
355  ID *id_self = cb_data->id_self;
356  ID **id_pointer = cb_data->id_pointer;
357  int const cb_flag = cb_data->cb_flag;
358 
359  if (cb_flag & IDWALK_CB_LOOPBACK) {
360  /* We should never have anything to do with loop-back pointers here. */
361  return IDWALK_RET_NOP;
362  }
363 
364  if (cb_flag & IDWALK_CB_EMBEDDED) {
365  /* Embedded data-blocks need to be made fully local as well.
366  * Note however that in some cases (when owner ID had to be duplicated instead of being made
367  * local directly), its embedded IDs should also have already been duplicated, and hence be
368  * fully local here already. */
369  if (*id_pointer != NULL && ID_IS_LINKED(*id_pointer)) {
370  BLI_assert(*id_pointer != id_self);
371 
372  lib_id_clear_library_data_ex(bmain, *id_pointer);
373  }
374  return IDWALK_RET_NOP;
375  }
376 
377  /* Can happen that we get un-linkable ID here, e.g. with shape-key referring to itself
378  * (through drivers)...
379  * Just skip it, shape key can only be either indirectly linked, or fully local, period.
380  * And let's curse one more time that stupid useless shapekey ID type! */
381  if (*id_pointer && *id_pointer != id_self &&
382  BKE_idtype_idcode_is_linkable(GS((*id_pointer)->name))) {
383  id_lib_extern(*id_pointer);
384  }
385 
386  return IDWALK_RET_NOP;
387 }
388 
393 void BKE_lib_id_expand_local(Main *bmain, ID *id)
394 {
396 }
397 
401 static void lib_id_copy_ensure_local(Main *bmain, const ID *old_id, ID *new_id)
402 {
403  if (ID_IS_LINKED(old_id)) {
404  BKE_lib_id_expand_local(bmain, new_id);
405  lib_id_library_local_paths(bmain, old_id->lib, new_id);
406  }
407 }
408 
412 void BKE_lib_id_make_local_generic(Main *bmain, ID *id, const int flags)
413 {
414  const bool lib_local = (flags & LIB_ID_MAKELOCAL_FULL_LIBRARY) != 0;
415  bool is_local = false, is_lib = false;
416 
417  /* - only lib users: do nothing (unless force_local is set)
418  * - only local users: set flag
419  * - mixed: make copy
420  * In case we make a whole lib's content local,
421  * we always want to localize, and we skip remapping (done later).
422  */
423 
424  if (!ID_IS_LINKED(id)) {
425  return;
426  }
427 
428  BKE_library_ID_test_usages(bmain, id, &is_local, &is_lib);
429 
430  if (lib_local || is_local) {
431  if (!is_lib) {
432  lib_id_clear_library_data_ex(bmain, id);
433  BKE_lib_id_expand_local(bmain, id);
434  }
435  else {
436  ID *id_new = BKE_id_copy(bmain, id);
437 
438  /* Should not fail in expected use cases,
439  * but a few ID types cannot be copied (LIB, WM, SCR...). */
440  if (id_new != NULL) {
441  id_new->us = 0;
442 
443  /* setting newid is mandatory for complex make_lib_local logic... */
444  ID_NEW_SET(id, id_new);
445  Key *key = BKE_key_from_id(id), *key_new = BKE_key_from_id(id);
446  if (key && key_new) {
447  ID_NEW_SET(key, key_new);
448  }
449  bNodeTree *ntree = ntreeFromID(id), *ntree_new = ntreeFromID(id_new);
450  if (ntree && ntree_new) {
451  ID_NEW_SET(ntree, ntree_new);
452  }
453  if (GS(id->name) == ID_SCE) {
454  Collection *master_collection = ((Scene *)id)->master_collection,
455  *master_collection_new = ((Scene *)id_new)->master_collection;
456  if (master_collection && master_collection_new) {
457  ID_NEW_SET(master_collection, master_collection_new);
458  }
459  }
460 
461  if (!lib_local) {
463  }
464  }
465  }
466  }
467 }
468 
479 bool BKE_lib_id_make_local(Main *bmain, ID *id, const bool test, const int flags)
480 {
481  const bool lib_local = (flags & LIB_ID_MAKELOCAL_FULL_LIBRARY) != 0;
482 
483  /* We don't care whether ID is directly or indirectly linked
484  * in case we are making a whole lib local... */
485  if (!lib_local && (id->tag & LIB_TAG_INDIRECT)) {
486  return false;
487  }
488 
489  const IDTypeInfo *idtype_info = BKE_idtype_get_info_from_id(id);
490 
491  if (idtype_info != NULL) {
492  if ((idtype_info->flags & IDTYPE_FLAGS_NO_MAKELOCAL) == 0) {
493  if (!test) {
494  if (idtype_info->make_local != NULL) {
495  idtype_info->make_local(bmain, id, flags);
496  }
497  else {
498  BKE_lib_id_make_local_generic(bmain, id, flags);
499  }
500  }
501  return true;
502  }
503  return false;
504  }
505 
506  BLI_assert(!"IDType Missing IDTypeInfo");
507  return false;
508 }
509 
511  const ID *id_src;
513  int flag;
514 };
515 
516 /* Increases usercount as required, and remap self ID pointers. */
518 {
519  ID **id_pointer = cb_data->id_pointer;
520  ID *id = *id_pointer;
521  const int cb_flag = cb_data->cb_flag;
522  struct IDCopyLibManagementData *data = cb_data->user_data;
523 
524  /* Remap self-references to new copied ID. */
525  if (id == data->id_src) {
526  /* We cannot use id_self here, it is not *always* id_dst (thanks to $£!+@#&/? nodetrees). */
527  id = *id_pointer = data->id_dst;
528  }
529 
530  /* Increase used IDs refcount if needed and required. */
531  if ((data->flag & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0 && (cb_flag & IDWALK_CB_USER)) {
532  id_us_plus(id);
533  }
534 
535  return IDWALK_RET_NOP;
536 }
537 
538 bool BKE_id_copy_is_allowed(const ID *id)
539 {
540 #define LIB_ID_TYPES_NOCOPY \
541  ID_LI, ID_SCR, ID_WM, ID_WS, /* Not supported */ \
542  ID_IP /* Deprecated */
543 
544  return !ELEM(GS(id->name), LIB_ID_TYPES_NOCOPY);
545 
546 #undef LIB_ID_TYPES_NOCOPY
547 }
548 
570 ID *BKE_id_copy_ex(Main *bmain, const ID *id, ID **r_newid, const int flag)
571 {
572  ID *newid = (r_newid != NULL) ? *r_newid : NULL;
573  /* Make sure destination pointer is all good. */
574  if ((flag & LIB_ID_CREATE_NO_ALLOCATE) == 0) {
575  newid = NULL;
576  }
577  else {
578  if (newid != NULL) {
579  /* Allow some garbage non-initialized memory to go in, and clean it up here. */
580  const size_t size = BKE_libblock_get_alloc_info(GS(id->name), NULL);
581  memset(newid, 0, size);
582  }
583  }
584 
585  /* Early output is source is NULL. */
586  if (id == NULL) {
587  return NULL;
588  }
589 
590  const IDTypeInfo *idtype_info = BKE_idtype_get_info_from_id(id);
591 
592  if (idtype_info != NULL) {
593  if ((idtype_info->flags & IDTYPE_FLAGS_NO_COPY) != 0) {
594  return NULL;
595  }
596 
597  BKE_libblock_copy_ex(bmain, id, &newid, flag);
598 
599  if (idtype_info->copy_data != NULL) {
600  idtype_info->copy_data(bmain, newid, id, flag);
601  }
602  }
603  else {
604  BLI_assert(!"IDType Missing IDTypeInfo");
605  }
606 
607  /* Update ID refcount, remap pointers to self in new ID. */
608  struct IDCopyLibManagementData data = {
609  .id_src = id,
610  .id_dst = newid,
611  .flag = flag,
612  };
614 
615  /* Do not make new copy local in case we are copying outside of main...
616  * XXX TODO: is this behavior OK, or should we need own flag to control that? */
617  if ((flag & LIB_ID_CREATE_NO_MAIN) == 0) {
619  lib_id_copy_ensure_local(bmain, id, newid);
620  }
621  else {
622  newid->lib = id->lib;
623  }
624 
625  if (r_newid != NULL) {
626  *r_newid = newid;
627  }
628 
629  return newid;
630 }
631 
636 ID *BKE_id_copy(Main *bmain, const ID *id)
637 {
638  return BKE_id_copy_ex(bmain, id, NULL, LIB_ID_COPY_DEFAULT);
639 }
640 
645 ID *BKE_id_copy_for_duplicate(Main *bmain, ID *id, const eDupli_ID_Flags duplicate_flags)
646 {
647  if (id == NULL) {
648  return id;
649  }
650  if (id->newid == NULL) {
651  const bool do_linked_id = (duplicate_flags & USER_DUP_LINKED_ID) != 0;
652  if (!(do_linked_id || !ID_IS_LINKED(id))) {
653  return id;
654  }
655 
656  ID *id_new = BKE_id_copy(bmain, id);
657  /* Copying add one user by default, need to get rid of that one. */
658  id_us_min(id_new);
659  ID_NEW_SET(id, id_new);
660 
661  /* Shape keys are always copied with their owner ID, by default. */
662  ID *key_new = (ID *)BKE_key_from_id(id_new);
663  ID *key = (ID *)BKE_key_from_id(id);
664  if (key != NULL) {
665  ID_NEW_SET(key, key_new);
666  }
667 
668  /* Note: embedded data (root nodetrees and master collections) should never be referenced by
669  * anything else, so we do not need to set their newid pointer and flag. */
670 
671  BKE_animdata_duplicate_id_action(bmain, id_new, duplicate_flags);
672  if (key_new != NULL) {
673  BKE_animdata_duplicate_id_action(bmain, key_new, duplicate_flags);
674  }
675  /* Note that actions of embedded data (root nodetrees and master collections) are handled
676  * by `BKE_animdata_duplicate_id_action` as well. */
677  }
678  return id->newid;
679 }
680 
685 static void id_swap(Main *bmain, ID *id_a, ID *id_b, const bool do_full_id)
686 {
687  BLI_assert(GS(id_a->name) == GS(id_b->name));
688 
689  const IDTypeInfo *id_type = BKE_idtype_get_info_from_id(id_a);
690  BLI_assert(id_type != NULL);
691  const size_t id_struct_size = id_type->struct_size;
692 
693  const ID id_a_back = *id_a;
694  const ID id_b_back = *id_b;
695 
696  char *id_swap_buff = alloca(id_struct_size);
697 
698  memcpy(id_swap_buff, id_a, id_struct_size);
699  memcpy(id_a, id_b, id_struct_size);
700  memcpy(id_b, id_swap_buff, id_struct_size);
701 
702  if (!do_full_id) {
703  /* Restore original ID's internal data. */
704  *id_a = id_a_back;
705  *id_b = id_b_back;
706 
707  /* Exception: IDProperties. */
708  id_a->properties = id_b_back.properties;
709  id_b->properties = id_a_back.properties;
710  /* Exception: recalc flags. */
711  id_a->recalc = id_b_back.recalc;
712  id_b->recalc = id_a_back.recalc;
713  }
714 
715  if (bmain != NULL) {
716  /* Swap will have broken internal references to itself, restore them. */
717  BKE_libblock_relink_ex(bmain, id_a, id_b, id_a, ID_REMAP_SKIP_NEVER_NULL_USAGE);
718  BKE_libblock_relink_ex(bmain, id_b, id_a, id_b, ID_REMAP_SKIP_NEVER_NULL_USAGE);
719  }
720 }
721 
729 void BKE_lib_id_swap(Main *bmain, ID *id_a, ID *id_b)
730 {
731  id_swap(bmain, id_a, id_b, false);
732 }
733 
741 void BKE_lib_id_swap_full(Main *bmain, ID *id_a, ID *id_b)
742 {
743  id_swap(bmain, id_a, id_b, true);
744 }
745 
748 {
749  ID *newid = NULL;
750  PointerRNA idptr;
751 
752  if (id) {
753  /* If property isn't editable,
754  * we're going to have an extra block hanging around until we save. */
755  if (RNA_property_editable(ptr, prop)) {
756  Main *bmain = CTX_data_main(C);
757  /* copy animation actions too */
759  if (newid != NULL) {
760  /* us is 1 by convention with new IDs, but RNA_property_pointer_set
761  * will also increment it, decrement it here. */
762  id_us_min(newid);
763 
764  /* assign copy */
765  RNA_id_pointer_create(newid, &idptr);
766  RNA_property_pointer_set(ptr, prop, idptr, NULL);
767  RNA_property_update(C, ptr, prop);
768 
769  /* tag grease pencil data-block and disable onion */
770  if (GS(id->name) == ID_GD) {
773  bGPdata *gpd = (bGPdata *)newid;
774  gpd->flag &= ~GP_DATA_SHOW_ONIONSKINS;
775  }
776 
777  return true;
778  }
779  }
780  }
781 
782  return false;
783 }
784 
786 {
787  ID **id_pointer = cb_data->id_pointer;
788  const int cb_flag = cb_data->cb_flag;
789  if (cb_flag & IDWALK_CB_USER) {
790  id_us_plus(*id_pointer);
791  }
792  if (cb_flag & IDWALK_CB_USER_ONE) {
793  id_us_ensure_real(*id_pointer);
794  }
795 
796  return IDWALK_RET_NOP;
797 }
798 
800 {
801  ID **id_pointer = cb_data->id_pointer;
802  const int cb_flag = cb_data->cb_flag;
803  if (cb_flag & IDWALK_CB_USER) {
804  id_us_min(*id_pointer);
805  }
806  /* We can do nothing in IDWALK_CB_USER_ONE case! */
807 
808  return IDWALK_RET_NOP;
809 }
810 
812 void BKE_libblock_management_main_add(Main *bmain, void *idv)
813 {
814  ID *id = idv;
815 
816  BLI_assert(bmain != NULL);
817  if ((id->tag & LIB_TAG_NO_MAIN) == 0) {
818  return;
819  }
820 
821  if ((id->tag & LIB_TAG_NOT_ALLOCATED) != 0) {
822  /* We cannot add non-allocated ID to Main! */
823  return;
824  }
825 
826  /* We cannot allow non-userrefcounting IDs in Main database! */
827  if ((id->tag & LIB_TAG_NO_USER_REFCOUNT) != 0) {
829  }
830 
831  ListBase *lb = which_libbase(bmain, GS(id->name));
832  BKE_main_lock(bmain);
833  BLI_addtail(lb, id);
835  /* alphabetic insertion: is in new_id */
837  bmain->is_memfile_undo_written = false;
838  BKE_main_unlock(bmain);
839 
841 }
842 
845 {
846  ID *id = idv;
847 
848  BLI_assert(bmain != NULL);
849  if ((id->tag & LIB_TAG_NO_MAIN) != 0) {
850  return;
851  }
852 
853  /* For now, allow userrefcounting IDs to get out of Main - can be handy in some cases... */
854 
855  ListBase *lb = which_libbase(bmain, GS(id->name));
856  BKE_main_lock(bmain);
857  BLI_remlink(lb, id);
858  id->tag |= LIB_TAG_NO_MAIN;
859  bmain->is_memfile_undo_written = false;
860  BKE_main_unlock(bmain);
861 }
862 
864 {
865  ID *id = idv;
866 
867  if ((id->tag & LIB_TAG_NO_USER_REFCOUNT) == 0) {
868  return;
869  }
870 
872  id->tag &= ~LIB_TAG_NO_USER_REFCOUNT;
873 }
874 
876 {
877  ID *id = idv;
878 
879  /* We do not allow IDs in Main database to not be userrefcounting. */
880  if ((id->tag & LIB_TAG_NO_USER_REFCOUNT) != 0 || (id->tag & LIB_TAG_NO_MAIN) != 0) {
881  return;
882  }
883 
885  id->tag |= LIB_TAG_NO_USER_REFCOUNT;
886 }
887 
891 void BKE_main_id_tag_listbase(ListBase *lb, const int tag, const bool value)
892 {
893  ID *id;
894  if (value) {
895  for (id = lb->first; id; id = id->next) {
896  id->tag |= tag;
897  }
898  }
899  else {
900  const int ntag = ~tag;
901  for (id = lb->first; id; id = id->next) {
902  id->tag &= ntag;
903  }
904  }
905 }
906 
910 void BKE_main_id_tag_idcode(struct Main *mainvar,
911  const short type,
912  const int tag,
913  const bool value)
914 {
915  ListBase *lb = which_libbase(mainvar, type);
916 
917  BKE_main_id_tag_listbase(lb, tag, value);
918 }
919 
923 void BKE_main_id_tag_all(struct Main *mainvar, const int tag, const bool value)
924 {
925  ListBase *lbarray[INDEX_ID_MAX];
926  int a;
927 
928  a = set_listbasepointers(mainvar, lbarray);
929  while (a--) {
930  BKE_main_id_tag_listbase(lbarray[a], tag, value);
931  }
932 }
933 
937 void BKE_main_id_flag_listbase(ListBase *lb, const int flag, const bool value)
938 {
939  ID *id;
940  if (value) {
941  for (id = lb->first; id; id = id->next) {
942  id->tag |= flag;
943  }
944  }
945  else {
946  const int nflag = ~flag;
947  for (id = lb->first; id; id = id->next) {
948  id->tag &= nflag;
949  }
950  }
951 }
952 
956 void BKE_main_id_flag_all(Main *bmain, const int flag, const bool value)
957 {
958  ListBase *lbarray[INDEX_ID_MAX];
959  int a;
960  a = set_listbasepointers(bmain, lbarray);
961  while (a--) {
962  BKE_main_id_flag_listbase(lbarray[a], flag, value);
963  }
964 }
965 
967 {
968  int lb_len = 0;
969  LISTBASE_FOREACH (ID *, id, lb) {
970  if (id->lib == NULL) {
971  lb_len += 1;
972  }
973  }
974  if (lb_len <= 1) {
975  return;
976  }
977 
978  /* Fill an array because renaming sorts. */
979  ID **id_array = MEM_mallocN(sizeof(*id_array) * lb_len, __func__);
980  GSet *gset = BLI_gset_str_new_ex(__func__, lb_len);
981  int i = 0;
982  LISTBASE_FOREACH (ID *, id, lb) {
983  if (id->lib == NULL) {
984  id_array[i] = id;
985  i++;
986  }
987  }
988  for (i = 0; i < lb_len; i++) {
989  if (!BLI_gset_add(gset, id_array[i]->name + 2)) {
990  BKE_id_new_name_validate(lb, id_array[i], NULL);
991  }
992  }
993  BLI_gset_free(gset, NULL);
994  MEM_freeN(id_array);
995 }
996 
998 {
999  Object *ob;
1000 
1001  /* flag for full recalc */
1002  for (ob = bmain->objects.first; ob; ob = ob->id.next) {
1003  if (ID_IS_LINKED(ob)) {
1005  }
1006  }
1007 
1008  DEG_id_type_tag(bmain, ID_OB);
1009 }
1010 
1011 /* *********** ALLOC AND FREE *****************
1012  *
1013  * BKE_libblock_free(ListBase *lb, ID *id )
1014  * provide a list-basis and data-block, but only ID is read
1015  *
1016  * void *BKE_libblock_alloc(ListBase *lb, type, name)
1017  * inserts in list and returns a new ID
1018  *
1019  * **************************** */
1020 
1024 size_t BKE_libblock_get_alloc_info(short type, const char **name)
1025 {
1027 
1028  if (id_type == NULL) {
1029  if (name != NULL) {
1030  *name = NULL;
1031  }
1032  return 0;
1033  }
1034 
1035  if (name != NULL) {
1036  *name = id_type->name;
1037  }
1038  return id_type->struct_size;
1039 }
1040 
1046 {
1047  const char *name;
1048  size_t size = BKE_libblock_get_alloc_info(type, &name);
1049  if (size != 0) {
1050  return MEM_callocN(size, name);
1051  }
1052  BLI_assert(!"Request to allocate unknown data type");
1053  return NULL;
1054 }
1055 
1062 void *BKE_libblock_alloc(Main *bmain, short type, const char *name, const int flag)
1063 {
1065  BLI_assert((flag & LIB_ID_CREATE_NO_MAIN) != 0 || bmain != NULL);
1067 
1069 
1070  if (id) {
1071  if ((flag & LIB_ID_CREATE_NO_MAIN) != 0) {
1072  id->tag |= LIB_TAG_NO_MAIN;
1073  }
1074  if ((flag & LIB_ID_CREATE_NO_USER_REFCOUNT) != 0) {
1075  id->tag |= LIB_TAG_NO_USER_REFCOUNT;
1076  }
1077  if (flag & LIB_ID_CREATE_LOCAL) {
1078  id->tag |= LIB_TAG_LOCALIZED;
1079  }
1080 
1081  id->icon_id = 0;
1082  *((short *)id->name) = type;
1083  if ((flag & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0) {
1084  id->us = 1;
1085  }
1086  if ((flag & LIB_ID_CREATE_NO_MAIN) == 0) {
1087  /* Note that 2.8x versioning has tested not to cause conflicts. */
1088  BLI_assert(bmain->is_locked_for_linking == false || ELEM(type, ID_WS, ID_GR));
1089  ListBase *lb = which_libbase(bmain, type);
1090 
1091  BKE_main_lock(bmain);
1092  BLI_addtail(lb, id);
1093  BKE_id_new_name_validate(lb, id, name);
1094  bmain->is_memfile_undo_written = false;
1095  /* alphabetic insertion: is in new_id */
1096  BKE_main_unlock(bmain);
1097 
1098  /* TODO to be removed from here! */
1099  if ((flag & LIB_ID_CREATE_NO_DEG_TAG) == 0) {
1100  DEG_id_type_tag(bmain, type);
1101  }
1102  }
1103  else {
1104  BLI_strncpy(id->name + 2, name, sizeof(id->name) - 2);
1105  }
1106 
1107  /* We also need to ensure a valid `session_uuid` for some non-main data (like embedded IDs).
1108  * IDs not allocated however should not need those (this would e.g. avoid generating session
1109  * uuids for depsgraph CoW IDs, if it was using this function). */
1110  if ((flag & LIB_ID_CREATE_NO_ALLOCATE) == 0) {
1112  }
1113  }
1114 
1115  return id;
1116 }
1117 
1123 {
1124  const IDTypeInfo *idtype_info = BKE_idtype_get_info_from_id(id);
1125 
1126  if (idtype_info != NULL) {
1127  if (idtype_info->init_data != NULL) {
1128  idtype_info->init_data(id);
1129  }
1130  return;
1131  }
1132 
1133  BLI_assert(!"IDType Missing IDTypeInfo");
1134 }
1135 
1136 /* ********** ID session-wise UUID management. ********** */
1138 
1146 {
1148  BLI_assert((id->tag & LIB_TAG_TEMP_MAIN) == 0); /* Caller must ensure this. */
1149  id->session_uuid = atomic_add_and_fetch_uint32(&global_session_uuid, 1);
1150  /* In case overflow happens, still assign a valid ID. This way opening files many times works
1151  * correctly. */
1153  id->session_uuid = atomic_add_and_fetch_uint32(&global_session_uuid, 1);
1154  }
1155  }
1156 }
1157 
1167 {
1168  id->session_uuid = MAIN_ID_SESSION_UUID_UNSET;
1170 }
1171 
1177 void *BKE_id_new(Main *bmain, const short type, const char *name)
1178 {
1179  BLI_assert(bmain != NULL);
1180 
1181  if (name == NULL) {
1183  }
1184 
1185  ID *id = BKE_libblock_alloc(bmain, type, name, 0);
1187 
1188  return id;
1189 }
1190 
1196 void *BKE_id_new_nomain(const short type, const char *name)
1197 {
1198  if (name == NULL) {
1200  }
1201 
1202  ID *id = BKE_libblock_alloc(NULL,
1203  type,
1204  name,
1208 
1209  return id;
1210 }
1211 
1212 void BKE_libblock_copy_ex(Main *bmain, const ID *id, ID **r_newid, const int orig_flag)
1213 {
1214  ID *new_id = *r_newid;
1215  int flag = orig_flag;
1216 
1217  const bool is_private_id_data = (id->flag & LIB_EMBEDDED_DATA) != 0;
1218 
1219  BLI_assert((flag & LIB_ID_CREATE_NO_MAIN) != 0 || bmain != NULL);
1222  if (!is_private_id_data) {
1223  /* When we are handling private ID data, we might still want to manage usercounts, even
1224  * though that ID data-block is actually outside of Main... */
1227  }
1228  /* Never implicitly copy shapekeys when generating temp data outside of Main database. */
1230 
1231  /* 'Private ID' data handling. */
1232  if ((bmain != NULL) && is_private_id_data) {
1234  }
1235 
1236  /* The id->flag bits to copy over. */
1237  const int copy_idflag_mask = LIB_EMBEDDED_DATA;
1238 
1239  if ((flag & LIB_ID_CREATE_NO_ALLOCATE) != 0) {
1240  /* r_newid already contains pointer to allocated memory. */
1241  /* TODO do we want to memset(0) whole mem before filling it? */
1242  BLI_strncpy(new_id->name, id->name, sizeof(new_id->name));
1243  new_id->us = 0;
1245  /* TODO Do we want/need to copy more from ID struct itself? */
1246  }
1247  else {
1248  new_id = BKE_libblock_alloc(bmain, GS(id->name), id->name + 2, flag);
1249  }
1250  BLI_assert(new_id != NULL);
1251 
1252  if ((flag & LIB_ID_COPY_SET_COPIED_ON_WRITE) != 0) {
1253  new_id->tag |= LIB_TAG_COPIED_ON_WRITE;
1254  }
1255  else {
1256  new_id->tag &= ~LIB_TAG_COPIED_ON_WRITE;
1257  }
1258 
1259  const size_t id_len = BKE_libblock_get_alloc_info(GS(new_id->name), NULL);
1260  const size_t id_offset = sizeof(ID);
1261  if ((int)id_len - (int)id_offset > 0) { /* signed to allow neg result */ /* XXX ????? */
1262  const char *cp = (const char *)id;
1263  char *cpn = (char *)new_id;
1264 
1265  memcpy(cpn + id_offset, cp + id_offset, id_len - id_offset);
1266  }
1267 
1268  new_id->flag = (new_id->flag & ~copy_idflag_mask) | (id->flag & copy_idflag_mask);
1269 
1270  /* We do not want any handling of usercount in code duplicating the data here, we do that all
1271  * at once in id_copy_libmanagement_cb() at the end. */
1272  const int copy_data_flag = orig_flag | LIB_ID_CREATE_NO_USER_REFCOUNT;
1273 
1274  if (id->properties) {
1275  new_id->properties = IDP_CopyProperty_ex(id->properties, copy_data_flag);
1276  }
1277 
1278  if ((orig_flag & LIB_ID_COPY_NO_LIB_OVERRIDE) == 0) {
1279  if (ID_IS_OVERRIDE_LIBRARY_REAL(id)) {
1280  /* We do not want to copy existing override rules here, as they would break the proper
1281  * remapping between IDs. Proper overrides rules will be re-generated anyway. */
1282  BKE_lib_override_library_copy(new_id, id, false);
1283  }
1284  else if (ID_IS_OVERRIDE_LIBRARY_VIRTUAL(id)) {
1285  /* Just ensure virtual overrides do get properly tagged, there is not actual override data to
1286  * copy here. */
1288  }
1289  }
1290 
1291  if (id_can_have_animdata(new_id)) {
1292  IdAdtTemplate *iat = (IdAdtTemplate *)new_id;
1293 
1294  /* the duplicate should get a copy of the animdata */
1295  if ((flag & LIB_ID_COPY_NO_ANIMDATA) == 0) {
1296  /* Note that even though horrors like root nodetrees are not in bmain, the actions they use
1297  * in their anim data *are* in bmain... super-mega-hooray. */
1298  BLI_assert((copy_data_flag & LIB_ID_COPY_ACTIONS) == 0 ||
1299  (copy_data_flag & LIB_ID_CREATE_NO_MAIN) == 0);
1300  iat->adt = BKE_animdata_copy(bmain, iat->adt, copy_data_flag);
1301  }
1302  else {
1303  iat->adt = NULL;
1304  }
1305  }
1306 
1307  if ((flag & LIB_ID_CREATE_NO_DEG_TAG) == 0 && (flag & LIB_ID_CREATE_NO_MAIN) == 0) {
1308  DEG_id_type_tag(bmain, GS(new_id->name));
1309  }
1310 
1311  *r_newid = new_id;
1312 }
1313 
1314 /* used everywhere in blenkernel */
1315 void *BKE_libblock_copy(Main *bmain, const ID *id)
1316 {
1317  ID *idn;
1318 
1319  BKE_libblock_copy_ex(bmain, id, &idn, 0);
1320 
1321  return idn;
1322 }
1323 
1324 /* XXX TODO: get rid of this useless wrapper at some point... */
1326 {
1327  ID *idn;
1329  return idn;
1330 }
1331 
1332 /* ***************** ID ************************ */
1333 ID *BKE_libblock_find_name(struct Main *bmain, const short type, const char *name)
1334 {
1335  ListBase *lb = which_libbase(bmain, type);
1336  BLI_assert(lb != NULL);
1337  return BLI_findstring(lb, name, offsetof(ID, name) + 2);
1338 }
1339 
1348 void id_sort_by_name(ListBase *lb, ID *id, ID *id_sorting_hint)
1349 {
1350 #define ID_SORT_STEP_SIZE 512
1351 
1352  ID *idtest;
1353 
1354  /* insert alphabetically */
1355  if (lb->first == lb->last) {
1356  return;
1357  }
1358 
1359  BLI_remlink(lb, id);
1360 
1361  /* Check if we can actually insert id before or after id_sorting_hint, if given. */
1362  if (!ELEM(id_sorting_hint, NULL, id)) {
1363  BLI_assert(BLI_findindex(lb, id_sorting_hint) >= 0);
1364 
1365  ID *id_sorting_hint_next = id_sorting_hint->next;
1366  if (BLI_strcasecmp(id_sorting_hint->name, id->name) < 0 &&
1367  (id_sorting_hint_next == NULL ||
1368  BLI_strcasecmp(id_sorting_hint_next->name, id->name) > 0)) {
1369  BLI_insertlinkafter(lb, id_sorting_hint, id);
1370  return;
1371  }
1372 
1373  ID *id_sorting_hint_prev = id_sorting_hint->prev;
1374  if (BLI_strcasecmp(id_sorting_hint->name, id->name) > 0 &&
1375  (id_sorting_hint_prev == NULL ||
1376  BLI_strcasecmp(id_sorting_hint_prev->name, id->name) < 0)) {
1377  BLI_insertlinkbefore(lb, id_sorting_hint, id);
1378  return;
1379  }
1380  }
1381 
1382  void *item_array[ID_SORT_STEP_SIZE];
1383  int item_array_index;
1384 
1385  /* Step one: We go backward over a whole chunk of items at once, until we find a limit item
1386  * that is lower than, or equal (should never happen!) to the one we want to insert. */
1387  /* Note: We start from the end, because in typical 'heavy' case (insertion of lots of IDs at
1388  * once using the same base name), newly inserted items will generally be towards the end
1389  * (higher extension numbers). */
1390  for (idtest = lb->last, item_array_index = ID_SORT_STEP_SIZE - 1; idtest != NULL;
1391  idtest = idtest->prev, item_array_index--) {
1392  item_array[item_array_index] = idtest;
1393  if (item_array_index == 0) {
1394  if ((idtest->lib == NULL && id->lib != NULL) ||
1395  BLI_strcasecmp(idtest->name, id->name) <= 0) {
1396  break;
1397  }
1398  item_array_index = ID_SORT_STEP_SIZE;
1399  }
1400  }
1401 
1402  /* Step two: we go forward in the selected chunk of items and check all of them, as we know
1403  * that our target is in there. */
1404 
1405  /* If we reached start of the list, current item_array_index is off-by-one.
1406  * Otherwise, we already know that it points to an item lower-or-equal-than the one we want to
1407  * insert, no need to redo the check for that one.
1408  * So we can increment that index in any case. */
1409  for (item_array_index++; item_array_index < ID_SORT_STEP_SIZE; item_array_index++) {
1410  idtest = item_array[item_array_index];
1411  if ((idtest->lib != NULL && id->lib == NULL) || BLI_strcasecmp(idtest->name, id->name) > 0) {
1412  BLI_insertlinkbefore(lb, idtest, id);
1413  break;
1414  }
1415  }
1416  if (item_array_index == ID_SORT_STEP_SIZE) {
1417  if (idtest == NULL) {
1418  /* If idtest is NULL here, it means that in the first loop, the last comparison was
1419  * performed exactly on the first item of the list, and that it also failed. In other
1420  * words, all items in the list are greater than inserted one, so we can put it at the
1421  * start of the list. */
1422  /* Note that BLI_insertlinkafter() would have same behavior in that case, but better be
1423  * explicit here. */
1424  BLI_addhead(lb, id);
1425  }
1426  else {
1427  BLI_insertlinkafter(lb, idtest, id);
1428  }
1429  }
1430 
1431 #undef ID_SORT_STEP_SIZE
1432 }
1433 
1434 /* Note: this code assumes and ensures that the suffix number can never go beyond 1 billion. */
1435 #define MAX_NUMBER 1000000000
1436 /* We do not want to get "name.000", so minimal number is 1. */
1437 #define MIN_NUMBER 1
1438 /* The maximum value up to which we search for the actual smallest unused number. Beyond that
1439  * value, we will only use the first biggest unused number, without trying to 'fill the gaps'
1440  * in-between already used numbers... */
1441 #define MAX_NUMBERS_IN_USE 1024
1442 
1451 static bool id_name_final_build(char *name, char *base_name, size_t base_name_len, int number)
1452 {
1453  char number_str[11]; /* Dot + nine digits + NULL terminator. */
1454  size_t number_str_len = BLI_snprintf_rlen(number_str, ARRAY_SIZE(number_str), ".%.3d", number);
1455 
1456  /* If the number would lead to an overflow of the maximum ID name length, we need to truncate
1457  * the base name part and do all the number checks again. */
1458  if (base_name_len + number_str_len >= MAX_ID_NAME - 2 || number >= MAX_NUMBER) {
1459  if (base_name_len + number_str_len >= MAX_ID_NAME - 2) {
1460  base_name_len = MAX_ID_NAME - 2 - number_str_len - 1;
1461  }
1462  else {
1463  base_name_len--;
1464  }
1465  base_name[base_name_len] = '\0';
1466 
1467  /* Code above may have generated invalid utf-8 string, due to raw truncation.
1468  * Ensure we get a valid one now. */
1469  base_name_len -= (size_t)BLI_utf8_invalid_strip(base_name, base_name_len);
1470 
1471  /* Also truncate orig name, and start the whole check again. */
1472  name[base_name_len] = '\0';
1473  return false;
1474  }
1475 
1476  /* We have our final number, we can put it in name and exit the function. */
1477  BLI_strncpy(name + base_name_len, number_str, number_str_len + 1);
1478  return true;
1479 }
1480 
1489 static bool check_for_dupid(ListBase *lb, ID *id, char *name, ID **r_id_sorting_hint)
1490 {
1491  BLI_assert(strlen(name) < MAX_ID_NAME - 2);
1492 
1493  *r_id_sorting_hint = NULL;
1494 
1495  ID *id_test = lb->first;
1496  bool is_name_changed = false;
1497 
1498  if (id_test == NULL) {
1499  return is_name_changed;
1500  }
1501 
1502  const short id_type = (short)GS(id_test->name);
1503 
1504  /* Static storage of previous handled ID/name info, used to perform a quicker test and optimize
1505  * creation of huge number of IDs using the same given base name. */
1506  static char prev_orig_base_name[MAX_ID_NAME - 2] = {0};
1507  static char prev_final_base_name[MAX_ID_NAME - 2] = {0};
1508  static short prev_id_type = ID_LINK_PLACEHOLDER; /* Should never exist in actual ID list. */
1509  static int prev_number = MIN_NUMBER - 1;
1510 
1511  /* Initial test to check whether we can 'shortcut' the more complex loop of the main code
1512  * below. Note that we do not do that for low numbers, as that would prevent using actual
1513  * smallest available number in some cases, and benefits of this special case handling mostly
1514  * show up with high numbers anyway. */
1515  if (id_type == prev_id_type && prev_number >= MAX_NUMBERS_IN_USE &&
1516  prev_number < MAX_NUMBER - 1 && name[0] == prev_final_base_name[0]) {
1517 
1518  /* Get the name and number parts ("name.number"). */
1519  char base_name[MAX_ID_NAME - 2];
1520  int number = MIN_NUMBER;
1521  size_t base_name_len = BLI_split_name_num(base_name, &number, name, '.');
1522  size_t prev_final_base_name_len = strlen(prev_final_base_name);
1523  size_t prev_orig_base_name_len = strlen(prev_orig_base_name);
1524 
1525  if (base_name_len == prev_orig_base_name_len &&
1526  STREQLEN(base_name, prev_orig_base_name, prev_orig_base_name_len)) {
1527  /* Once we have ensured given base_name and original previous one are the same, we can
1528  * check that previously used number is actually used, and that next one is free. */
1529  /* Note that from now on, we only used previous final base name, as it might have been
1530  * truncated from original one due to number suffix length. */
1531  char final_name[MAX_ID_NAME - 2];
1532  char prev_final_name[MAX_ID_NAME - 2];
1533  BLI_strncpy(final_name, prev_final_base_name, prev_final_base_name_len + 1);
1534  BLI_strncpy(prev_final_name, prev_final_base_name, prev_final_base_name_len + 1);
1535 
1536  if (id_name_final_build(final_name, base_name, prev_final_base_name_len, prev_number + 1) &&
1537  id_name_final_build(prev_final_name, base_name, prev_final_base_name_len, prev_number)) {
1538  /* We successfully built valid final names of previous and current iterations,
1539  * now we have to ensure that previous final name is indeed used in current ID list,
1540  * and that current one is not. */
1541  bool is_valid = false;
1542  for (id_test = lb->first; id_test; id_test = id_test->next) {
1543  if (id != id_test && !ID_IS_LINKED(id_test)) {
1544  if (id_test->name[2] == final_name[0] && STREQ(final_name, id_test->name + 2)) {
1545  /* We expect final_name to not be already used, so this is a failure. */
1546  is_valid = false;
1547  break;
1548  }
1549  /* Previous final name should only be found once in the list, so if it was found
1550  * already, no need to do a string comparison again. */
1551  if (!is_valid && id_test->name[2] == prev_final_name[0] &&
1552  STREQ(prev_final_name, id_test->name + 2)) {
1553  is_valid = true;
1554  *r_id_sorting_hint = id_test;
1555  }
1556  }
1557  }
1558 
1559  if (is_valid) {
1560  /* Only the number changed, prev_orig_base_name, prev_final_base_name and prev_id_type
1561  * remain the same. */
1562  prev_number++;
1563 
1564  strcpy(name, final_name);
1565  return true;
1566  }
1567  }
1568  }
1569  }
1570 
1571  /* To speed up finding smallest unused number within [0 .. MAX_NUMBERS_IN_USE - 1].
1572  * We do not bother beyond that point. */
1573  ID *ids_in_use[MAX_NUMBERS_IN_USE] = {NULL};
1574 
1575  bool is_first_run = true;
1576  while (true) {
1577  /* Get the name and number parts ("name.number"). */
1578  char base_name[MAX_ID_NAME - 2];
1579  int number = MIN_NUMBER;
1580  size_t base_name_len = BLI_split_name_num(base_name, &number, name, '.');
1581 
1582  /* Store previous original given base name now, as we might alter it later in code below. */
1583  if (is_first_run) {
1584  strcpy(prev_orig_base_name, base_name);
1585  is_first_run = false;
1586  }
1587 
1588  /* In case we get an insane initial number suffix in given name. */
1589  /* Note: BLI_split_name_num() cannot return negative numbers, so we do not have to check for
1590  * that here. */
1591  if (number >= MAX_NUMBER || number < MIN_NUMBER) {
1592  number = MIN_NUMBER;
1593  }
1594 
1595  bool is_orig_name_used = false;
1596  for (id_test = lb->first; id_test; id_test = id_test->next) {
1597  char base_name_test[MAX_ID_NAME - 2];
1598  int number_test;
1599  if ((id != id_test) && !ID_IS_LINKED(id_test) && (name[0] == id_test->name[2]) &&
1600  (ELEM(id_test->name[base_name_len + 2], '.', '\0')) &&
1601  STREQLEN(name, id_test->name + 2, base_name_len) &&
1602  (BLI_split_name_num(base_name_test, &number_test, id_test->name + 2, '.') ==
1603  base_name_len)) {
1604  /* If we did not yet encounter exact same name as the given one, check the remaining
1605  * parts of the strings. */
1606  if (!is_orig_name_used) {
1607  is_orig_name_used = STREQ(name + base_name_len, id_test->name + 2 + base_name_len);
1608  }
1609  /* Mark number of current id_test name as used, if possible. */
1610  if (number_test < MAX_NUMBERS_IN_USE) {
1611  ids_in_use[number_test] = id_test;
1612  }
1613  /* Keep track of first largest unused number. */
1614  if (number <= number_test) {
1615  *r_id_sorting_hint = id_test;
1616  number = number_test + 1;
1617  }
1618  }
1619  }
1620 
1621  /* If there is no double, we are done.
1622  * Note however that name might have been changed (truncated) in a previous iteration
1623  * already.
1624  */
1625  if (!is_orig_name_used) {
1626  /* Don't bother updating prev_ static variables here, this case is not supposed to happen
1627  * that often, and is not straight-forward here, so just ignore and reset them to default.
1628  */
1629  prev_id_type = ID_LINK_PLACEHOLDER;
1630  prev_final_base_name[0] = '\0';
1631  prev_number = MIN_NUMBER - 1;
1632 
1633  /* Value set previously is meaningless in that case. */
1634  *r_id_sorting_hint = NULL;
1635 
1636  return is_name_changed;
1637  }
1638 
1639  /* Decide which value of number to use, either the smallest unused one if possible, or
1640  * default to the first largest unused one we got from previous loop. */
1641  for (int i = MIN_NUMBER; i < MAX_NUMBERS_IN_USE; i++) {
1642  if (ids_in_use[i] == NULL) {
1643  number = i;
1644  if (i > 0) {
1645  *r_id_sorting_hint = ids_in_use[i - 1];
1646  }
1647  break;
1648  }
1649  }
1650  /* At this point, number is either the lowest unused number within
1651  * [MIN_NUMBER .. MAX_NUMBERS_IN_USE - 1], or 1 greater than the largest used number if all
1652  * those low ones are taken.
1653  * We can't be bothered to look for the lowest unused number beyond
1654  * (MAX_NUMBERS_IN_USE - 1).
1655  */
1656  /* We know for wure that name will be changed. */
1657  is_name_changed = true;
1658 
1659  /* If id_name_final_build helper returns false, it had to truncate further given name, hence
1660  * we have to go over the whole check again. */
1661  if (!id_name_final_build(name, base_name, base_name_len, number)) {
1662  /* We have to clear our list of small used numbers before we do the whole check again. */
1663  memset(ids_in_use, 0, sizeof(ids_in_use));
1664 
1665  continue;
1666  }
1667 
1668  /* Update prev_ static variables, in case next call is for the same type of IDs and with the
1669  * same initial base name, we can skip a lot of above process. */
1670  prev_id_type = id_type;
1671  strcpy(prev_final_base_name, base_name);
1672  prev_number = number;
1673 
1674  return is_name_changed;
1675  }
1676 
1677 #undef MAX_NUMBERS_IN_USE
1678 }
1679 
1680 #undef MIN_NUMBER
1681 #undef MAX_NUMBER
1682 
1690 bool BKE_id_new_name_validate(ListBase *lb, ID *id, const char *tname)
1691 {
1692  bool result;
1693  char name[MAX_ID_NAME - 2];
1694 
1695  /* if library, don't rename */
1696  if (ID_IS_LINKED(id)) {
1697  return false;
1698  }
1699 
1700  /* if no name given, use name of current ID
1701  * else make a copy (tname args can be const) */
1702  if (tname == NULL) {
1703  tname = id->name + 2;
1704  }
1705 
1706  BLI_strncpy(name, tname, sizeof(name));
1707 
1708  if (name[0] == '\0') {
1709  /* Disallow empty names. */
1710  BLI_strncpy(name, DATA_(BKE_idtype_idcode_to_name(GS(id->name))), sizeof(name));
1711  }
1712  else {
1713  /* disallow non utf8 chars,
1714  * the interface checks for this but new ID's based on file names don't */
1715  BLI_utf8_invalid_strip(name, strlen(name));
1716  }
1717 
1718  ID *id_sorting_hint = NULL;
1719  result = check_for_dupid(lb, id, name, &id_sorting_hint);
1720  strcpy(id->name + 2, name);
1721 
1722  /* This was in 2.43 and previous releases
1723  * however all data in blender should be sorted, not just duplicate names
1724  * sorting should not hurt, but noting just in case it alters the way other
1725  * functions work, so sort every time. */
1726 #if 0
1727  if (result) {
1728  id_sort_by_name(lb, id, id_sorting_hint);
1729  }
1730 #endif
1731 
1732  id_sort_by_name(lb, id, id_sorting_hint);
1733 
1734  return result;
1735 }
1736 
1737 /* next to indirect usage in read/writefile also in editobject.c scene.c */
1739 {
1740  ID *id;
1741 
1742  FOREACH_MAIN_ID_BEGIN (bmain, id) {
1743  id->newid = NULL;
1744  id->tag &= ~LIB_TAG_NEW;
1745  }
1747 }
1748 
1750 {
1751  ID **id_pointer = cb_data->id_pointer;
1752  const int cb_flag = cb_data->cb_flag;
1753  const bool do_linked_only = (bool)POINTER_AS_INT(cb_data->user_data);
1754 
1755  if (*id_pointer == NULL) {
1756  return IDWALK_RET_NOP;
1757  }
1758  if (do_linked_only && !ID_IS_LINKED(*id_pointer)) {
1759  return IDWALK_RET_NOP;
1760  }
1761 
1762  if (cb_flag & IDWALK_CB_USER) {
1763  /* Do not touch to direct/indirect linked status here... */
1764  id_us_plus_no_lib(*id_pointer);
1765  }
1766  if (cb_flag & IDWALK_CB_USER_ONE) {
1767  id_us_ensure_real(*id_pointer);
1768  }
1769 
1770  return IDWALK_RET_NOP;
1771 }
1772 
1773 void BKE_main_id_refcount_recompute(struct Main *bmain, const bool do_linked_only)
1774 {
1775  ID *id;
1776 
1777  FOREACH_MAIN_ID_BEGIN (bmain, id) {
1778  if (!ID_IS_LINKED(id) && do_linked_only) {
1779  continue;
1780  }
1781  id->us = ID_FAKE_USERS(id);
1782  /* Note that we keep EXTRAUSER tag here, since some UI users may define it too... */
1783  if (id->tag & LIB_TAG_EXTRAUSER) {
1784  id->tag &= ~(LIB_TAG_EXTRAUSER | LIB_TAG_EXTRAUSER_SET);
1785  id_us_ensure_real(id);
1786  }
1787  }
1789 
1790  /* Go over whole Main database to re-generate proper usercounts... */
1791  FOREACH_MAIN_ID_BEGIN (bmain, id) {
1793  id,
1795  POINTER_FROM_INT((int)do_linked_only),
1797  }
1799 }
1800 
1802  GSet *loop_tags,
1803  MainIDRelations *id_relations,
1804  GSet *done_ids)
1805 {
1806  if (BLI_gset_haskey(done_ids, id)) {
1807  return; /* Already checked, nothing else to do. */
1808  }
1809 
1811  BLI_gset_insert(loop_tags, id);
1812  for (MainIDRelationsEntryItem *from_id_entry = entry->from_ids; from_id_entry != NULL;
1813  from_id_entry = from_id_entry->next) {
1814  /* Our oh-so-beloved 'from' pointers... Those should always be ignored here, since the actual
1815  * relation we want to check is in the other way around. */
1816  if (from_id_entry->usage_flag & IDWALK_CB_LOOPBACK) {
1817  continue;
1818  }
1819 
1820  ID *from_id = from_id_entry->id_pointer.from;
1821 
1822  /* Shape-keys are considered 'private' to their owner ID here, and never tagged
1823  * (since they cannot be linked), so we have to switch effective parent to their owner.
1824  */
1825  if (GS(from_id->name) == ID_KE) {
1826  from_id = ((Key *)from_id)->from;
1827  }
1828 
1829  if (from_id->lib == NULL) {
1830  /* Local user, early out to avoid some gset querying... */
1831  continue;
1832  }
1833  if (!BLI_gset_haskey(done_ids, from_id)) {
1834  if (BLI_gset_haskey(loop_tags, from_id)) {
1835  /* We are in a 'dependency loop' of IDs, this does not say us anything, skip it.
1836  * Note that this is the situation that can lead to archipelagoes of linked data-blocks
1837  * (since all of them have non-local users, they would all be duplicated,
1838  * leading to a loop of unused linked data-blocks that cannot be freed since they all use
1839  * each other...). */
1840  continue;
1841  }
1842  /* Else, recursively check that user ID. */
1843  library_make_local_copying_check(from_id, loop_tags, id_relations, done_ids);
1844  }
1845 
1846  if (from_id->tag & LIB_TAG_DOIT) {
1847  /* This user will be fully local in future, so far so good,
1848  * nothing to do here but check next user. */
1849  }
1850  else {
1851  /* This user won't be fully local in future, so current ID won't be either.
1852  * And we are done checking it. */
1853  id->tag &= ~LIB_TAG_DOIT;
1854  break;
1855  }
1856  }
1857  BLI_gset_add(done_ids, id);
1858  BLI_gset_remove(loop_tags, id, NULL);
1859 }
1860 
1871 /* Note: Old (2.77) version was simply making (tagging) data-blocks as local,
1872  * without actually making any check whether they were also indirectly used or not...
1873  *
1874  * Current version uses regular id_make_local callback, with advanced pre-processing step to
1875  * detect all cases of IDs currently indirectly used, but which will be used by local data only
1876  * once this function is finished. This allows to avoid any unneeded duplication of IDs, and
1877  * hence all time lost afterwards to remove orphaned linked data-blocks...
1878  */
1880  const Library *lib,
1881  GHash *old_to_new_ids,
1882  const bool untagged_only,
1883  const bool set_fake)
1884 {
1885  ListBase *lbarray[INDEX_ID_MAX];
1886 
1887  LinkNode *todo_ids = NULL;
1888  LinkNode *copied_ids = NULL;
1889  MemArena *linklist_mem = BLI_memarena_new(512 * sizeof(*todo_ids), __func__);
1890 
1891  GSet *done_ids = BLI_gset_ptr_new(__func__);
1892 
1893 #ifdef DEBUG_TIME
1894  TIMEIT_START(make_local);
1895 #endif
1896 
1897  BKE_main_relations_create(bmain, 0);
1898 
1899 #ifdef DEBUG_TIME
1900  printf("Pre-compute current ID relations: Done.\n");
1901  TIMEIT_VALUE_PRINT(make_local);
1902 #endif
1903 
1904  /* Step 1: Detect data-blocks to make local. */
1905  for (int a = set_listbasepointers(bmain, lbarray); a--;) {
1906  ID *id = lbarray[a]->first;
1907 
1908  /* Do not explicitly make local non-linkable IDs (shapekeys, in fact),
1909  * they are assumed to be handled by real data-blocks responsible of them. */
1910  const bool do_skip = (id && !BKE_idtype_idcode_is_linkable(GS(id->name)));
1911 
1912  for (; id; id = id->next) {
1913  ID *ntree = (ID *)ntreeFromID(id);
1914 
1915  id->tag &= ~LIB_TAG_DOIT;
1916  if (ntree != NULL) {
1917  ntree->tag &= ~LIB_TAG_DOIT;
1918  }
1919 
1920  if (!ID_IS_LINKED(id)) {
1921  id->tag &= ~(LIB_TAG_EXTERN | LIB_TAG_INDIRECT | LIB_TAG_NEW);
1922  id->flag &= ~LIB_INDIRECT_WEAK_LINK;
1923  if (ID_IS_OVERRIDE_LIBRARY_REAL(id) &&
1925  ((untagged_only == false) || !(id->tag & LIB_TAG_PRE_EXISTING))) {
1927  }
1928  }
1929  /* The check on the fourth line (LIB_TAG_PRE_EXISTING) is done so it's possible to tag data
1930  * you don't want to be made local, used for appending data,
1931  * so any libdata already linked wont become local (very nasty
1932  * to discover all your links are lost after appending).
1933  * Also, never ever make proxified objects local, would not make any sense. */
1934  /* Some more notes:
1935  * - Shapekeys are never tagged here (since they are not linkable).
1936  * - Nodetrees used in materials etc. have to be tagged manually,
1937  * since they do not exist in Main (!).
1938  * This is ok-ish on 'make local' side of things
1939  * (since those are handled by their 'owner' IDs),
1940  * but complicates slightly the pre-processing of relations between IDs at step 2... */
1941  else if (!do_skip && id->tag & (LIB_TAG_EXTERN | LIB_TAG_INDIRECT | LIB_TAG_NEW) &&
1942  ELEM(lib, NULL, id->lib) &&
1943  !(GS(id->name) == ID_OB && ((Object *)id)->proxy_from != NULL) &&
1944  ((untagged_only == false) || !(id->tag & LIB_TAG_PRE_EXISTING))) {
1945  BLI_linklist_prepend_arena(&todo_ids, id, linklist_mem);
1946  id->tag |= LIB_TAG_DOIT;
1947 
1948  /* Tag those nasty non-ID nodetrees,
1949  * but do not add them to todo list, making them local is handled by 'owner' ID.
1950  * This is needed for library_make_local_copying_check() to work OK at step 2. */
1951  if (ntree != NULL) {
1952  ntree->tag |= LIB_TAG_DOIT;
1953  }
1954  }
1955  else {
1956  /* Linked ID that we won't be making local (needed info for step 2, see below). */
1957  BLI_gset_add(done_ids, id);
1958  }
1959  }
1960  }
1961 
1962 #ifdef DEBUG_TIME
1963  printf("Step 1: Detect data-blocks to make local: Done.\n");
1964  TIMEIT_VALUE_PRINT(make_local);
1965 #endif
1966 
1967  /* Step 2: Check which data-blocks we can directly make local
1968  * (because they are only used by already, or future, local data),
1969  * others will need to be duplicated. */
1970  GSet *loop_tags = BLI_gset_ptr_new(__func__);
1971  for (LinkNode *it = todo_ids; it; it = it->next) {
1972  library_make_local_copying_check(it->link, loop_tags, bmain->relations, done_ids);
1973  BLI_assert(BLI_gset_len(loop_tags) == 0);
1974  }
1975  BLI_gset_free(loop_tags, NULL);
1976  BLI_gset_free(done_ids, NULL);
1977 
1978  /* Next step will most likely add new IDs, better to get rid of this mapping now. */
1979  BKE_main_relations_free(bmain);
1980 
1981 #ifdef DEBUG_TIME
1982  printf("Step 2: Check which data-blocks we can directly make local: Done.\n");
1983  TIMEIT_VALUE_PRINT(make_local);
1984 #endif
1985 
1986  /* Step 3: Make IDs local, either directly (quick and simple), or using generic process,
1987  * which involves more complex checks and might instead
1988  * create a local copy of original linked ID. */
1989  for (LinkNode *it = todo_ids, *it_next; it; it = it_next) {
1990  it_next = it->next;
1991  ID *id = it->link;
1992 
1993  if (id->tag & LIB_TAG_DOIT) {
1994  /* We know all users of this object are local or will be made fully local, even if
1995  * currently there are some indirect usages. So instead of making a copy that we'll likely
1996  * get rid of later, directly make that data block local.
1997  * Saves a tremendous amount of time with complex scenes... */
1998  lib_id_clear_library_data_ex(bmain, id);
1999  BKE_lib_id_expand_local(bmain, id);
2000  id->tag &= ~LIB_TAG_DOIT;
2001 
2002  if (GS(id->name) == ID_OB) {
2004  }
2005  }
2006  else {
2007  /* In this specific case, we do want to make ID local even if it has no local usage yet...
2008  * Note that for objects, we don't want proxy pointers to be cleared yet. This will happen
2009  * down the road in this function.
2010  */
2011  BKE_lib_id_make_local(bmain,
2012  id,
2013  false,
2016 
2017  if (id->newid) {
2018  if (GS(id->newid->name) == ID_OB) {
2020  }
2021 
2022  /* Reuse already allocated LinkNode (transferring it from todo_ids to copied_ids). */
2023  BLI_linklist_prepend_nlink(&copied_ids, id, it);
2024  }
2025  }
2026 
2027  if (set_fake) {
2028  if (!ELEM(GS(id->name), ID_OB, ID_GR)) {
2029  /* do not set fake user on objects, groups (instancing) */
2030  id_fake_user_set(id);
2031  }
2032  }
2033  }
2034 
2035 #ifdef DEBUG_TIME
2036  printf("Step 3: Make IDs local: Done.\n");
2037  TIMEIT_VALUE_PRINT(make_local);
2038 #endif
2039 
2040  /* At this point, we are done with directly made local IDs.
2041  * Now we have to handle duplicated ones, since their
2042  * remaining linked original counterpart may not be needed anymore... */
2043  todo_ids = NULL;
2044 
2045  /* Step 4: We have to remap local usages of old (linked) ID to new (local)
2046  * ID in a separated loop,
2047  * as lbarray ordering is not enough to ensure us we did catch all dependencies
2048  * (e.g. if making local a parent object before its child...). See T48907. */
2049  /* TODO This is now the biggest step by far (in term of processing time).
2050  * We may be able to gain here by using again main->relations mapping, but...
2051  * this implies BKE_libblock_remap & co to be able to update main->relations on the fly.
2052  * Have to think about it a bit more, and see whether new code is OK first, anyway. */
2053  for (LinkNode *it = copied_ids; it; it = it->next) {
2054  ID *id = it->link;
2055 
2056  BLI_assert(id->newid != NULL);
2057  BLI_assert(id->lib != NULL);
2058 
2060  if (old_to_new_ids) {
2061  BLI_ghash_insert(old_to_new_ids, id, id->newid);
2062  }
2063 
2064  /* Special hack for groups... Thing is, since we can't instantiate them here, we need to
2065  * ensure they remain 'alive' (only instantiation is a real group 'user'... *sigh* See
2066  * T49722. */
2067  if (GS(id->name) == ID_GR && (id->tag & LIB_TAG_INDIRECT) != 0) {
2069  }
2070  }
2071 
2072 #ifdef DEBUG_TIME
2073  printf("Step 4: Remap local usages of old (linked) ID to new (local) ID: Done.\n");
2074  TIMEIT_VALUE_PRINT(make_local);
2075 #endif
2076 
2077  /* Step 5: proxy 'remapping' hack. */
2078  for (LinkNode *it = copied_ids; it; it = it->next) {
2079  ID *id = it->link;
2080 
2081  /* Attempt to re-link copied proxy objects. This allows appending of an entire scene
2082  * from another blend file into this one, even when that blend file contains proxified
2083  * armatures that have local references. Since the proxified object needs to be linked
2084  * (not local), this will only work when the "Localize all" checkbox is disabled.
2085  * TL;DR: this is a dirty hack on top of an already weak feature (proxies). */
2086  if (GS(id->name) == ID_OB && ((Object *)id)->proxy != NULL) {
2087  Object *ob = (Object *)id;
2088  Object *ob_new = (Object *)id->newid;
2089  bool is_local = false, is_lib = false;
2090 
2091  /* Proxies only work when the proxified object is linked-in from a library. */
2092  if (ob->proxy->id.lib == NULL) {
2093  CLOG_WARN(&LOG,
2094  "proxy object %s will lose its link to %s, because the "
2095  "proxified object is local.",
2096  id->newid->name,
2097  ob->proxy->id.name);
2098  continue;
2099  }
2100 
2101  BKE_library_ID_test_usages(bmain, id, &is_local, &is_lib);
2102 
2103  /* We can only switch the proxy'ing to a made-local proxy if it is no longer
2104  * referred to from a library. Not checking for local use; if new local proxy
2105  * was not used locally would be a nasty bug! */
2106  if (is_local || is_lib) {
2107  CLOG_WARN(&LOG,
2108  "made-local proxy object %s will lose its link to %s, "
2109  "because the linked-in proxy is referenced (is_local=%i, is_lib=%i).",
2110  id->newid->name,
2111  ob->proxy->id.name,
2112  is_local,
2113  is_lib);
2114  }
2115  else {
2116  /* we can switch the proxy'ing from the linked-in to the made-local proxy.
2117  * BKE_object_make_proxy() shouldn't be used here, as it allocates memory that
2118  * was already allocated by object_make_local() (which called BKE_object_copy). */
2119  ob_new->proxy = ob->proxy;
2120  ob_new->proxy_group = ob->proxy_group;
2121  ob_new->proxy_from = ob->proxy_from;
2122  ob_new->proxy->proxy_from = ob_new;
2123  ob->proxy = ob->proxy_from = ob->proxy_group = NULL;
2124  }
2125  }
2126  }
2127 
2128 #ifdef DEBUG_TIME
2129  printf("Step 5: Proxy 'remapping' hack: Done.\n");
2130  TIMEIT_VALUE_PRINT(make_local);
2131 #endif
2132 
2133  /* This is probably more of a hack than something we should do here, but...
2134  * Issue is, the whole copying + remapping done in complex cases above may leave pose-channels
2135  * of armatures in complete invalid state (more precisely, the bone pointers of the
2136  * pose-channels - very crappy cross-data-blocks relationship), se we tag it to be fully
2137  * recomputed, but this does not seems to be enough in some cases, and evaluation code ends up
2138  * trying to evaluate a not-yet-updated armature object's deformations.
2139  * Try "make all local" in 04_01_H.lighting.blend from Agent327 without this, e.g. */
2140  for (Object *ob = bmain->objects.first; ob; ob = ob->id.next) {
2141  if (ob->data != NULL && ob->type == OB_ARMATURE && ob->pose != NULL &&
2142  ob->pose->flag & POSE_RECALC) {
2143  BKE_pose_rebuild(bmain, ob, ob->data, true);
2144  }
2145  }
2146 
2147 #ifdef DEBUG_TIME
2148  printf("Hack: Forcefully rebuild armature object poses: Done.\n");
2149  TIMEIT_VALUE_PRINT(make_local);
2150 #endif
2151 
2153  BLI_memarena_free(linklist_mem);
2154 
2155 #ifdef DEBUG_TIME
2156  printf("Cleanup and finish: Done.\n");
2157  TIMEIT_END(make_local);
2158 #endif
2159 }
2160 
2165 void BLI_libblock_ensure_unique_name(Main *bmain, const char *name)
2166 {
2167  ListBase *lb;
2168  ID *idtest;
2169 
2170  lb = which_libbase(bmain, GS(name));
2171  if (lb == NULL) {
2172  return;
2173  }
2174 
2175  /* search for id */
2176  idtest = BLI_findstring(lb, name + 2, offsetof(ID, name) + 2);
2177  if (idtest != NULL) {
2178  /* BKE_id_new_name_validate also takes care of sorting. */
2179  BKE_id_new_name_validate(lb, idtest, NULL);
2180  bmain->is_memfile_undo_written = false;
2181  }
2182 }
2183 
2187 void BKE_libblock_rename(Main *bmain, ID *id, const char *name)
2188 {
2189  ListBase *lb = which_libbase(bmain, GS(id->name));
2190  if (BKE_id_new_name_validate(lb, id, name)) {
2191  bmain->is_memfile_undo_written = false;
2192  }
2193 }
2194 
2205 void BKE_id_full_name_get(char name[MAX_ID_FULL_NAME], const ID *id, char separator_char)
2206 {
2207  strcpy(name, id->name + 2);
2208 
2209  if (id->lib != NULL) {
2210  const size_t idname_len = strlen(id->name + 2);
2211  const size_t libname_len = strlen(id->lib->id.name + 2);
2212 
2213  name[idname_len] = separator_char ? separator_char : ' ';
2214  name[idname_len + 1] = '[';
2215  strcpy(name + idname_len + 2, id->lib->id.name + 2);
2216  name[idname_len + 2 + libname_len] = ']';
2217  name[idname_len + 2 + libname_len + 1] = '\0';
2218  }
2219 }
2220 
2235  const ID *id,
2236  const bool add_lib_hint,
2237  char separator_char,
2238  int *r_prefix_len)
2239 {
2240  int i = 0;
2241 
2242  if (add_lib_hint) {
2243  name[i++] = id->lib ? (ID_MISSING(id) ? 'M' : 'L') : ID_IS_OVERRIDE_LIBRARY(id) ? 'O' : ' ';
2244  }
2245  name[i++] = (id->flag & LIB_FAKEUSER) ? 'F' : ((id->us == 0) ? '0' : ' ');
2246  name[i++] = ' ';
2247 
2248  BKE_id_full_name_get(name + i, id, separator_char);
2249 
2250  if (r_prefix_len) {
2251  *r_prefix_len = i;
2252  }
2253 }
2254 
2260 char *BKE_id_to_unique_string_key(const struct ID *id)
2261 {
2262  if (id->lib == NULL) {
2263  return BLI_strdup(id->name);
2264  }
2265 
2266  /* Prefix with an ascii character in the range of 32..96 (visible)
2267  * this ensures we can't have a library ID pair that collide.
2268  * Where 'LIfooOBbarOBbaz' could be ('LIfoo, OBbarOBbaz') or ('LIfooOBbar', 'OBbaz'). */
2269  const char ascii_len = strlen(id->lib->id.name + 2) + 32;
2270  return BLI_sprintfN("%c%s%s", ascii_len, id->lib->id.name, id->name);
2271 }
2272 
2273 void BKE_id_tag_set_atomic(ID *id, int tag)
2274 {
2276 }
2277 
2278 void BKE_id_tag_clear_atomic(ID *id, int tag)
2279 {
2281 }
2282 
2288 {
2289  /* We do not want to fail when id is NULL here, even though this is a bit strange behavior...
2290  */
2291  return (id == NULL || BLI_findindex(which_libbase(G_MAIN, GS(id->name)), id) != -1);
2292 }
2293 
2294 bool BKE_id_can_be_asset(const ID *id)
2295 {
2296  return !ID_IS_LINKED(id) && !ID_IS_OVERRIDE_LIBRARY(id) &&
2298 }
2299 
2300 /************************* Datablock order in UI **************************/
2301 
2302 static int *id_order_get(ID *id)
2303 {
2304  /* Only for workspace tabs currently. */
2305  switch (GS(id->name)) {
2306  case ID_WS:
2307  return &((WorkSpace *)id)->order;
2308  default:
2309  return NULL;
2310  }
2311 }
2312 
2313 static int id_order_compare(const void *a, const void *b)
2314 {
2315  ID *id_a = ((LinkData *)a)->data;
2316  ID *id_b = ((LinkData *)b)->data;
2317 
2318  int *order_a = id_order_get(id_a);
2319  int *order_b = id_order_get(id_b);
2320 
2321  if (order_a && order_b) {
2322  if (*order_a < *order_b) {
2323  return -1;
2324  }
2325  if (*order_a > *order_b) {
2326  return 1;
2327  }
2328  }
2329 
2330  return strcmp(id_a->name, id_b->name);
2331 }
2332 
2337 void BKE_id_ordered_list(ListBase *ordered_lb, const ListBase *lb)
2338 {
2339  BLI_listbase_clear(ordered_lb);
2340 
2341  LISTBASE_FOREACH (ID *, id, lb) {
2342  BLI_addtail(ordered_lb, BLI_genericNodeN(id));
2343  }
2344 
2345  BLI_listbase_sort(ordered_lb, id_order_compare);
2346 
2347  int num = 0;
2348  LISTBASE_FOREACH (LinkData *, link, ordered_lb) {
2349  int *order = id_order_get(link->data);
2350  if (order) {
2351  *order = num++;
2352  }
2353  }
2354 }
2355 
2359 void BKE_id_reorder(const ListBase *lb, ID *id, ID *relative, bool after)
2360 {
2361  int *id_order = id_order_get(id);
2362  int relative_order;
2363 
2364  if (relative) {
2365  relative_order = *id_order_get(relative);
2366  }
2367  else {
2368  relative_order = (after) ? BLI_listbase_count(lb) : 0;
2369  }
2370 
2371  if (after) {
2372  /* Insert after. */
2373  LISTBASE_FOREACH (ID *, other, lb) {
2374  int *order = id_order_get(other);
2375  if (*order > relative_order) {
2376  (*order)++;
2377  }
2378  }
2379 
2380  *id_order = relative_order + 1;
2381  }
2382  else {
2383  /* Insert before. */
2384  LISTBASE_FOREACH (ID *, other, lb) {
2385  int *order = id_order_get(other);
2386  if (*order < relative_order) {
2387  (*order)--;
2388  }
2389  }
2390 
2391  *id_order = relative_order - 1;
2392  }
2393 }
2394 
2396 {
2397  if (id->asset_data) {
2399  }
2400 
2401  /* ID_WM's id->properties are considered runtime only, and never written in .blend file. */
2402  if (id->properties && !ELEM(GS(id->name), ID_WM)) {
2403  IDP_BlendWrite(writer, id->properties);
2404  }
2405 
2406  if (id->override_library) {
2408 
2411  BLO_write_string(writer, op->rna_path);
2412 
2414  LISTBASE_FOREACH (IDOverrideLibraryPropertyOperation *, opop, &op->operations) {
2415  if (opop->subitem_reference_name) {
2416  BLO_write_string(writer, opop->subitem_reference_name);
2417  }
2418  if (opop->subitem_local_name) {
2419  BLO_write_string(writer, opop->subitem_local_name);
2420  }
2421  }
2422  }
2423  }
2424 }
bool id_can_have_animdata(const struct ID *id)
struct AnimData * BKE_animdata_copy(struct Main *bmain, struct AnimData *adt, const int flag)
Definition: anim_data.c:318
void BKE_animdata_duplicate_id_action(struct Main *bmain, struct ID *id, const uint duplicate_flags)
void BKE_pose_rebuild(struct Main *bmain, struct Object *ob, struct bArmature *arm, const bool do_id_user)
Definition: armature.c:2536
void BKE_asset_metadata_write(struct BlendWriter *writer, struct AssetMetaData *asset_data)
Definition: asset.cc:123
void BKE_bpath_traverse_id(struct Main *bmain, struct ID *id, BPathVisitor visit_cb, const int flag, void *bpath_user_data)
Definition: bpath.c:540
bool BKE_bpath_relocate_visitor(void *oldbasepath, char *path_dst, const char *path_src)
Definition: bpath.c:797
@ BKE_BPATH_TRAVERSE_SKIP_MULTIFILE
Definition: BKE_bpath.h:70
struct Main * CTX_data_main(const bContext *C)
Definition: context.c:1018
#define G_MAIN
Definition: BKE_global.h:232
void IDP_BlendWrite(struct BlendWriter *writer, const struct IDProperty *prop)
struct IDProperty * IDP_CopyProperty_ex(const struct IDProperty *prop, const int flag) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
@ IDTYPE_FLAGS_NO_MAKELOCAL
Definition: BKE_idtype.h:49
@ IDTYPE_FLAGS_NO_COPY
Definition: BKE_idtype.h:45
@ IDTYPE_FLAGS_NO_LIBLINKING
Definition: BKE_idtype.h:47
bool BKE_idtype_idcode_is_linkable(const short idcode)
Definition: idtype.c:232
const struct IDTypeInfo * BKE_idtype_get_info_from_idcode(const short id_code)
Definition: idtype.c:131
const char * BKE_idtype_idcode_to_name(const short idcode)
Definition: idtype.c:168
const struct IDTypeInfo * BKE_idtype_get_info_from_id(const struct ID *id)
struct Key * BKE_key_from_id(struct ID *id)
Definition: key.c:1786
@ LIB_ID_CREATE_NO_ALLOCATE
Definition: BKE_lib_id.h:96
@ LIB_ID_COPY_NO_LIB_OVERRIDE
Definition: BKE_lib_id.h:124
@ LIB_ID_COPY_SET_COPIED_ON_WRITE
Definition: BKE_lib_id.h:109
@ LIB_ID_CREATE_LOCAL
Definition: BKE_lib_id.h:105
@ LIB_ID_COPY_ACTIONS
Definition: BKE_lib_id.h:129
@ LIB_ID_COPY_KEEP_LIB
Definition: BKE_lib_id.h:131
@ LIB_ID_COPY_LOCALIZE
Definition: BKE_lib_id.h:145
@ LIB_ID_CREATE_NO_USER_REFCOUNT
Definition: BKE_lib_id.h:92
@ LIB_ID_COPY_NO_ANIMDATA
Definition: BKE_lib_id.h:120
@ LIB_ID_COPY_SHAPEKEY
Definition: BKE_lib_id.h:133
@ LIB_ID_CREATE_NO_MAIN
Definition: BKE_lib_id.h:88
@ LIB_ID_COPY_DEFAULT
Definition: BKE_lib_id.h:139
@ LIB_ID_CREATE_NO_DEG_TAG
Definition: BKE_lib_id.h:99
#define MAX_ID_FULL_NAME_UI
Definition: BKE_lib_id.h:294
@ LIB_ID_MAKELOCAL_OBJECT_NO_PROXY_CLEARING
Definition: BKE_lib_id.h:244
@ LIB_ID_MAKELOCAL_FULL_LIBRARY
Definition: BKE_lib_id.h:240
#define MAIN_ID_SESSION_UUID_UNSET
Definition: BKE_lib_id.h:73
#define MAX_ID_FULL_NAME
Definition: BKE_lib_id.h:293
void BKE_lib_override_library_make_local(struct ID *id)
void BKE_lib_override_library_copy(struct ID *dst_id, const struct ID *src_id, const bool do_full_copy)
@ IDWALK_INCLUDE_UI
@ IDWALK_NOP
@ IDWALK_READONLY
void BKE_library_foreach_ID_link(struct Main *bmain, struct ID *id, LibraryIDLinkCallback callback, void *user_data, int flag)
Definition: lib_query.c:322
@ IDWALK_RET_STOP_ITER
Definition: BKE_lib_query.h:99
@ IDWALK_RET_NOP
Definition: BKE_lib_query.h:97
void BKE_library_ID_test_usages(struct Main *bmain, void *idv, bool *is_used_local, bool *is_used_linked)
Definition: lib_query.c:597
@ IDWALK_CB_LOOPBACK
Definition: BKE_lib_query.h:68
@ IDWALK_CB_USER_ONE
Definition: BKE_lib_query.h:93
@ IDWALK_CB_USER
Definition: BKE_lib_query.h:87
@ IDWALK_CB_EMBEDDED
Definition: BKE_lib_query.h:62
void void BKE_libblock_remap(struct Main *bmain, void *old_idv, void *new_idv, const short remap_flags) ATTR_NONNULL(1
void BKE_libblock_relink_ex(struct Main *bmain, void *idv, void *old_idv, void *new_idv, const short remap_flags) ATTR_NONNULL(1
@ ID_REMAP_SKIP_NEVER_NULL_USAGE
Definition: BKE_lib_remap.h:55
@ ID_REMAP_SKIP_INDIRECT_USAGE
Definition: BKE_lib_remap.h:46
#define FOREACH_MAIN_ID_END
Definition: BKE_main.h:250
void BKE_main_relations_create(struct Main *bmain, const short flag)
Definition: main.c:265
int set_listbasepointers(struct Main *main, struct ListBase *lb[])
void BKE_main_unlock(struct Main *bmain)
Definition: main.c:207
const char * BKE_main_blendfile_path(const struct Main *bmain) ATTR_NONNULL()
void BKE_main_lock(struct Main *bmain)
Definition: main.c:202
void BKE_main_relations_free(struct Main *bmain)
Definition: main.c:300
#define FOREACH_MAIN_ID_BEGIN(_bmain, _id)
Definition: BKE_main.h:244
struct ListBase * which_libbase(struct Main *bmain, short type)
Definition: main.c:447
struct bNodeTree * ntreeFromID(struct ID *id)
Definition: node.cc:3147
API for Blender-side Rigid Body stuff.
void BKE_rigidbody_ensure_local_object(struct Main *bmain, struct Object *ob)
Definition: rigidbody.c:2312
#define BLI_assert(a)
Definition: BLI_assert.h:58
struct GSet GSet
Definition: BLI_ghash.h:189
GSet * BLI_gset_str_new_ex(const char *info, const unsigned int nentries_reserve) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
unsigned int BLI_gset_len(GSet *gs) ATTR_WARN_UNUSED_RESULT
Definition: BLI_ghash.c:1138
GSet * BLI_gset_ptr_new(const char *info)
void BLI_gset_insert(GSet *gs, void *key)
Definition: BLI_ghash.c:1147
bool BLI_gset_haskey(GSet *gs, const void *key) ATTR_WARN_UNUSED_RESULT
Definition: BLI_ghash.c:1216
void BLI_ghash_insert(GHash *gh, void *key, void *val)
Definition: BLI_ghash.c:756
void BLI_gset_free(GSet *gs, GSetKeyFreeFP keyfreefp)
Definition: BLI_ghash.c:1253
bool BLI_gset_add(GSet *gs, void *key)
Definition: BLI_ghash.c:1160
bool BLI_gset_remove(GSet *gs, const void *key, GSetKeyFreeFP keyfreefp)
Definition: BLI_ghash.c:1211
void * BLI_ghash_lookup(GHash *gh, const void *key) ATTR_WARN_UNUSED_RESULT
Definition: BLI_ghash.c:803
void BLI_addhead(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:87
#define LISTBASE_FOREACH(type, var, list)
Definition: BLI_listbase.h:172
BLI_INLINE void BLI_listbase_clear(struct ListBase *lb)
Definition: BLI_listbase.h:128
struct LinkData * BLI_genericNodeN(void *data)
Definition: listbase.c:923
void BLI_insertlinkafter(struct ListBase *listbase, void *vprevlink, void *vnewlink) ATTR_NONNULL(1)
Definition: listbase.c:352
void * BLI_findstring(const struct ListBase *listbase, const char *id, const int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
void void BLI_listbase_sort(struct ListBase *listbase, int(*cmp)(const void *, const void *)) ATTR_NONNULL(1
void BLI_addtail(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:110
void BLI_remlink(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:133
int BLI_findindex(const struct ListBase *listbase, const void *vlink) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
void BLI_insertlinkbefore(struct ListBase *listbase, void *vnextlink, void *vnewlink) ATTR_NONNULL(1)
Definition: listbase.c:395
int BLI_listbase_count(const struct ListBase *listbase) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
void BLI_memarena_free(struct MemArena *ma) ATTR_NONNULL(1)
Definition: BLI_memarena.c:109
struct MemArena * BLI_memarena_new(const size_t bufsize, const char *name) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(2) ATTR_MALLOC
Definition: BLI_memarena.c:79
size_t size_t char * BLI_sprintfN(const char *__restrict format,...) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC ATTR_PRINTF_FORMAT(1
int BLI_strcasecmp(const char *s1, const char *s2) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: string.c:666
size_t BLI_snprintf_rlen(char *__restrict dst, size_t maxncpy, const char *__restrict format,...) ATTR_NONNULL(1
char * BLI_strdup(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL() ATTR_MALLOC
Definition: string.c:70
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, const size_t maxncpy) ATTR_NONNULL()
Definition: string.c:108
int BLI_utf8_invalid_strip(char *str, size_t length) ATTR_NONNULL()
Definition: string_utf8.c:203
size_t BLI_split_name_num(char *left, int *nr, const char *name, const char delim)
Definition: string_utils.c:55
unsigned int uint
Definition: BLI_sys_types.h:83
#define ARRAY_SIZE(arr)
#define STREQLEN(a, b, n)
#define POINTER_FROM_INT(i)
#define POINTER_AS_INT(i)
#define UNLIKELY(x)
#define ELEM(...)
#define STREQ(a, b)
#define BLO_write_struct(writer, struct_name, data_ptr)
void BLO_write_string(BlendWriter *writer, const char *data_ptr)
Definition: writefile.c:1401
#define BLO_write_struct_list(writer, struct_name, list_ptr)
#define BLT_I18NCONTEXT_ID_ID
#define DATA_(msgid)
#define CLOG_ERROR(clg_ref,...)
Definition: CLG_log.h:204
#define CLOG_WARN(clg_ref,...)
Definition: CLG_log.h:203
void DEG_id_type_tag(struct Main *bmain, short id_type)
void DEG_id_tag_update_ex(struct Main *bmain, struct ID *id, int flag)
void DEG_id_tag_update(struct ID *id, int flag)
void DEG_relations_tag_update(struct Main *bmain)
ID and Library types, which are fundamental for sdna.
#define ID_IS_OVERRIDE_LIBRARY_VIRTUAL(_id)
Definition: DNA_ID.h:442
#define ID_FAKE_USERS(id)
Definition: DNA_ID.h:412
@ ID_RECALC_TRANSFORM
Definition: DNA_ID.h:599
@ ID_RECALC_COPY_ON_WRITE
Definition: DNA_ID.h:654
@ ID_RECALC_TAG_FOR_UNDO
Definition: DNA_ID.h:682
@ ID_RECALC_ANIMATION
Definition: DNA_ID.h:614
@ ID_RECALC_GEOMETRY
Definition: DNA_ID.h:611
#define ID_IS_OVERRIDE_LIBRARY_REAL(_id)
Definition: DNA_ID.h:438
@ LIB_TAG_EXTRAUSER_SET
Definition: DNA_ID.h:547
@ LIB_TAG_NO_USER_REFCOUNT
Definition: DNA_ID.h:573
@ LIB_TAG_NOT_ALLOCATED
Definition: DNA_ID.h:576
@ LIB_TAG_INDIRECT
Definition: DNA_ID.h:524
@ LIB_TAG_EXTRAUSER
Definition: DNA_ID.h:545
@ LIB_TAG_TEMP_MAIN
Definition: DNA_ID.h:585
@ LIB_TAG_NEW
Definition: DNA_ID.h:551
@ LIB_TAG_PRE_EXISTING
Definition: DNA_ID.h:556
@ LIB_TAG_COPIED_ON_WRITE
Definition: DNA_ID.h:565
@ LIB_TAG_LOCALIZED
Definition: DNA_ID.h:568
@ LIB_TAG_DOIT
Definition: DNA_ID.h:554
@ LIB_TAG_EXTERN
Definition: DNA_ID.h:521
@ LIB_TAG_NO_MAIN
Definition: DNA_ID.h:572
#define ID_IS_LINKED(_id)
Definition: DNA_ID.h:426
#define MAX_ID_NAME
Definition: DNA_ID.h:269
struct ID ID
@ LIB_EMBEDDED_DATA
Definition: DNA_ID.h:482
@ LIB_FAKEUSER
Definition: DNA_ID.h:477
@ LIB_EMBEDDED_DATA_LIB_OVERRIDE
Definition: DNA_ID.h:493
@ LIB_INDIRECT_WEAK_LINK
Definition: DNA_ID.h:488
#define ID_IS_OVERRIDE_LIBRARY(_id)
Definition: DNA_ID.h:445
#define ID_NEW_SET(_id, _idn)
Definition: DNA_ID.h:464
#define ID_MISSING(_id)
Definition: DNA_ID.h:424
@ INDEX_ID_NULL
Definition: DNA_ID.h:858
@ INDEX_ID_MAX
Definition: DNA_ID.h:859
@ ID_WM
Definition: DNA_ID_enums.h:84
@ ID_WS
Definition: DNA_ID_enums.h:91
@ ID_KE
Definition: DNA_ID_enums.h:70
@ ID_SCE
Definition: DNA_ID_enums.h:57
@ ID_GD
Definition: DNA_ID_enums.h:83
@ ID_IP
Definition: DNA_ID_enums.h:69
@ ID_GR
Definition: DNA_ID_enums.h:77
@ ID_OB
Definition: DNA_ID_enums.h:59
#define ID_LINK_PLACEHOLDER
Definition: DNA_ID_enums.h:100
@ POSE_RECALC
Object groups, one object can be in many groups at once.
@ GP_DATA_SHOW_ONIONSKINS
@ OB_ARMATURE
eDupli_ID_Flags
@ USER_DUP_LINKED_ID
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum type
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint order
Read Guarded memory(de)allocation.
static void init_data(ModifierData *md)
Utility defines for timing/benchmarks.
#define TIMEIT_START(var)
#define TIMEIT_VALUE_PRINT(var)
#define TIMEIT_END(var)
#define C
Definition: RandGen.cpp:39
Provides wrapper around system-specific atomic primitives, and some extensions (faked-atomic operatio...
ATOMIC_INLINE int32_t atomic_fetch_and_or_int32(int32_t *p, int32_t x)
ATOMIC_INLINE uint32_t atomic_add_and_fetch_uint32(uint32_t *p, uint32_t x)
ATOMIC_INLINE int32_t atomic_fetch_and_and_int32(int32_t *p, int32_t x)
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition: btDbvt.cpp:52
bool is_valid
bNodeTree * ntree
DRWShaderLibrary * lib
#define GS(x)
Definition: iris.c:241
static uint global_session_uuid
Definition: lib_id.c:1137
void BKE_libblock_init_empty(ID *id)
Definition: lib_id.c:1122
void id_lib_extern(ID *id)
Definition: lib_id.c:207
void BKE_lib_libblock_session_uuid_renew(ID *id)
Definition: lib_id.c:1166
static int id_copy_libmanagement_cb(LibraryIDLinkCallbackData *cb_data)
Definition: lib_id.c:517
#define ID_SORT_STEP_SIZE
static int * id_order_get(ID *id)
Definition: lib_id.c:2302
void * BKE_libblock_copy(Main *bmain, const ID *id)
Definition: lib_id.c:1315
void BKE_id_tag_clear_atomic(ID *id, int tag)
Definition: lib_id.c:2278
static void lib_id_clear_library_data_ex(Main *bmain, ID *id)
Definition: lib_id.c:156
bool BKE_lib_id_make_local(Main *bmain, ID *id, const bool test, const int flags)
Definition: lib_id.c:479
void * BKE_id_new_nomain(const short type, const char *name)
Definition: lib_id.c:1196
bool id_single_user(bContext *C, ID *id, PointerRNA *ptr, PropertyRNA *prop)
Definition: lib_id.c:747
#define MIN_NUMBER
Definition: lib_id.c:1437
void BKE_main_id_tag_all(struct Main *mainvar, const int tag, const bool value)
Definition: lib_id.c:923
void BLI_libblock_ensure_unique_name(Main *bmain, const char *name)
Definition: lib_id.c:2165
void BKE_id_full_name_get(char name[MAX_ID_FULL_NAME], const ID *id, char separator_char)
Definition: lib_id.c:2205
void BKE_lib_id_clear_library_data(Main *bmain, ID *id)
Definition: lib_id.c:202
static int id_refcount_recompute_callback(LibraryIDLinkCallbackData *cb_data)
Definition: lib_id.c:1749
char * BKE_id_to_unique_string_key(const struct ID *id)
Definition: lib_id.c:2260
void BKE_libblock_copy_ex(Main *bmain, const ID *id, ID **r_newid, const int orig_flag)
Definition: lib_id.c:1212
bool BKE_id_new_name_validate(ListBase *lb, ID *id, const char *tname)
Definition: lib_id.c:1690
size_t BKE_libblock_get_alloc_info(short type, const char **name)
Definition: lib_id.c:1024
static bool id_name_final_build(char *name, char *base_name, size_t base_name_len, int number)
Definition: lib_id.c:1451
void BKE_main_id_flag_listbase(ListBase *lb, const int flag, const bool value)
Definition: lib_id.c:937
void id_us_plus(ID *id)
Definition: lib_id.c:288
void id_sort_by_name(ListBase *lb, ID *id, ID *id_sorting_hint)
Definition: lib_id.c:1348
static void lib_id_library_local_paths(Main *bmain, Library *lib, ID *id)
Definition: lib_id.c:130
void BKE_main_id_clear_newpoins(Main *bmain)
Definition: lib_id.c:1738
bool BKE_id_copy_is_allowed(const ID *id)
Definition: lib_id.c:538
void BKE_libblock_management_main_remove(Main *bmain, void *idv)
Definition: lib_id.c:844
static int libblock_management_us_plus(LibraryIDLinkCallbackData *cb_data)
Definition: lib_id.c:785
static int id_order_compare(const void *a, const void *b)
Definition: lib_id.c:2313
void BKE_id_tag_set_atomic(ID *id, int tag)
Definition: lib_id.c:2273
#define LIB_ID_TYPES_NOCOPY
void id_fake_user_set(ID *id)
Definition: lib_id.c:328
void BKE_id_reorder(const ListBase *lb, ID *id, ID *relative, bool after)
Definition: lib_id.c:2359
void BKE_id_full_name_ui_prefix_get(char name[MAX_ID_FULL_NAME_UI], const ID *id, const bool add_lib_hint, char separator_char, int *r_prefix_len)
Definition: lib_id.c:2234
void * BKE_libblock_alloc_notest(short type)
Definition: lib_id.c:1045
void BKE_lib_id_swap(Main *bmain, ID *id_a, ID *id_b)
Definition: lib_id.c:729
void BKE_main_id_flag_all(Main *bmain, const int flag, const bool value)
Definition: lib_id.c:956
void * BKE_libblock_copy_for_localize(const ID *id)
Definition: lib_id.c:1325
void BKE_lib_id_expand_local(Main *bmain, ID *id)
Definition: lib_id.c:393
static int lib_id_expand_local_cb(LibraryIDLinkCallbackData *cb_data)
Definition: lib_id.c:352
void BKE_id_clear_newpoin(ID *id)
Definition: lib_id.c:344
void * BKE_id_new(Main *bmain, const short type, const char *name)
Definition: lib_id.c:1177
void BKE_library_make_local(Main *bmain, const Library *lib, GHash *old_to_new_ids, const bool untagged_only, const bool set_fake)
Definition: lib_id.c:1879
void BKE_libblock_rename(Main *bmain, ID *id, const char *name)
Definition: lib_id.c:2187
#define MAX_NUMBERS_IN_USE
Definition: lib_id.c:1441
void id_us_ensure_real(ID *id)
Definition: lib_id.c:238
void BKE_lib_id_make_local_generic(Main *bmain, ID *id, const int flags)
Definition: lib_id.c:412
void id_fake_user_clear(ID *id)
Definition: lib_id.c:336
void id_us_clear_real(ID *id)
Definition: lib_id.c:257
void BKE_libblock_management_usercounts_set(Main *bmain, void *idv)
Definition: lib_id.c:863
static bool check_for_dupid(ListBase *lb, ID *id, char *name, ID **r_id_sorting_hint)
Definition: lib_id.c:1489
void BKE_main_lib_objects_recalc_all(Main *bmain)
Definition: lib_id.c:997
ID * BKE_id_copy(Main *bmain, const ID *id)
Definition: lib_id.c:636
void BKE_main_id_repair_duplicate_names_listbase(ListBase *lb)
Definition: lib_id.c:966
void id_us_plus_no_lib(ID *id)
Definition: lib_id.c:272
ID * BKE_libblock_find_name(struct Main *bmain, const short type, const char *name)
Definition: lib_id.c:1333
static void library_make_local_copying_check(ID *id, GSet *loop_tags, MainIDRelations *id_relations, GSet *done_ids)
Definition: lib_id.c:1801
void BKE_main_id_tag_listbase(ListBase *lb, const int tag, const bool value)
Definition: lib_id.c:891
void BKE_main_id_tag_idcode(struct Main *mainvar, const short type, const int tag, const bool value)
Definition: lib_id.c:910
void id_lib_indirect_weak_link(ID *id)
Definition: lib_id.c:220
void id_us_min(ID *id)
Definition: lib_id.c:297
#define MAX_NUMBER
Definition: lib_id.c:1435
static CLG_LogRef LOG
Definition: lib_id.c:90
IDTypeInfo IDType_ID_LINK_PLACEHOLDER
Definition: lib_id.c:93
ID * BKE_id_copy_ex(Main *bmain, const ID *id, ID **r_newid, const int flag)
Definition: lib_id.c:570
void BKE_main_id_refcount_recompute(struct Main *bmain, const bool do_linked_only)
Definition: lib_id.c:1773
static int lib_id_clear_library_data_users_update_cb(LibraryIDLinkCallbackData *cb_data)
Definition: lib_id.c:141
static int libblock_management_us_min(LibraryIDLinkCallbackData *cb_data)
Definition: lib_id.c:799
void BKE_id_ordered_list(ListBase *ordered_lb, const ListBase *lb)
Definition: lib_id.c:2337
void BKE_id_blend_write(BlendWriter *writer, ID *id)
Definition: lib_id.c:2395
void BKE_libblock_management_main_add(Main *bmain, void *idv)
Definition: lib_id.c:812
ID * BKE_id_copy_for_duplicate(Main *bmain, ID *id, const eDupli_ID_Flags duplicate_flags)
Definition: lib_id.c:645
static void id_swap(Main *bmain, ID *id_a, ID *id_b, const bool do_full_id)
Definition: lib_id.c:685
void BKE_lib_libblock_session_uuid_ensure(ID *id)
Definition: lib_id.c:1145
void BKE_libblock_management_usercounts_clear(Main *bmain, void *idv)
Definition: lib_id.c:875
bool BKE_id_can_be_asset(const ID *id)
Definition: lib_id.c:2294
static void lib_id_copy_ensure_local(Main *bmain, const ID *old_id, ID *new_id)
Definition: lib_id.c:401
bool BKE_id_is_in_global_main(ID *id)
Definition: lib_id.c:2287
void BKE_lib_id_swap_full(Main *bmain, ID *id_a, ID *id_b)
Definition: lib_id.c:741
void * BKE_libblock_alloc(Main *bmain, short type, const char *name, const int flag)
Definition: lib_id.c:1062
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:45
void *(* MEM_mallocN)(size_t len, const char *str)
Definition: mallocn.c:47
static unsigned a[3]
Definition: RandGen.cpp:92
void RNA_id_pointer_create(ID *id, PointerRNA *r_ptr)
Definition: rna_access.c:122
void RNA_property_pointer_set(PointerRNA *ptr, PropertyRNA *prop, PointerRNA ptr_value, ReportList *reports)
Definition: rna_access.c:3673
void RNA_property_update(bContext *C, PointerRNA *ptr, PropertyRNA *prop)
Definition: rna_access.c:2317
bool RNA_property_editable(PointerRNA *ptr, PropertyRNA *prop_orig)
Definition: rna_access.c:2073
const char * identifier
Definition: CLG_log.h:119
const ID * id_src
Definition: lib_id.c:511
ListBase properties
Definition: DNA_ID.h:252
struct ID * reference
Definition: DNA_ID.h:250
short id_code
Definition: BKE_idtype.h:120
const char * name
Definition: BKE_idtype.h:138
IDTypeCopyDataFunction copy_data
Definition: BKE_idtype.h:162
IDTypeInitDataFunction init_data
Definition: BKE_idtype.h:156
uint32_t flags
Definition: BKE_idtype.h:145
size_t struct_size
Definition: BKE_idtype.h:135
IDTypeMakeLocalFunction make_local
Definition: BKE_idtype.h:173
Definition: DNA_ID.h:273
int tag
Definition: DNA_ID.h:292
struct AssetMetaData * asset_data
Definition: DNA_ID.h:280
struct Library * lib
Definition: DNA_ID.h:277
int recalc
Definition: DNA_ID.h:295
int us
Definition: DNA_ID.h:293
struct ID * newid
Definition: DNA_ID.h:275
void * prev
Definition: DNA_ID.h:274
IDProperty * properties
Definition: DNA_ID.h:314
IDOverrideLibrary * override_library
Definition: DNA_ID.h:317
short flag
Definition: DNA_ID.h:288
unsigned int session_uuid
Definition: DNA_ID.h:312
void * next
Definition: DNA_ID.h:274
char name[66]
Definition: DNA_ID.h:283
AnimData * adt
ID id
Definition: DNA_key_types.h:79
ID id
Definition: DNA_ID.h:349
char filepath_abs[1024]
Definition: DNA_ID.h:362
struct LinkNode * next
Definition: BLI_linklist.h:39
void * last
Definition: DNA_listBase.h:47
void * first
Definition: DNA_listBase.h:47
struct MainIDRelationsEntryItem * from_ids
Definition: BKE_main.h:80
struct GHash * relations_from_pointers
Definition: BKE_main.h:102
Definition: BKE_main.h:116
char is_locked_for_linking
Definition: BKE_main.h:141
char is_memfile_undo_written
Definition: BKE_main.h:125
struct MainIDRelations * relations
Definition: BKE_main.h:192
ListBase objects
Definition: BKE_main.h:148
struct Object * proxy_group
struct Object * proxy_from
struct Object * proxy
PointerRNA * ptr
Definition: wm_files.c:3157