Blender V4.3
rna_blendfile_import.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
9#include <cstdlib>
10
11#include "BLO_readfile.hh"
12
13#include "DNA_space_types.h"
14
16
17#include "RNA_access.hh"
18#include "RNA_define.hh"
19#include "RNA_enum_types.hh"
20
21#include "rna_internal.hh"
22
23#ifdef RNA_RUNTIME
24
25# include "BLI_bit_span.hh"
26# include "BLI_string_utils.hh"
27
28void rna_BlendImportContextLibrary_filepath_get(PointerRNA *ptr, char *value)
29{
31 ptr->data);
32 const size_t str_len = ctx_lib->path.length();
33 BLI_strncpy(value, ctx_lib->path.c_str(), str_len + 1);
34}
35
36int rna_BlendImportContextLibrary_filepath_len(PointerRNA *ptr)
37{
39 ptr->data);
40 return int(ctx_lib->path.length());
41}
42
43void rna_BlendImportContextItem_name_get(PointerRNA *ptr, char *value)
44{
46 ptr->data);
47 const size_t str_len = ctx_item->name.length();
48 BLI_strncpy(value, ctx_item->name.c_str(), str_len + 1);
49}
50
51int rna_BlendImportContextItem_name_len(PointerRNA *ptr)
52{
54 ptr->data);
55 return int(ctx_item->name.length());
56}
57
58int rna_BlendImportContextItem_id_type_get(PointerRNA *ptr)
59{
61 ptr->data);
62 return int(ctx_item->idcode);
63}
64
65struct RNABlendImportContextItemLibrariesIterator {
68 int iter_index;
69};
70
71void rna_BlendImportContextItem_libraries_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
72{
74 ptr->data);
75
76 const blender::BitVector<> &libraries = ctx_item->libraries;
77 RNABlendImportContextItemLibrariesIterator *libs_iter =
78 MEM_new<RNABlendImportContextItemLibrariesIterator>(
79 __func__, RNABlendImportContextItemLibrariesIterator{ctx_item, libraries.begin(), 0});
80 iter->internal.custom = libs_iter;
81 while (!(*libs_iter->iter) && libs_iter->iter != libs_iter->ctx_item->libraries.end()) {
82 libs_iter->iter.operator++();
83 libs_iter->iter_index++;
84 }
85 iter->valid = (libs_iter->iter != libs_iter->ctx_item->libraries.end());
86}
87
88void rna_BlendImportContextItem_libraries_next(CollectionPropertyIterator *iter)
89{
90 RNABlendImportContextItemLibrariesIterator *libs_iter =
91 static_cast<RNABlendImportContextItemLibrariesIterator *>(iter->internal.custom);
92 do {
93 libs_iter->iter.operator++();
94 libs_iter->iter_index++;
95 } while (!(*libs_iter->iter) && libs_iter->iter != libs_iter->ctx_item->libraries.end());
96 iter->valid = (libs_iter->iter != libs_iter->ctx_item->libraries.end());
97}
98
99void rna_BlendImportContextItem_libraries_end(CollectionPropertyIterator *iter)
100{
101 RNABlendImportContextItemLibrariesIterator *libs_iter =
102 static_cast<RNABlendImportContextItemLibrariesIterator *>(iter->internal.custom);
103
104 iter->valid = false;
105 iter->internal.custom = nullptr;
106 MEM_delete(libs_iter);
107}
108
109PointerRNA rna_BlendImportContextItem_libraries_get(CollectionPropertyIterator *iter)
110{
111 RNABlendImportContextItemLibrariesIterator *libs_iter =
112 static_cast<RNABlendImportContextItemLibrariesIterator *>(iter->internal.custom);
113
115 libs_iter->ctx_item->lapp_context->libraries[libs_iter->iter_index];
116 return rna_pointer_inherit_refine(&iter->parent, &RNA_BlendImportContextLibrary, &ctx_lib);
117}
118
119int rna_BlendImportContextItem_libraries_len(PointerRNA *ptr)
120{
122 ptr->data);
123
124 /* Count amount of enabled libraries in the item's bitmask. */
125 int count = 0;
126 for (const blender::BitRef &bit : ctx_item->libraries) {
127 if (bit) {
128 count++;
129 }
130 }
131 return count;
132}
133
134int rna_BlendImportContextItem_append_action_get(PointerRNA *ptr)
135{
137 ptr->data);
138 return int(ctx_item->action);
139}
140
141int rna_BlendImportContextItem_import_info_get(PointerRNA *ptr)
142{
144 ptr->data);
145 return int(ctx_item->tag);
146}
147
148PointerRNA rna_BlendImportContextItem_id_get(PointerRNA *ptr)
149{
151 ptr->data);
152 return rna_pointer_inherit_refine(&PointerRNA_NULL, &RNA_ID, ctx_item->new_id);
153}
154
155PointerRNA rna_BlendImportContextItem_source_library_get(PointerRNA *ptr)
156{
158 ptr->data);
159 return rna_pointer_inherit_refine(&PointerRNA_NULL, &RNA_Library, ctx_item->source_library);
160}
161
162PointerRNA rna_BlendImportContextItem_library_override_id_get(PointerRNA *ptr)
163{
165 ptr->data);
166 return rna_pointer_inherit_refine(&PointerRNA_NULL, &RNA_ID, ctx_item->liboverride_id);
167}
168
169PointerRNA rna_BlendImportContextItem_reusable_local_id_get(PointerRNA *ptr)
170{
172 ptr->data);
174}
175
176struct RNABlendImportContextItemsIterator {
179};
180
181void rna_BlendImportContext_import_items_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
182{
184
185 RNABlendImportContextItemsIterator *items_iter = MEM_new<RNABlendImportContextItemsIterator>(
186 __func__, RNABlendImportContextItemsIterator{ctx, ctx->items.begin()});
187 iter->internal.custom = items_iter;
188 iter->valid = (items_iter->iter != items_iter->ctx->items.end());
189}
190
191void rna_BlendImportContext_import_items_next(CollectionPropertyIterator *iter)
192{
193 RNABlendImportContextItemsIterator *items_iter =
194 static_cast<RNABlendImportContextItemsIterator *>(iter->internal.custom);
195 items_iter->iter++;
196 iter->valid = (items_iter->iter != items_iter->ctx->items.end());
197}
198
199void rna_BlendImportContext_import_items_end(CollectionPropertyIterator *iter)
200{
201 RNABlendImportContextItemsIterator *items_iter =
202 static_cast<RNABlendImportContextItemsIterator *>(iter->internal.custom);
203
204 iter->valid = false;
205 iter->internal.custom = nullptr;
206 MEM_delete(items_iter);
207}
208
209PointerRNA rna_BlendImportContext_import_items_get(CollectionPropertyIterator *iter)
210{
211 RNABlendImportContextItemsIterator *items_iter =
212 static_cast<RNABlendImportContextItemsIterator *>(iter->internal.custom);
213
214 BlendfileLinkAppendContextItem &ctx_item = *items_iter->iter;
215 return rna_pointer_inherit_refine(&iter->parent, &RNA_BlendImportContextItem, &ctx_item);
216}
217
218int rna_BlendImportContext_import_items_len(PointerRNA *ptr)
219{
221 return int(ctx->items.size());
222}
223
224int rna_BlendImportContext_options_get(PointerRNA *ptr)
225{
227 return int(ctx->params->flag);
228}
229
230int rna_BlendImportContext_process_stage_get(PointerRNA *ptr)
231{
233 return int(ctx->process_stage);
234}
235
236#else /* RNA_RUNTIME */
237
239{
240 StructRNA *srna;
241 PropertyRNA *prop;
242
243 srna = RNA_def_struct(brna, "BlendImportContextLibrary", nullptr);
245 srna,
246 "Blendfile Import Context Library",
247 "Library (blendfile) reference in a BlendImportContext data. Currently only "
248 "exposed as read-only data for the pre/post blendimport handlers");
249
250 RNA_define_verify_sdna(false); /* not in sdna */
251
252 prop = RNA_def_property(srna, "filepath", PROP_STRING, PROP_FILEPATH);
255 "rna_BlendImportContextLibrary_filepath_get",
256 "rna_BlendImportContextLibrary_filepath_len",
257 nullptr);
258
259 RNA_define_verify_sdna(true); /* not in sdna */
260}
261
263{
264 StructRNA *srna;
265
266 RNA_def_property_srna(cprop, "BlendImportContextLibraries");
267 srna = RNA_def_struct(brna, "BlendImportContextLibraries", nullptr);
269 "Blendfile Import Context Libraries",
270 "Collection of source libraries, i.e. blendfile paths");
271}
272
274{
275 StructRNA *srna;
276 PropertyRNA *prop;
277
278 srna = RNA_def_struct(brna, "BlendImportContextItem", nullptr);
280 srna,
281 "Blendfile Import Context Item",
282 "An item (representing a data-block) in a BlendImportContext data. Currently only "
283 "exposed as read-only data for the pre/post linking handlers");
284
285 RNA_define_verify_sdna(false); /* not in sdna */
286
287 prop = RNA_def_property(srna, "name", PROP_STRING, PROP_NONE);
289 RNA_def_property_ui_text(prop, "ID Name", "ID name of the item");
291 prop, "rna_BlendImportContextItem_name_get", "rna_BlendImportContextItem_name_len", nullptr);
292
293 prop = RNA_def_property(srna, "id_type", PROP_ENUM, PROP_NONE);
296 RNA_def_property_ui_text(prop, "ID Type", "ID type of the item");
297 RNA_def_property_enum_funcs(prop, "rna_BlendImportContextItem_id_type_get", nullptr, nullptr);
298
299 prop = RNA_def_property(srna, "source_libraries", PROP_COLLECTION, PROP_NONE);
300 RNA_def_property_struct_type(prop, "BlendImportContextLibrary");
303 "Source Libraries",
304 "List of libraries to search and import that ID from. The ID will be "
305 "imported from the first file in that list that contains it");
307 "rna_BlendImportContextItem_libraries_begin",
308 "rna_BlendImportContextItem_libraries_next",
309 "rna_BlendImportContextItem_libraries_end",
310 "rna_BlendImportContextItem_libraries_get",
311 "rna_BlendImportContextItem_libraries_len",
312 nullptr,
313 nullptr,
314 nullptr);
316
317 static const EnumPropertyItem blend_import_item_append_action_items[] = {
318 {LINK_APPEND_ACT_UNSET, "UNSET", 0, "", "Not yet defined"},
319 {LINK_APPEND_ACT_KEEP_LINKED, "KEEP_LINKED", 0, "", "ID has been kept linked"},
321 "REUSE_LOCAL",
322 0,
323 "",
324 "An existing matching local ID has been re-used"},
325 {LINK_APPEND_ACT_MAKE_LOCAL, "MAKE_LOCAL", 0, "", "The newly linked ID has been made local"},
327 "COPY_LOCAL",
328 0,
329 "",
330 "The linked ID had other unrelated usages, so it has been duplicated into a local copy"},
331 {0, nullptr, 0, nullptr, nullptr},
332 };
333 prop = RNA_def_property(srna, "append_action", PROP_ENUM, PROP_NONE);
334 RNA_def_property_enum_items(prop, blend_import_item_append_action_items);
337 "Append Action",
338 "How this item has been handled by the append operation. Only set if "
339 "the data has been appended");
341 prop, "rna_BlendImportContextItem_append_action_get", nullptr, nullptr);
342
343 static const EnumPropertyItem blend_import_item_import_info_items[] = {
345 "INDIRECT_USAGE",
346 0,
347 "",
348 "That item was added for an indirectly imported ID, as a dependency of another data-block"},
350 "LIBOVERRIDE_DEPENDENCY",
351 0,
352 "",
353 "That item represents an ID also used as liboverride dependency (either directly, as a "
354 "liboverride reference, or indirectly, as data used by a liboverride reference). It should "
355 "never be directly made local. Mutually exclusive with `LIBOVERRIDE_DEPENDENCY_ONLY`"},
357 "LIBOVERRIDE_DEPENDENCY_ONLY",
358 0,
359 "",
360 "That item represents an ID only used as liboverride dependency (either directly or "
361 "indirectly, see `LIBOVERRIDE_DEPENDENCY` for precisions). It should not be considered "
362 "during the 'make local' (append) process, and remain purely linked data. Mutually "
363 "exclusive with `LIBOVERRIDE_DEPENDENCY`"},
364 {0, nullptr, 0, nullptr, nullptr},
365 };
366 prop = RNA_def_property(srna, "import_info", PROP_ENUM, PROP_NONE);
367 RNA_def_property_enum_items(prop, blend_import_item_import_info_items);
371 prop, "Import Info", "Various status info about an item after it has been imported");
373 prop, "rna_BlendImportContextItem_import_info_get", nullptr, nullptr);
374
375 prop = RNA_def_property(srna, "id", PROP_POINTER, PROP_NONE);
379 "Imported ID",
380 "The imported ID. None until it has been linked or appended. "
381 "May be the same as ``reusable_local_id`` when appended");
383 prop, "rna_BlendImportContextItem_id_get", nullptr, nullptr, nullptr);
384
385 prop = RNA_def_property(srna, "source_library", PROP_POINTER, PROP_NONE);
386 RNA_def_property_struct_type(prop, "Library");
389 "Source Library",
390 "Library ID representing the blendfile from which the ID was imported. "
391 "None until the ID has been linked or appended");
393 prop, "rna_BlendImportContextItem_source_library_get", nullptr, nullptr, nullptr);
394
395 prop = RNA_def_property(srna, "library_override_id", PROP_POINTER, PROP_NONE);
399 prop,
400 "Library Overridden ID",
401 "The library override of the linked ID. None until it has been created");
403 prop, "rna_BlendImportContextItem_library_override_id_get", nullptr, nullptr, nullptr);
404
405 prop = RNA_def_property(srna, "reusable_local_id", PROP_POINTER, PROP_NONE);
409 "Reusable Local ID",
410 "The already existing local ID that may be reused in append & reuse "
411 "case. None until it has been found");
413 prop, "rna_BlendImportContextItem_reusable_local_id_get", nullptr, nullptr, nullptr);
414
415 RNA_define_verify_sdna(true); /* not in sdna */
416}
417
419{
420 StructRNA *srna;
421
422 RNA_def_property_srna(cprop, "BlendImportContextItems");
423 srna = RNA_def_struct(brna, "BlendImportContextItems", nullptr);
425 srna, "Blendfile Import Context Items", "Collection of blendfile import context items");
426
427 /* TODO: Add/Remove items _before_ doing link/append (i.e. for 'pre' handlers). */
428}
429
431{
432 StructRNA *srna;
433 PropertyRNA *prop;
434
435 srna = RNA_def_struct(brna, "BlendImportContext", nullptr);
437 srna,
438 "Blendfile Import Context",
439 "Contextual data for a blendfile library/linked-data related operation. Currently "
440 "only exposed as read-only data for the pre/post blendimport handlers");
441
442 RNA_define_verify_sdna(false); /* not in sdna */
443
444 /* NOTE: Cannot use just `items` here as this is a reserved Python dict method name. */
445 prop = RNA_def_property(srna, "import_items", PROP_COLLECTION, PROP_NONE);
446 RNA_def_property_struct_type(prop, "BlendImportContextItem");
449 "rna_BlendImportContext_import_items_begin",
450 "rna_BlendImportContext_import_items_next",
451 "rna_BlendImportContext_import_items_end",
452 "rna_BlendImportContext_import_items_get",
453 "rna_BlendImportContext_import_items_len",
454 nullptr,
455 nullptr,
456 nullptr);
458
459 static const EnumPropertyItem blend_import_options_items[] = {
460 {FILE_LINK, "LINK", 0, "", "Only link data, instead of appending it"},
462 "MAKE_PATHS_RELATIVE",
463 0,
464 "",
465 "Make paths of used library blendfiles relative to current blendfile"},
467 "USE_PLACEHOLDERS",
468 0,
469 "",
470 "Generate a placeholder (empty ID) if not found in any library files"},
472 "FORCE_INDIRECT",
473 0,
474 "",
475 "Force loaded ID to be tagged as indirectly linked (used in reload context only)"},
477 "APPEND_SET_FAKEUSER",
478 0,
479 "",
480 "Set fake user on appended IDs"},
482 "APPEND_RECURSIVE",
483 0,
484 "",
485 "Append (make local) also indirect dependencies of appended IDs coming from other "
486 "libraries. NOTE: All IDs (including indirectly linked ones) coming from the same initial "
487 "library are always made local"},
489 "APPEND_LOCAL_ID_REUSE",
490 0,
491 "",
492 "Try to re-use previously appended matching IDs when appending them again, instead of "
493 "creating local duplicates"},
495 "APPEND_ASSET_DATA_CLEAR",
496 0,
497 "",
498 "Clear the asset data on append (it is always kept for linked data)"},
499 {FILE_AUTOSELECT, "SELECT_OBJECTS", 0, "", "Automatically select imported objects"},
501 "USE_ACTIVE_COLLECTION",
502 0,
503 "",
504 "Use the active Collection of the current View Layer to instantiate imported "
505 "collections and objects"},
507 "OBDATA_INSTANCE",
508 0,
509 "",
510 "Instantiate object data IDs (i.e. create objects for them if needed)"},
512 "COLLECTION_INSTANCE",
513 0,
514 "",
515 "Instantiate collections as empties, instead of linking them into the current view layer"},
516 {0, nullptr, 0, nullptr, nullptr},
517 };
518 prop = RNA_def_property(srna, "options", PROP_ENUM, PROP_NONE);
519 RNA_def_property_enum_items(prop, blend_import_options_items);
522 RNA_def_property_ui_text(prop, "", "Options for this blendfile import operation");
523 RNA_def_property_enum_funcs(prop, "rna_BlendImportContext_options_get", nullptr, nullptr);
524
525 /* NOTE: Only stages currently exposed to handlers are listed here. */
526 static const EnumPropertyItem blend_import_process_stage_items[] = {
528 "INIT",
529 0,
530 "",
531 "Blendfile import context has been initialized and filled with a list of items to import, "
532 "no data has been linked or appended yet"},
534 "DONE",
535 0,
536 "",
537 "All data has been imported and is available in the list of ``import_items``"},
538 {0, nullptr, 0, nullptr, nullptr},
539 };
540 prop = RNA_def_property(srna, "process_stage", PROP_ENUM, PROP_NONE);
541 RNA_def_property_enum_items(prop, blend_import_process_stage_items);
543 RNA_def_property_ui_text(prop, "", "Current stage of the import process");
544 RNA_def_property_enum_funcs(prop, "rna_BlendImportContext_process_stage_get", nullptr, nullptr);
545
546 RNA_define_verify_sdna(true); /* not in sdna */
547}
548
555
556#endif /* RNA_RUNTIME */
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
external readfile function prototypes.
@ BLO_LIBLINK_APPEND_RECURSIVE
@ BLO_LIBLINK_USE_PLACEHOLDERS
@ BLO_LIBLINK_APPEND_ASSET_DATA_CLEAR
@ BLO_LIBLINK_OBDATA_INSTANCE
@ BLO_LIBLINK_APPEND_SET_FAKEUSER
@ BLO_LIBLINK_FORCE_INDIRECT
@ BLO_LIBLINK_APPEND_LOCAL_ID_REUSE
@ BLO_LIBLINK_COLLECTION_INSTANCE
@ FILE_ACTIVE_COLLECTION
@ FILE_RELPATH
@ FILE_AUTOSELECT
@ FILE_LINK
@ PROP_ENUM
Definition RNA_types.hh:69
@ PROP_STRING
Definition RNA_types.hh:68
@ PROP_POINTER
Definition RNA_types.hh:70
@ PROP_COLLECTION
Definition RNA_types.hh:71
@ PROP_EDITABLE
Definition RNA_types.hh:207
@ PROP_ENUM_FLAG
Definition RNA_types.hh:293
@ PROP_NONE
Definition RNA_types.hh:136
@ PROP_FILEPATH
Definition RNA_types.hh:139
constexpr PointerRNA PointerRNA_NULL
Definition RNA_types.hh:45
BitIterator begin() const
draw_view push_constant(Type::INT, "radiance_src") .push_constant(Type capture_info_buf storage_buf(1, Qualifier::READ, "ObjectBounds", "bounds_buf[]") .push_constant(Type draw_view int
int count
const EnumPropertyItem rna_enum_id_type_items[]
Definition rna_ID.cc:35
PointerRNA rna_pointer_inherit_refine(const PointerRNA *ptr, StructRNA *type, void *data)
static void rna_def_blendfile_import_context(BlenderRNA *brna)
static void rna_def_blendfile_import_library(BlenderRNA *brna)
static void RNA_def_blendfile_import_libraries(BlenderRNA *brna, PropertyRNA *cprop)
static void rna_def_blendfile_import_items(BlenderRNA *brna, PropertyRNA *cprop)
void RNA_def_blendfile_import(BlenderRNA *brna)
static void rna_def_blendfile_import_item(BlenderRNA *brna)
void RNA_def_property_string_funcs(PropertyRNA *prop, const char *get, const char *length, const char *set)
void RNA_define_verify_sdna(bool verify)
void RNA_def_property_ui_text(PropertyRNA *prop, const char *name, const char *description)
void RNA_def_property_srna(PropertyRNA *prop, const char *type)
void RNA_def_property_collection_funcs(PropertyRNA *prop, const char *begin, const char *next, const char *end, const char *get, const char *length, const char *lookupint, const char *lookupstring, const char *assignint)
void RNA_def_struct_ui_text(StructRNA *srna, const char *name, const char *description)
void RNA_def_property_enum_items(PropertyRNA *prop, const EnumPropertyItem *item)
void RNA_def_property_struct_type(PropertyRNA *prop, const char *type)
PropertyRNA * RNA_def_property(StructOrFunctionRNA *cont_, const char *identifier, int type, int subtype)
void RNA_def_property_enum_funcs(PropertyRNA *prop, const char *get, const char *set, const char *item)
StructRNA * RNA_def_struct(BlenderRNA *brna, const char *identifier, const char *from)
void RNA_def_property_clear_flag(PropertyRNA *prop, PropertyFlag flag)
void RNA_def_property_pointer_funcs(PropertyRNA *prop, const char *get, const char *set, const char *type_fn, const char *poll)
void RNA_def_property_flag(PropertyRNA *prop, PropertyFlag flag)
std::list< BlendfileLinkAppendContextItem >::iterator items_iterator_t
std::list< BlendfileLinkAppendContextItem > items
union CollectionPropertyIterator::@1329 internal
void * data
Definition RNA_types.hh:42
PointerRNA * ptr
Definition wm_files.cc:4126