Blender V5.3
idtype.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2005 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <array>
10#include <cstring>
11
12#include "BLI_ghash.hh"
13#include "BLI_utildefines.hh"
14
15#include "BLT_translation.hh"
16
17#include "DNA_ID.h"
19#include "DNA_node_types.h"
20#include "DNA_scene_types.h"
21
22#include "BKE_node.hh"
23
24#include "BKE_idtype.hh"
25
26namespace blender {
27
28// static CLG_LogRef LOG = {"lib.idtype"};
29
31{
32 const IDCacheKey *key = static_cast<const IDCacheKey *>(key_v);
35 return uint(hash);
36}
37
38bool BKE_idtype_cache_key_cmp(const void *key_a_v, const void *key_b_v)
39{
40 const IDCacheKey *key_a = static_cast<const IDCacheKey *>(key_a_v);
41 const IDCacheKey *key_b = static_cast<const IDCacheKey *>(key_b_v);
42 return (key_a->id_session_uid != key_b->id_session_uid) ||
43 (key_a->identifier != key_b->identifier);
44}
45
46static std::array<IDTypeInfo *, INDEX_ID_MAX> id_types;
47
48#ifndef NDEBUG
50static bool id_type_is_valid(const IDTypeInfo &id_type)
51{
52 if (id_type.id_code == ID_LINK_PLACEHOLDER) {
53 return false;
54 }
55 if (id_type.id_filter == 0) {
56 return false;
57 }
58 if (id_type.main_listbase_index == INDEX_ID_MAX) {
59 return false;
60 }
61 if (id_type.struct_size == 0) {
62 return false;
63 }
64 if (id_type.name == nullptr) {
65 return false;
66 }
67 if (id_type.name_plural == nullptr) {
68 return false;
69 }
70 if (id_type.translation_context == nullptr) {
71 return false;
72 }
73 if (id_type.asset_type_info == id_type.InvalidPointer<AssetTypeInfo *>()) {
74 return false;
75 }
76 if (id_type.init_data == id_type.InvalidPointer<IDTypeInitDataFunction>()) {
77 return false;
78 }
79 if (id_type.copy_data == id_type.InvalidPointer<IDTypeCopyDataFunction>()) {
80 return false;
81 }
82 if (id_type.free_data == id_type.InvalidPointer<IDTypeFreeDataFunction>()) {
83 return false;
84 }
85 if (id_type.make_local == id_type.InvalidPointer<IDTypeMakeLocalFunction>()) {
86 return false;
87 }
88 if (id_type.foreach_id == id_type.InvalidPointer<IDTypeForeachIDFunction>()) {
89 return false;
90 }
92 return false;
93 }
94 if (id_type.foreach_path == id_type.InvalidPointer<IDTypeForeachPathFunction>()) {
95 return false;
96 }
98 {
99 return false;
100 }
101 if (id_type.foreach_asset_weak_reference ==
103 {
104 return false;
105 }
107 {
108 return false;
109 }
110 if (id_type.blend_write == id_type.InvalidPointer<IDTypeBlendWriteFunction>()) {
111 return false;
112 }
114 return false;
115 }
116 if (id_type.blend_read_after_liblink ==
118 {
119 return false;
120 }
122 return false;
123 }
125 return false;
126 }
127 return true;
128}
129#endif
130
131static void id_type_init()
132{
133 int init_types_num = 0;
134
135#define INIT_TYPE(_id_code) \
136 { \
137 BLI_assert(IDType_##_id_code.main_listbase_index == INDEX_##_id_code); \
138 BLI_assert(id_type_is_valid(IDType_##_id_code)); \
139 id_types[INDEX_##_id_code] = &IDType_##_id_code; \
140 init_types_num++; \
141 } \
142 (void)0
143
183
184 /* Special case. */
187 init_types_num++;
188
189 BLI_assert_msg(init_types_num == INDEX_ID_MAX, "Some IDTypeInfo initialization is missing");
190 UNUSED_VARS_NDEBUG(init_types_num);
191
192 { /* Inspect which ID types can be animated, so that IDType_ID_AC.dependencies_id_types can be
193 * set to include those. The runtime ID* cache of #animrig::Slot will point to any
194 * ID that is animated by it, and thus can point to any animatable ID type. */
196 for (const IDTypeInfo *id_type : id_types) {
197 const bool is_animatable = (id_type->flags & IDTYPE_FLAGS_NO_ANIMDATA) == 0;
198 if (is_animatable) {
199 IDType_ID_AC.dependencies_id_types |= id_type->id_filter;
200 }
201 }
202 }
203
204#undef INIT_TYPE
205}
206
208{
209 /* Initialize data-block types. */
210 id_type_init();
211}
212
214{
215 if (idtype_index >= 0 && idtype_index < int(id_types.size())) {
216 const IDTypeInfo *id_type = id_types[size_t(idtype_index)];
217 if (id_type && id_type->name[0] != '\0') {
218 BLI_assert_msg(BKE_idtype_idcode_to_index(id_type->id_code) == idtype_index,
219 "Critical inconsistency in ID type information");
220 return id_type;
221 }
222 }
223
224 return nullptr;
225}
226
231
233{
235}
236
237static const IDTypeInfo *idtype_get_info_from_name(const char *idtype_name)
238{
239 for (const IDTypeInfo *id_type : id_types) {
240 if (id_type && STREQ(idtype_name, id_type->name)) {
241 return id_type;
242 }
243 }
244
245 return nullptr;
246}
247
248static const IDTypeInfo *idtype_get_info_from_name_case_insensitive(const char *idtype_name)
249{
250 for (const IDTypeInfo *id_type : id_types) {
251 if (id_type && STRCASEEQ(idtype_name, id_type->name)) {
252 return id_type;
253 }
254 }
255
256 return nullptr;
257}
258
259/* Various helpers/wrappers around #IDTypeInfo structure. */
260
261const char *BKE_idtype_idcode_to_name(const short idcode)
262{
263 const IDTypeInfo *id_type = BKE_idtype_get_info_from_idcode(idcode);
264 BLI_assert(id_type != nullptr);
265 return id_type != nullptr ? id_type->name : nullptr;
266}
267
268const char *BKE_idtype_idcode_to_name_plural(const short idcode)
269{
270 const IDTypeInfo *id_type = BKE_idtype_get_info_from_idcode(idcode);
271 BLI_assert(id_type != nullptr);
272 return id_type != nullptr ? id_type->name_plural : nullptr;
273}
274
275const char *BKE_idtype_idcode_to_translation_context(const short idcode)
276{
277 const IDTypeInfo *id_type = BKE_idtype_get_info_from_idcode(idcode);
278 BLI_assert(id_type != nullptr);
279 return id_type != nullptr ? id_type->translation_context : BLT_I18NCONTEXT_DEFAULT;
280}
281
282short BKE_idtype_idcode_from_name(const char *idtype_name)
283{
284 const IDTypeInfo *id_type = idtype_get_info_from_name(idtype_name);
285 BLI_assert(id_type);
286 return id_type != nullptr ? id_type->id_code : 0;
287}
288
290{
291 const IDTypeInfo *id_type = idtype_get_info_from_name_case_insensitive(idtype_name);
292 BLI_assert(id_type);
293 return id_type != nullptr ? id_type->id_code : 0;
294}
295
296const char *BKE_idtype_name_normalize(const char *idtype_name)
297{
298 const IDTypeInfo *id_type = idtype_get_info_from_name_case_insensitive(idtype_name);
299 return id_type ? id_type->name : nullptr;
300}
301
302bool BKE_idtype_idcode_is_valid(const short idcode)
303{
304 return BKE_idtype_get_info_from_idcode(idcode) != nullptr ? true : false;
305}
306
307bool BKE_idtype_idcode_is_linkable(const short idcode)
308{
309 const IDTypeInfo *id_type = BKE_idtype_get_info_from_idcode(idcode);
310 BLI_assert(id_type != nullptr);
311 return id_type != nullptr ? (id_type->flags & IDTYPE_FLAGS_NO_LIBLINKING) == 0 : false;
312}
313
315{
316 const IDTypeInfo *id_type = BKE_idtype_get_info_from_idcode(idcode);
317 BLI_assert(id_type != nullptr);
318 if (id_type != nullptr && (id_type->flags & IDTYPE_FLAGS_ONLY_APPEND) != 0) {
319 /* Only appendable ID types should also always be linkable. */
321 return true;
322 }
323 return false;
324}
325
327{
328 const IDTypeInfo *id_type = BKE_idtype_get_info_from_idcode(idcode);
329 BLI_assert(id_type != nullptr);
330 if (id_type != nullptr && (id_type->flags & IDTYPE_FLAGS_APPEND_IS_REUSABLE) != 0) {
331 /* All appendable ID types should also always be linkable. */
333 return true;
334 }
335 return false;
336}
337
338int BKE_idtype_idcode_to_index(const short idcode)
339{
340#define CASE_IDINDEX(_id) \
341 case ID_##_id: \
342 return INDEX_ID_##_id
343
344 switch (ID_Type(idcode)) {
345 CASE_IDINDEX(AC);
346 CASE_IDINDEX(AR);
348 CASE_IDINDEX(CA);
349 CASE_IDINDEX(CF);
350 CASE_IDINDEX(CU_LEGACY);
351 CASE_IDINDEX(GD_LEGACY);
352 CASE_IDINDEX(GP);
353 CASE_IDINDEX(GR);
355 CASE_IDINDEX(IM);
356 CASE_IDINDEX(KE);
357 CASE_IDINDEX(LA);
358 CASE_IDINDEX(LI);
359 CASE_IDINDEX(LS);
361 CASE_IDINDEX(MA);
362 CASE_IDINDEX(MB);
363 CASE_IDINDEX(MC);
364 CASE_IDINDEX(ME);
365 CASE_IDINDEX(MSK);
366 CASE_IDINDEX(NT);
367 CASE_IDINDEX(OB);
368 CASE_IDINDEX(PA);
369 CASE_IDINDEX(PAL);
370 CASE_IDINDEX(PC);
371 CASE_IDINDEX(PT);
372 CASE_IDINDEX(LP);
373 CASE_IDINDEX(SCE);
374 CASE_IDINDEX(SCR);
375 CASE_IDINDEX(SPK);
376 CASE_IDINDEX(SO);
377 CASE_IDINDEX(TE);
378 CASE_IDINDEX(TXT);
379 CASE_IDINDEX(VF);
380 CASE_IDINDEX(VO);
381 CASE_IDINDEX(WM);
382 CASE_IDINDEX(WO);
383 CASE_IDINDEX(WS);
384 }
385
386 /* Special naughty boy... */
387 if (idcode == ID_LINK_PLACEHOLDER) {
388 return INDEX_ID_NULL;
389 }
390
391 return -1;
392
393#undef CASE_IDINDEX
394}
395
397{
398#define CASE_IDINDEX(_id) \
399 case FILTER_ID_##_id: \
400 return INDEX_ID_##_id
401
402 switch (id_filter) {
403 CASE_IDINDEX(AC);
404 CASE_IDINDEX(AR);
406 CASE_IDINDEX(CA);
407 CASE_IDINDEX(CF);
408 CASE_IDINDEX(CU_LEGACY);
409 CASE_IDINDEX(GD_LEGACY);
410 CASE_IDINDEX(GP);
411 CASE_IDINDEX(GR);
413 CASE_IDINDEX(IM);
414 CASE_IDINDEX(KE);
415 CASE_IDINDEX(LA);
416 CASE_IDINDEX(LI);
417 CASE_IDINDEX(LS);
419 CASE_IDINDEX(MA);
420 CASE_IDINDEX(MB);
421 CASE_IDINDEX(MC);
422 CASE_IDINDEX(ME);
423 CASE_IDINDEX(MSK);
424 CASE_IDINDEX(NT);
425 CASE_IDINDEX(OB);
426 CASE_IDINDEX(PA);
427 CASE_IDINDEX(PAL);
428 CASE_IDINDEX(PC);
429 CASE_IDINDEX(PT);
430 CASE_IDINDEX(LP);
431 CASE_IDINDEX(SCE);
432 CASE_IDINDEX(SCR);
433 CASE_IDINDEX(SPK);
434 CASE_IDINDEX(SO);
435 CASE_IDINDEX(TE);
436 CASE_IDINDEX(TXT);
437 CASE_IDINDEX(VF);
438 CASE_IDINDEX(VO);
439 CASE_IDINDEX(WM);
440 CASE_IDINDEX(WO);
441 CASE_IDINDEX(WS);
442 }
443
444 /* No handling of #ID_LINK_PLACEHOLDER or #INDEX_ID_NULL here. */
445
446 return -1;
447
448#undef CASE_IDINDEX
449}
450
451short BKE_idtype_index_to_idcode(const int idtype_index)
452{
453 const IDTypeInfo *id_type = BKE_idtype_get_info_from_idtype_index(idtype_index);
454 if (id_type) {
455 return id_type->id_code;
456 }
457
459 return -1;
460}
461
463{
464 const IDTypeInfo *id_type = BKE_idtype_get_info_from_idtype_index(idtype_index);
465 if (id_type) {
466 return id_type->id_filter;
467 }
468
470 return 0;
471}
472
477
482
483short BKE_idtype_idcode_iter_step(int *idtype_index)
484{
485 return (*idtype_index < int(id_types.size())) ? BKE_idtype_index_to_idcode((*idtype_index)++) :
486 0;
487}
488
490 IDTypeForeachCacheFunctionCallback function_callback,
491 void *user_data)
492{
493 const IDTypeInfo *type_info = BKE_idtype_get_info_from_id(id);
494 if (type_info->foreach_cache != nullptr) {
495 type_info->foreach_cache(id, function_callback, user_data);
496 }
497
498 /* Handle 'private IDs'. */
499 bNodeTree *nodetree = bke::node_tree_from_id(id);
500 if (nodetree != nullptr) {
501 type_info = BKE_idtype_get_info_from_id(&nodetree->id);
502 if (type_info == nullptr) {
503 /* Very old .blend file seem to have empty names for their embedded node trees, see
504 * `blo_do_versions_250()`. Assume those are node-trees then. */
506 }
507 if (type_info->foreach_cache != nullptr) {
508 type_info->foreach_cache(&nodetree->id, function_callback, user_data);
509 }
510 }
511
512 if (GS(id->name) == ID_SCE) {
513 Scene *scene = id_cast<Scene *>(id);
514 if (scene->master_collection != nullptr) {
516 if (type_info->foreach_cache != nullptr) {
517 type_info->foreach_cache(&scene->master_collection->id, function_callback, user_data);
518 }
519 }
520 }
521}
522
523} // namespace blender
#define BLT_I18NCONTEXT_DEFAULT
ID and Library types, which are fundamental for SDNA.
#define INDEX_ID_MAX
Definition DNA_ID.h:1362
#define ID_LINK_PLACEHOLDER
Object groups, one object can be in many groups at once.
#define BLI_assert_unreachable()
#define UNUSED_VARS_NDEBUG(...)
#define STRCASEEQ(a, b)
#define BLI_assert_msg(a, msg)
#define STREQ(a, b)
#define BR
Definition boxpack_2d.cc:79
#define INIT_TYPE(_id_code)
#define CASE_IDINDEX(_id)
#define LT
bNodeTree * node_tree_from_id(ID *id)
Definition node.cc:5337
BMesh Iterators.
const char * BKE_idtype_idcode_to_name(short idcode)
Definition idtype.cc:261
void(*)(ID *id, FunctionRef< void(AssetWeakReference &weak_ref)> fn) IDTypeForeachAssetWeakReferenceFunction
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void BKE_idtype_id_foreach_cache(ID *id, IDTypeForeachCacheFunctionCallback function_callback, void *user_data)
Definition idtype.cc:489
uint64_t BKE_idtype_idcode_to_idfilter(short idcode)
Definition idtype.cc:473
unsigned int BLI_ghashutil_uinthash(unsigned int key)
unsigned __int64 uint64_t
Definition BKE_text.h:91
short BKE_idtype_index_to_idcode(int idtype_index)
Definition idtype.cc:451
const IDTypeInfo * BKE_idtype_get_info_from_idcode(short id_code)
Definition idtype.cc:227
unsigned int uint
Definition BKE_text.h:55
bool BKE_idtype_idcode_is_valid(short idcode)
Definition idtype.cc:302
IDTypeInfo IDType_ID_LINK_PLACEHOLDER
Definition lib_id.cc:89
static std::array< IDTypeInfo *, INDEX_ID_MAX > id_types
Definition idtype.cc:46
static void id_type_init()
Definition idtype.cc:131
bool BKE_idtype_idcode_is_linkable(short idcode)
Definition idtype.cc:307
static bool id_type_is_valid(const IDTypeInfo &id_type)
Definition idtype.cc:50
void(*)(ID *id, const IDCacheKey *cache_key, void **cache_p, uint flags, void *user_data) IDTypeForeachCacheFunctionCallback
uint BKE_idtype_cache_key_hash(const void *key_v)
Definition idtype.cc:30
bool BKE_idtype_idcode_append_is_reusable(short idcode)
Definition idtype.cc:326
void(*)(BlendLibReader *reader, ID *id) IDTypeBlendReadAfterLiblinkFunction
void(*)(ID *id, const IDTypeForeachColorFunctionCallback &cb) IDTypeForeachColorFunction
void(*)(BlendDataReader *reader, ID *id) IDTypeBlendReadDataFunction
void(*)(Main *bmain, ID *id, int flags) IDTypeMakeLocalFunction
const char * BKE_idtype_name_normalize(const char *idtype_name)
Definition idtype.cc:296
void BKE_idtype_init()
Definition idtype.cc:207
void(*)(ID *id, IDTypeForeachCacheFunctionCallback function_callback, void *user_data) IDTypeForeachCacheFunction
const IDTypeInfo * BKE_idtype_get_info_from_id(const ID *id)
Definition idtype.cc:232
void(*)(BlendLibReader *reader, ID *id_new, ID *id_old) IDTypeBlendReadUndoPreserve
void(*)(ID *id, BPathForeachPathData *bpath_data) IDTypeForeachPathFunction
short BKE_idtype_idcode_iter_step(int *idtype_index)
Definition idtype.cc:483
void(*)(ID *id) IDTypeInitDataFunction
Definition BKE_idtype.hh:91
const IDTypeInfo * BKE_idtype_get_info_from_idtype_index(const int idtype_index)
Definition idtype.cc:213
int BKE_idtype_idcode_to_index(short idcode)
Definition idtype.cc:338
void(*)(ID *id, LibraryForeachIDData *data) IDTypeForeachIDFunction
const char * BKE_idtype_idcode_to_name_plural(short idcode)
Definition idtype.cc:268
ID **(*)(ID *id, bool debug_relationship_assert) IDTypeEmbeddedOwnerPointerGetFunction
bool BKE_idtype_idcode_is_only_appendable(short idcode)
Definition idtype.cc:314
uint64_t BKE_idtype_index_to_idfilter(int idtype_index)
Definition idtype.cc:462
@ INDEX_ID_NULL
Definition DNA_ID.h:1359
void(*)(BlendWriter *writer, ID *id, const void *id_address) IDTypeBlendWriteFunction
ID_Type GS(const char *name)
Definition DNA_ID.h:410
short BKE_idtype_idfilter_to_idcode(uint64_t idfilter)
Definition idtype.cc:478
size_t BLI_ghashutil_combine_hash(size_t hash_a, size_t hash_b)
bool BKE_idtype_cache_key_cmp(const void *key_a_v, const void *key_b_v)
Definition idtype.cc:38
short BKE_idtype_idcode_from_name_case_insensitive(const char *idtype_name)
Definition idtype.cc:289
void(*)(ID *id_dst, ID *id_src) IDTypeLibOverrideApplyPost
short BKE_idtype_idcode_from_name(const char *idtype_name)
Definition idtype.cc:282
static const IDTypeInfo * idtype_get_info_from_name_case_insensitive(const char *idtype_name)
Definition idtype.cc:248
int BKE_idtype_idfilter_to_index(uint64_t id_filter)
Definition idtype.cc:396
void(*)(ID *id) IDTypeFreeDataFunction
Definition BKE_idtype.hh:97
void(*)( Main *bmain, std::optional< Library * > owner_library, ID *id_dst, const ID *id_src, int flag) IDTypeCopyDataFunction
Definition BKE_idtype.hh:94
const char * BKE_idtype_idcode_to_translation_context(short idcode)
Definition idtype.cc:275
static const IDTypeInfo * idtype_get_info_from_name(const char *idtype_name)
Definition idtype.cc:237
@ IDTYPE_FLAGS_NO_ANIMDATA
Definition BKE_idtype.hh:52
@ IDTYPE_FLAGS_ONLY_APPEND
Definition BKE_idtype.hh:44
@ IDTYPE_FLAGS_NO_LIBLINKING
Definition BKE_idtype.hh:38
@ IDTYPE_FLAGS_APPEND_IS_REUSABLE
Definition BKE_idtype.hh:50
#define CV
#define hash
Definition noise_c.cc:156
unsigned int id_session_uid
Definition BKE_idtype.hh:78
IDTypeForeachIDFunction foreach_id
const char * name_plural
IDTypeForeachPathFunction foreach_path
IDTypeBlendWriteFunction blend_write
static constexpr T InvalidPointer()
IDTypeBlendReadAfterLiblinkFunction blend_read_after_liblink
IDTypeForeachCacheFunction foreach_cache
IDTypeEmbeddedOwnerPointerGetFunction owner_pointer_get
IDTypeForeachColorFunction foreach_working_space_color
IDTypeMakeLocalFunction make_local
AssetTypeInfo * asset_type_info
IDTypeForeachAssetWeakReferenceFunction foreach_asset_weak_reference
IDTypeLibOverrideApplyPost lib_override_apply_post
IDTypeInitDataFunction init_data
IDTypeBlendReadUndoPreserve blend_read_undo_preserve
IDTypeCopyDataFunction copy_data
const char * translation_context
IDTypeBlendReadDataFunction blend_read_data
uint64_t dependencies_id_types
IDTypeFreeDataFunction free_data
const char * name
struct Collection * master_collection