Blender V5.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
8
9#include <cstdlib>
10
11#include "BLO_readfile.hh"
12
13#include "BLT_translation.hh"
14
15#include "DNA_space_types.h"
16
18
19#include "RNA_define.hh"
20#include "RNA_enum_types.hh"
21
22#include "rna_internal.hh"
23
24#ifdef RNA_RUNTIME
25
26# include "BLI_bit_span.hh"
27# include "BLI_string.hh"
28
29namespace blender {
30
31void rna_BlendImportContextLibrary_filepath_get(PointerRNA *ptr, char *value)
32{
34 ptr->data);
35 const size_t str_len = ctx_lib->path.length();
36 BLI_strncpy(value, ctx_lib->path.c_str(), str_len + 1);
37}
38
39int rna_BlendImportContextLibrary_filepath_len(PointerRNA *ptr)
40{
42 ptr->data);
43 return int(ctx_lib->path.length());
44}
45
46void rna_BlendImportContextItem_name_get(PointerRNA *ptr, char *value)
47{
49 ptr->data);
50 const size_t str_len = ctx_item->name.length();
51 BLI_strncpy(value, ctx_item->name.c_str(), str_len + 1);
52}
53
54int rna_BlendImportContextItem_name_len(PointerRNA *ptr)
55{
57 ptr->data);
58 return int(ctx_item->name.length());
59}
60
61int rna_BlendImportContextItem_id_type_get(PointerRNA *ptr)
62{
64 ptr->data);
65 return int(ctx_item->idcode);
66}
67
68struct RNABlendImportContextItemLibrariesIterator {
69 BlendfileLinkAppendContextItem *ctx_item;
70 bits::BitIterator iter;
71 int iter_index;
72};
73
74void rna_BlendImportContextItem_libraries_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
75{
77 ptr->data);
78
79 const BitVector<> &libraries = ctx_item->libraries;
80 RNABlendImportContextItemLibrariesIterator *libs_iter =
81 MEM_new<RNABlendImportContextItemLibrariesIterator>(
82 __func__, RNABlendImportContextItemLibrariesIterator{ctx_item, libraries.begin(), 0});
83 iter->internal.custom = libs_iter;
84 while (!(*libs_iter->iter) && libs_iter->iter != libs_iter->ctx_item->libraries.end()) {
85 libs_iter->iter.operator++();
86 libs_iter->iter_index++;
87 }
88 iter->valid = (libs_iter->iter != libs_iter->ctx_item->libraries.end());
89}
90
91void rna_BlendImportContextItem_libraries_next(CollectionPropertyIterator *iter)
92{
93 RNABlendImportContextItemLibrariesIterator *libs_iter =
94 static_cast<RNABlendImportContextItemLibrariesIterator *>(iter->internal.custom);
95 do {
96 libs_iter->iter.operator++();
97 libs_iter->iter_index++;
98 } while (!(*libs_iter->iter) && libs_iter->iter != libs_iter->ctx_item->libraries.end());
99 iter->valid = (libs_iter->iter != libs_iter->ctx_item->libraries.end());
100}
101
102void rna_BlendImportContextItem_libraries_end(CollectionPropertyIterator *iter)
103{
104 RNABlendImportContextItemLibrariesIterator *libs_iter =
105 static_cast<RNABlendImportContextItemLibrariesIterator *>(iter->internal.custom);
106
107 iter->valid = false;
108 iter->internal.custom = nullptr;
109 MEM_delete(libs_iter);
110}
111
112PointerRNA rna_BlendImportContextItem_libraries_get(CollectionPropertyIterator *iter)
113{
114 RNABlendImportContextItemLibrariesIterator *libs_iter =
115 static_cast<RNABlendImportContextItemLibrariesIterator *>(iter->internal.custom);
116
118 libs_iter->ctx_item->lapp_context->libraries[libs_iter->iter_index];
119 return RNA_pointer_create_with_parent(iter->parent, RNA_BlendImportContextLibrary, &ctx_lib);
120}
121
122int rna_BlendImportContextItem_libraries_len(PointerRNA *ptr)
123{
125 ptr->data);
126
127 /* Count amount of enabled libraries in the item's bitmask. */
128 int count = 0;
129 for (const BitRef &bit : ctx_item->libraries) {
130 if (bit) {
131 count++;
132 }
133 }
134 return count;
135}
136
137int rna_BlendImportContextItem_append_action_get(PointerRNA *ptr)
138{
140 ptr->data);
141 return int(ctx_item->action);
142}
143
144int rna_BlendImportContextItem_import_info_get(PointerRNA *ptr)
145{
147 ptr->data);
148 return int(ctx_item->tag);
149}
150
151PointerRNA rna_BlendImportContextItem_id_get(PointerRNA *ptr)
152{
154 ptr->data);
155 return RNA_id_pointer_create(ctx_item->new_id);
156}
157
158PointerRNA rna_BlendImportContextItem_source_library_get(PointerRNA *ptr)
159{
161 ptr->data);
162 return RNA_id_pointer_create(&ctx_item->source_library->id);
163}
164
165PointerRNA rna_BlendImportContextItem_library_override_id_get(PointerRNA *ptr)
166{
168 ptr->data);
169 return RNA_id_pointer_create(ctx_item->liboverride_id);
170}
171
172PointerRNA rna_BlendImportContextItem_reusable_local_id_get(PointerRNA *ptr)
173{
175 ptr->data);
176 return RNA_id_pointer_create(ctx_item->reusable_local_id);
177}
178
179struct RNABlendImportContextItemsIterator {
180 BlendfileLinkAppendContext *ctx;
181 BlendfileLinkAppendContext::items_iterator_t iter;
182};
183
184void rna_BlendImportContext_import_items_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
185{
186 BlendfileLinkAppendContext *ctx = static_cast<BlendfileLinkAppendContext *>(ptr->data);
187
188 RNABlendImportContextItemsIterator *items_iter = MEM_new<RNABlendImportContextItemsIterator>(
189 __func__, RNABlendImportContextItemsIterator{ctx, ctx->items.begin()});
190 iter->internal.custom = items_iter;
191 iter->valid = (items_iter->iter != items_iter->ctx->items.end());
192}
193
194void rna_BlendImportContext_import_items_next(CollectionPropertyIterator *iter)
195{
196 RNABlendImportContextItemsIterator *items_iter =
197 static_cast<RNABlendImportContextItemsIterator *>(iter->internal.custom);
198 items_iter->iter++;
199 iter->valid = (items_iter->iter != items_iter->ctx->items.end());
200}
201
202void rna_BlendImportContext_import_items_end(CollectionPropertyIterator *iter)
203{
204 RNABlendImportContextItemsIterator *items_iter =
205 static_cast<RNABlendImportContextItemsIterator *>(iter->internal.custom);
206
207 iter->valid = false;
208 iter->internal.custom = nullptr;
209 MEM_delete(items_iter);
210}
211
212PointerRNA rna_BlendImportContext_import_items_get(CollectionPropertyIterator *iter)
213{
214 RNABlendImportContextItemsIterator *items_iter =
215 static_cast<RNABlendImportContextItemsIterator *>(iter->internal.custom);
216
217 BlendfileLinkAppendContextItem &ctx_item = *items_iter->iter;
218 return RNA_pointer_create_with_parent(iter->parent, RNA_BlendImportContextItem, &ctx_item);
219}
220
221int rna_BlendImportContext_import_items_len(PointerRNA *ptr)
222{
223 BlendfileLinkAppendContext *ctx = static_cast<BlendfileLinkAppendContext *>(ptr->data);
224 return int(ctx->items.size());
225}
226
227int rna_BlendImportContext_options_get(PointerRNA *ptr)
228{
229 BlendfileLinkAppendContext *ctx = static_cast<BlendfileLinkAppendContext *>(ptr->data);
230 return ctx->params->flag;
231}
232
233int rna_BlendImportContext_process_stage_get(PointerRNA *ptr)
234{
235 BlendfileLinkAppendContext *ctx = static_cast<BlendfileLinkAppendContext *>(ptr->data);
236 return int(ctx->process_stage);
237}
238
239} // namespace blender
240
241#else /* RNA_RUNTIME */
242
243namespace blender {
244
246{
247 StructRNA *srna;
248 PropertyRNA *prop;
249
250 srna = RNA_def_struct(brna, "BlendImportContextLibrary", nullptr);
252 srna,
253 "BlendFile Import Context Library",
254 "Library (blend-file) reference in a BlendImportContext data. Currently only "
255 "exposed as read-only data for the pre/post blend-import handlers");
256
257 RNA_define_verify_sdna(false); /* not in sdna */
258
259 prop = RNA_def_property(srna, "filepath", PROP_STRING, PROP_FILEPATH);
262 "rna_BlendImportContextLibrary_filepath_get",
263 "rna_BlendImportContextLibrary_filepath_len",
264 nullptr);
265
266 RNA_define_verify_sdna(true); /* not in sdna */
267}
268
270{
271 StructRNA *srna;
272
273 RNA_def_property_srna(cprop, "BlendImportContextLibraries");
274 srna = RNA_def_struct(brna, "BlendImportContextLibraries", nullptr);
276 "Blendfile Import Context Libraries",
277 "Collection of source libraries, i.e. blendfile paths");
278}
279
281{
282 StructRNA *srna;
283 PropertyRNA *prop;
284
285 srna = RNA_def_struct(brna, "BlendImportContextItem", nullptr);
287 srna,
288 "Blendfile Import Context Item",
289 "An item (representing a data-block) in a BlendImportContext data. Currently only "
290 "exposed as read-only data for the pre/post linking handlers");
291
292 RNA_define_verify_sdna(false); /* not in sdna */
293
294 prop = RNA_def_property(srna, "name", PROP_STRING, PROP_NONE);
296 RNA_def_property_ui_text(prop, "ID Name", "ID name of the item");
298 prop, "rna_BlendImportContextItem_name_get", "rna_BlendImportContextItem_name_len", nullptr);
299
300 prop = RNA_def_property(srna, "id_type", PROP_ENUM, PROP_NONE);
303 RNA_def_property_ui_text(prop, "ID Type", "ID type of the item");
305 RNA_def_property_enum_funcs(prop, "rna_BlendImportContextItem_id_type_get", nullptr, nullptr);
306
307 prop = RNA_def_property(srna, "source_libraries", PROP_COLLECTION, PROP_NONE);
308 RNA_def_property_struct_type(prop, "BlendImportContextLibrary");
311 "Source Libraries",
312 "List of libraries to search and import that ID from. The ID will be "
313 "imported from the first file in that list that contains it");
315 "rna_BlendImportContextItem_libraries_begin",
316 "rna_BlendImportContextItem_libraries_next",
317 "rna_BlendImportContextItem_libraries_end",
318 "rna_BlendImportContextItem_libraries_get",
319 "rna_BlendImportContextItem_libraries_len",
320 nullptr,
321 nullptr,
322 nullptr);
324
325 static const EnumPropertyItem blend_import_item_append_action_items[] = {
326 {LINK_APPEND_ACT_UNSET, "UNSET", 0, "", "Not yet defined"},
327 {LINK_APPEND_ACT_KEEP_LINKED, "KEEP_LINKED", 0, "", "ID has been kept linked"},
329 "REUSE_LOCAL",
330 0,
331 "",
332 "An existing matching local ID has been re-used"},
333 {LINK_APPEND_ACT_MAKE_LOCAL, "MAKE_LOCAL", 0, "", "The newly linked ID has been made local"},
335 "COPY_LOCAL",
336 0,
337 "",
338 "The linked ID had other unrelated usages, so it has been duplicated into a local copy"},
339 {0, nullptr, 0, nullptr, nullptr},
340 };
341 prop = RNA_def_property(srna, "append_action", PROP_ENUM, PROP_NONE);
342 RNA_def_property_enum_items(prop, blend_import_item_append_action_items);
345 "Append Action",
346 "How this item has been handled by the append operation. Only set if "
347 "the data has been appended");
349 prop, "rna_BlendImportContextItem_append_action_get", nullptr, nullptr);
350
351 static const EnumPropertyItem blend_import_item_import_info_items[] = {
353 "INDIRECT_USAGE",
354 0,
355 "",
356 "That item was added for an indirectly imported ID, as a dependency of another data-block"},
358 "LIBOVERRIDE_DEPENDENCY",
359 0,
360 "",
361 "That item represents an ID also used as liboverride dependency (either directly, as a "
362 "liboverride reference, or indirectly, as data used by a liboverride reference). It should "
363 "never be directly made local. Mutually exclusive with `LIBOVERRIDE_DEPENDENCY_ONLY`"},
365 "LIBOVERRIDE_DEPENDENCY_ONLY",
366 0,
367 "",
368 "That item represents an ID only used as liboverride dependency (either directly or "
369 "indirectly, see `LIBOVERRIDE_DEPENDENCY` for precisions). It should not be considered "
370 "during the 'make local' (append) process, and remain purely linked data. Mutually "
371 "exclusive with `LIBOVERRIDE_DEPENDENCY`"},
372 {0, nullptr, 0, nullptr, nullptr},
373 };
374 prop = RNA_def_property(srna, "import_info", PROP_ENUM, PROP_NONE);
377 RNA_def_property_enum_items(prop, blend_import_item_import_info_items);
379 prop, "Import Info", "Various status info about an item after it has been imported");
381 prop, "rna_BlendImportContextItem_import_info_get", nullptr, nullptr);
382
383 prop = RNA_def_property(srna, "id", PROP_POINTER, PROP_NONE);
387 "Imported ID",
388 "The imported ID. None until it has been linked or appended. "
389 "May be the same as ``reusable_local_id`` when appended");
391 prop, "rna_BlendImportContextItem_id_get", nullptr, nullptr, nullptr);
392
393 prop = RNA_def_property(srna, "source_library", PROP_POINTER, PROP_NONE);
394 RNA_def_property_struct_type(prop, "Library");
397 "Source Library",
398 "Library ID representing the blendfile from which the ID was imported. "
399 "None until the ID has been linked or appended");
401 prop, "rna_BlendImportContextItem_source_library_get", nullptr, nullptr, nullptr);
402
403 prop = RNA_def_property(srna, "library_override_id", PROP_POINTER, PROP_NONE);
407 prop,
408 "Library Overridden ID",
409 "The library override of the linked ID. None until it has been created");
411 prop, "rna_BlendImportContextItem_library_override_id_get", nullptr, nullptr, nullptr);
412
413 prop = RNA_def_property(srna, "reusable_local_id", PROP_POINTER, PROP_NONE);
417 "Reusable Local ID",
418 "The already existing local ID that may be reused in append & reuse "
419 "case. None until it has been found");
421 prop, "rna_BlendImportContextItem_reusable_local_id_get", nullptr, nullptr, nullptr);
422
423 RNA_define_verify_sdna(true); /* not in sdna */
424}
425
427{
428 StructRNA *srna;
429
430 RNA_def_property_srna(cprop, "BlendImportContextItems");
431 srna = RNA_def_struct(brna, "BlendImportContextItems", nullptr);
433 srna, "Blendfile Import Context Items", "Collection of blendfile import context items");
434
435 /* TODO: Add/Remove items _before_ doing link/append (i.e. for 'pre' handlers). */
436}
437
439{
440 StructRNA *srna;
441 PropertyRNA *prop;
442
443 srna = RNA_def_struct(brna, "BlendImportContext", nullptr);
445 srna,
446 "Blendfile Import Context",
447 "Contextual data for a blendfile library/linked-data related operation. Currently "
448 "only exposed as read-only data for the pre/post blendimport handlers");
449
450 RNA_define_verify_sdna(false); /* not in sdna */
451
452 /* NOTE: Cannot use just `items` here as this is a reserved Python dict method name. */
453 prop = RNA_def_property(srna, "import_items", PROP_COLLECTION, PROP_NONE);
454 RNA_def_property_struct_type(prop, "BlendImportContextItem");
457 "rna_BlendImportContext_import_items_begin",
458 "rna_BlendImportContext_import_items_next",
459 "rna_BlendImportContext_import_items_end",
460 "rna_BlendImportContext_import_items_get",
461 "rna_BlendImportContext_import_items_len",
462 nullptr,
463 nullptr,
464 nullptr);
466
467 static const EnumPropertyItem blend_import_options_items[] = {
468 {FILE_LINK, "LINK", 0, "", "Only link data, instead of appending it"},
470 "MAKE_PATHS_RELATIVE",
471 0,
472 "",
473 "Make paths of used library blendfiles relative to current blendfile"},
475 "USE_PLACEHOLDERS",
476 0,
477 "",
478 "Generate a placeholder (empty ID) if not found in any library files"},
480 "FORCE_INDIRECT",
481 0,
482 "",
483 "Force loaded ID to be tagged as indirectly linked (used in reload context only)"},
485 "APPEND_SET_FAKEUSER",
486 0,
487 "",
488 "Set fake user on appended IDs"},
490 "APPEND_RECURSIVE",
491 0,
492 "",
493 "Append (make local) also indirect dependencies of appended IDs coming from other "
494 "libraries. NOTE: All IDs (including indirectly linked ones) coming from the same initial "
495 "library are always made local"},
497 "APPEND_LOCAL_ID_REUSE",
498 0,
499 "",
500 "Try to re-use previously appended matching IDs when appending them again, instead of "
501 "creating local duplicates"},
503 "APPEND_ASSET_DATA_CLEAR",
504 0,
505 "",
506 "Clear the asset data on append (it is always kept for linked data)"},
507 {FILE_AUTOSELECT, "SELECT_OBJECTS", 0, "", "Automatically select imported objects"},
509 "USE_ACTIVE_COLLECTION",
510 0,
511 "",
512 "Use the active Collection of the current View Layer to instantiate imported "
513 "collections and objects"},
515 "OBDATA_INSTANCE",
516 0,
517 "",
518 "Instantiate object data IDs (i.e. create objects for them if needed)"},
520 "COLLECTION_INSTANCE",
521 0,
522 "",
523 "Instantiate collections as empties, instead of linking them into the current view layer"},
524 {0, nullptr, 0, nullptr, nullptr},
525 };
526 prop = RNA_def_property(srna, "options", PROP_ENUM, PROP_NONE);
529 RNA_def_property_enum_items(prop, blend_import_options_items);
530 RNA_def_property_enum_funcs(prop, "rna_BlendImportContext_options_get", nullptr, nullptr);
531 RNA_def_property_ui_text(prop, "", "Options for this blendfile import operation");
532
533 /* NOTE: Only stages currently exposed to handlers are listed here. */
534 static const EnumPropertyItem blend_import_process_stage_items[] = {
536 "INIT",
537 0,
538 "",
539 "Blendfile import context has been initialized and filled with a list of items to import, "
540 "no data has been linked or appended yet"},
542 "DONE",
543 0,
544 "",
545 "All data has been imported and is available in the list of \"import_items\""},
546 {0, nullptr, 0, nullptr, nullptr},
547 };
548 prop = RNA_def_property(srna, "process_stage", PROP_ENUM, PROP_NONE);
549 RNA_def_property_enum_items(prop, blend_import_process_stage_items);
551 RNA_def_property_ui_text(prop, "", "Current stage of the import process");
552 RNA_def_property_enum_funcs(prop, "rna_BlendImportContext_process_stage_get", nullptr, nullptr);
553
554 RNA_define_verify_sdna(true); /* not in sdna */
555}
556
563
564} // namespace blender
565
566#endif /* RNA_RUNTIME */
external readfile function prototypes.
#define BLT_I18NCONTEXT_ID_ID
BMesh Iterators.
void RNA_def_property_srna(PropertyRNA *prop, const char *type)
void RNA_def_struct_ui_text(StructRNA *srna, const char *name, const char *description)
void RNA_def_property_ui_text(PropertyRNA *prop, const char *name, const char *description)
const EnumPropertyItem rna_enum_id_type_items[]
Definition rna_ID.cc:33
void RNA_def_property_pointer_funcs(PropertyRNA *prop, const char *get, const char *set, const char *type_fn, const char *poll)
PointerRNA RNA_id_pointer_create(ID *id)
void RNA_def_property_struct_type(PropertyRNA *prop, const char *type)
@ BLO_LIBLINK_APPEND_LOCAL_ID_REUSE
@ BLO_LIBLINK_APPEND_RECURSIVE
@ BLO_LIBLINK_APPEND_SET_FAKEUSER
@ BLO_LIBLINK_FORCE_INDIRECT
@ BLO_LIBLINK_OBDATA_INSTANCE
@ BLO_LIBLINK_APPEND_ASSET_DATA_CLEAR
@ BLO_LIBLINK_USE_PLACEHOLDERS
@ BLO_LIBLINK_COLLECTION_INSTANCE
static void rna_def_blendfile_import_context(BlenderRNA *brna)
static void rna_def_blendfile_import_items(BlenderRNA *brna, PropertyRNA *cprop)
PropertyRNA * RNA_def_property(StructOrFunctionRNA *cont_, const char *identifier_c_str, int type, int subtype)
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
void RNA_def_property_enum_items(PropertyRNA *prop, const EnumPropertyItem *item)
@ PROP_POINTER
Definition RNA_types.hh:215
@ PROP_STRING
Definition RNA_types.hh:213
@ PROP_COLLECTION
Definition RNA_types.hh:216
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)
@ FILE_ACTIVE_COLLECTION
static void rna_def_blendfile_import_item(BlenderRNA *brna)
static void RNA_def_blendfile_import_libraries(BlenderRNA *brna, PropertyRNA *cprop)
void RNA_def_property_enum_funcs(PropertyRNA *prop, const char *get, const char *set, const char *item)
void RNA_def_property_translation_context(PropertyRNA *prop, const char *context)
void RNA_def_property_clear_flag(PropertyRNA *prop, PropertyFlag flag)
void RNA_def_property_flag(PropertyRNA *prop, PropertyFlag flag)
StructRNA * RNA_def_struct(BlenderRNA *brna, const char *identifier, const char *from)
@ PROP_EDITABLE
Definition RNA_types.hh:376
@ PROP_ENUM_FLAG
Definition RNA_types.hh:474
@ LINK_APPEND_TAG_LIBOVERRIDE_DEPENDENCY_ONLY
static void rna_def_blendfile_import_library(BlenderRNA *brna)
PointerRNA RNA_pointer_create_with_parent(const PointerRNA &parent, StructRNA *type, void *data)
void RNA_def_property_string_funcs(PropertyRNA *prop, const char *get, const char *length, const char *set)
void RNA_define_verify_sdna(bool verify)
@ PROP_FILEPATH
Definition RNA_types.hh:284
void RNA_def_blendfile_import(BlenderRNA *brna)