Blender V5.3
wm_files_link.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2007 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#include <cctype>
12#include <cerrno>
13#include <cfloat>
14#include <cstring>
15
16#include "CLG_log.h"
17
18#include "DNA_ID.h"
19#include "DNA_scene_types.h"
20#include "DNA_screen_types.h"
22
23#include "BLI_fileops.hh"
24#include "BLI_map.hh"
25#include "BLI_path_utils.hh"
26#include "BLI_string.hh"
27
28#include "BLO_readfile.hh"
29
30#include "BKE_armature.hh"
31#include "BKE_blendfile.hh"
33#include "BKE_context.hh"
34#include "BKE_global.hh"
35#include "BKE_key.hh"
36#include "BKE_layer.hh"
37#include "BKE_lib_id.hh"
38#include "BKE_lib_override.hh"
39#include "BKE_lib_query.hh"
40#include "BKE_lib_remap.hh"
41#include "BKE_library.hh"
42#include "BKE_main.hh"
43#include "BKE_material.hh"
44#include "BKE_object.hh"
45#include "BKE_report.hh"
46#include "BKE_rigidbody.h"
47#include "BKE_scene.hh"
48
49#include "BKE_idtype.hh"
50
51#include "DEG_depsgraph.hh"
53
55
56#include "ED_datafiles.h"
57#include "ED_screen.hh"
58
59#include "RNA_access.hh"
60#include "RNA_define.hh"
61
62#include "WM_api.hh"
63#include "WM_types.hh"
64
65#include "wm_files.hh"
66
67namespace blender {
68
69static CLG_LogRef LOG = {"blend.link"};
70
71/* -------------------------------------------------------------------- */
74
76{
78 /* NOTE(@sergey): Linking changes active object which is pretty useful in general,
79 * but which totally confuses edit mode (i.e. it becoming not so obvious
80 * to leave from edit mode and invalid tools in toolbar might be displayed)
81 * so disable link/append when in edit mode. */
83 return false;
84 }
85
86 return true;
87 }
88
89 return false;
90}
91
93 wmOperator *op,
94 const wmEvent * /*event*/)
95{
96 if (!RNA_struct_property_is_set(op->ptr, "filepath")) {
97 const char *blendfile_path = BKE_main_blendfile_path_from_global();
98 if (G.filepath_last_library[0] != '\0') {
99 RNA_string_set(op->ptr, "filepath", G.filepath_last_library);
100 }
101 else if (blendfile_path[0] != '\0') {
102 char dirpath[FILE_MAX];
103 BLI_path_split_dir_part(blendfile_path, dirpath, sizeof(dirpath));
104 RNA_string_set(op->ptr, "filepath", dirpath);
105 }
106 }
107
110}
111
113{
114 /* NOTE: most options exposed here should also be available in the Python API
115 * unless there are good reasons to have them only exposed to the operator.
116 * see `bpy_lib_load`. */
117 PropertyRNA *prop;
118 int flag = 0;
119
120 if (RNA_boolean_get(op->ptr, "autoselect")) {
121 flag |= FILE_AUTOSELECT;
122 }
123 if (RNA_boolean_get(op->ptr, "active_collection")) {
125 }
126 if ((prop = RNA_struct_find_property(op->ptr, "relative_path")) &&
127 RNA_property_boolean_get(op->ptr, prop))
128 {
129 flag |= FILE_RELPATH;
130 }
131 if (RNA_boolean_get(op->ptr, "link")) {
132 flag |= FILE_LINK;
133 }
134 else {
135 if (RNA_boolean_get(op->ptr, "use_recursive")) {
137 }
138 if (RNA_boolean_get(op->ptr, "set_fake")) {
140 }
141 if (RNA_boolean_get(op->ptr, "do_reuse_local_id")) {
143 }
144 if (RNA_boolean_get(op->ptr, "clear_asset_data")) {
146 }
147 }
148 if (RNA_boolean_get(op->ptr, "instance_collections")) {
150 }
151 if (RNA_boolean_get(op->ptr, "instance_object_data")) {
153 }
154
155 return flag;
156}
157
164 const char *filepath,
165 const char *group,
166 const char *name,
167 const bool do_append)
168{
169 short idcode;
170
171 if (!group || !name) {
172 CLOG_WARN(&LOG, "Skipping %s", filepath);
173 return false;
174 }
175
176 idcode = BKE_idtype_idcode_from_name(group);
177
178 if (!BKE_idtype_idcode_is_linkable(idcode) ||
179 (!do_append && BKE_idtype_idcode_is_only_appendable(idcode)))
180 {
181 if (reports) {
182 if (do_append) {
183 BKE_reportf(reports,
185 "Cannot append data-block '%s' of type '%s'",
186 name,
187 group);
188 }
189 else {
190 BKE_reportf(reports,
192 "Cannot link data-block '%s' of type '%s'",
193 name,
194 group);
195 }
196 }
197 return false;
198 }
199
200 return true;
201}
202
204{
205 Main *bmain = CTX_data_main(C);
206 const char *blendfile_path = BKE_main_blendfile_path(bmain);
207 Scene *scene = CTX_data_scene(C);
208 ViewLayer *view_layer = CTX_data_view_layer(C);
209 PropertyRNA *prop;
210 BlendfileLinkAppendContext *lapp_context;
211 char filepath[FILE_MAX_LIBEXTRA], root[FILE_MAX], libname[FILE_MAX_LIBEXTRA], relname[FILE_MAX];
212 char *group, *name;
213 int totfiles = 0;
214
215 RNA_string_get(op->ptr, "filename", relname);
216 RNA_string_get(op->ptr, "directory", root);
217 if (BLI_path_is_rel(root)) {
218 BLI_path_abs(root, blendfile_path);
219 }
220
221 BLI_path_join(filepath, sizeof(filepath), root, relname);
222
223 /* Test if we have a valid data. */
224 const bool is_librarypath_valid = BKE_blendfile_library_path_explode(
225 filepath, libname, &group, &name);
226
227 {
228 if (blendfile_path[0] != '\0') {
229 /* NOTE: Need to also check `filepath`, as typically `libname` is an empty string here
230 * (when trying to append from current file from the file-browser e.g.). */
231 if (BLI_path_cmp(blendfile_path, filepath) == 0 ||
232 BLI_path_cmp(blendfile_path, libname) == 0)
233 {
234 BKE_reportf(op->reports, RPT_ERROR, "'%s': cannot use current file as library", filepath);
235 return OPERATOR_CANCELLED;
236 }
237 }
238 }
239
240 if (!group) {
241 BKE_reportf(op->reports, RPT_ERROR, "'%s': nothing indicated", filepath);
242 return OPERATOR_CANCELLED;
243 }
244 if (!is_librarypath_valid) {
245 BKE_reportf(op->reports, RPT_ERROR, "'%s': not a library", filepath);
246 return OPERATOR_CANCELLED;
247 }
248
249 /* Check if something is indicated for append/link. */
250 prop = RNA_struct_find_property(op->ptr, "files");
251 if (prop) {
252 totfiles = RNA_property_collection_length(op->ptr, prop);
253 if (totfiles == 0) {
254 if (!name) {
255 BKE_reportf(op->reports, RPT_ERROR, "'%s': nothing indicated", filepath);
256 return OPERATOR_CANCELLED;
257 }
258 }
259 }
260 else if (!name) {
261 BKE_reportf(op->reports, RPT_ERROR, "'%s': nothing indicated", filepath);
262 return OPERATOR_CANCELLED;
263 }
264
265 int flag = wm_link_append_flag(op);
266 const bool do_append = (flag & FILE_LINK) == 0;
267
268 /* From here down, no error returns. */
269
270 if (view_layer && RNA_boolean_get(op->ptr, "autoselect")) {
271 BKE_view_layer_base_deselect_all(*bmain, scene, view_layer);
272 }
273
274 /* Sanity checks for flag. */
275 if (scene && scene->id.lib) {
278 "Scene '%s' is linked, instantiation of objects is disabled",
279 scene->id.name + 2);
281 scene = nullptr;
282 }
283
284 /* Tag everything, all untagged data can be made local
285 * its also generally useful to know what is new.
286 *
287 * Take extra care `BKE_main_id_flag_all(bmain, ID_TAG_PRE_EXISTING, false)` is called after! */
289
290 /* We define our working data...
291 * Note that here, each item 'uses' one library, and only one. */
292 LibraryLink_Params lapp_params;
294 &lapp_params, bmain, flag, 0, scene, view_layer, CTX_wm_view3d(C));
295
296 lapp_context = BKE_blendfile_link_append_context_new(&lapp_params);
299
300 if (totfiles != 0) {
301 Map<std::string, int> libraries;
302 int lib_idx = 0;
303
304 RNA_BEGIN (op->ptr, itemptr, "files") {
305 RNA_string_get(&itemptr, "name", relname);
306
307 BLI_path_join(filepath, sizeof(filepath), root, relname);
308
309 if (BKE_blendfile_library_path_explode(filepath, libname, &group, &name)) {
310 if (!wm_link_append_item_poll(nullptr, filepath, group, name, do_append)) {
311 continue;
312 }
313
314 if (libraries.add(libname, lib_idx)) {
315 lib_idx++;
316 BKE_blendfile_link_append_context_library_add(lapp_context, libname, nullptr);
317 }
318 }
319 }
320 RNA_END;
321
322 RNA_BEGIN (op->ptr, itemptr, "files") {
323 RNA_string_get(&itemptr, "name", relname);
324
325 BLI_path_join(filepath, sizeof(filepath), root, relname);
326
327 if (BKE_blendfile_library_path_explode(filepath, libname, &group, &name)) {
329
330 if (!wm_link_append_item_poll(op->reports, filepath, group, name, do_append)) {
331 continue;
332 }
333
334 lib_idx = libraries.lookup(libname);
335
337 lapp_context, name, BKE_idtype_idcode_from_name(group), nullptr);
339 }
340 }
341 RNA_END;
342 }
343 else {
345
346 BKE_blendfile_link_append_context_library_add(lapp_context, libname, nullptr);
348 lapp_context, name, BKE_idtype_idcode_from_name(group), nullptr);
350 }
351
353 /* Early out in case there is nothing to link. */
355 /* Clear pre existing tag. */
357 return OPERATOR_CANCELLED;
358 }
359
361
362 /* XXX We'd need re-entrant locking on Main for this to work... */
363 // BKE_main_lock(bmain);
364
365 BKE_blendfile_link(lapp_context, op->reports);
366
367 // BKE_main_unlock(bmain);
368
369 /* Mark all library linked objects to be updated. */
372
373 /* Append, rather than linking. */
374 if (do_append) {
375 BKE_blendfile_append(lapp_context, op->reports);
376 }
377
378 /* Instantiate loose data in the scene (e.g. add object to the active collection). */
380
382
384
385 /* Important we unset, otherwise these object won't
386 * link into other scenes from this blend file. */
388
389 /* TODO(sergey): Use proper flag for tagging here. */
390
391 /* TODO(dalai): Temporary solution!
392 * Ideally we only need to tag the new objects themselves, not the scene.
393 * This way we'll avoid flush of collection properties
394 * to all objects and limit update to the particular object only.
395 * But afraid first we need to change collection evaluation in DEG
396 * according to depsgraph manifesto. */
397 if (scene) {
398 DEG_id_tag_update(&scene->id, 0);
399 }
400
401 /* Recreate dependency graph to include new objects. */
403
404 /* TODO: align `G.filepath_last_library` with other directory storage
405 * (like last opened image, etc). */
406 STRNCPY(G.filepath_last_library, root);
407
409
410 return OPERATOR_FINISHED;
411}
412
414 const bool is_link,
415 const bool is_relocate)
416{
417 PropertyRNA *prop;
418
419 /* Better not save _any_ settings for this operator. */
420
421 /* Properties. */
422 prop = RNA_def_boolean(ot->srna,
423 "link",
424 is_link || is_relocate,
425 "Link",
426 "Link the objects or data-blocks rather than appending");
428
429 prop = RNA_def_boolean(
430 ot->srna,
431 "do_reuse_local_id",
432 false,
433 "Re-Use Local Data",
434 "Try to re-use previously matching appended data-blocks instead of appending a new copy");
436 prop = RNA_def_boolean(ot->srna,
437 "clear_asset_data",
438 false,
439 "Clear Asset Data",
440 "Don't add asset meta-data or tags from the original data-block");
442
443 prop = RNA_def_boolean(ot->srna, "autoselect", true, "Select", "Select new objects");
445
446 prop = RNA_def_boolean(ot->srna,
447 "active_collection",
448 !is_relocate,
449 "Active Collection",
450 "Put new objects on the active collection");
452
453 /* NOTE: do not force instancing when relocating, as direct data (the selected ID) status should
454 * not change on that regard, and other dependencies would be indirectly linked and therefore
455 * should not require any enforced instancing when linked. */
456 prop = RNA_def_boolean(
457 ot->srna,
458 "instance_collections",
459 is_link && !is_relocate,
460 "Instance Collections",
461 "Create instances for collections, rather than adding them directly to the scene");
463
464 prop = RNA_def_boolean(
465 ot->srna,
466 "instance_object_data",
467 !is_relocate,
468 "Instance Object Data",
469 "Create instances for object data which are not referenced by any objects");
471}
472
496
498{
499 ot->name = "Append";
500 ot->idname = "WM_OT_append";
501 ot->description = "Append from a Library .blend file";
502
506
507 ot->flag = OPTYPE_UNDO;
508
517
518 wm_link_append_properties_common(ot, false, false);
520 "set_fake",
521 false,
522 "Fake User",
523 "Set \"Fake User\" for appended items (except objects and collections)");
525 ot->srna,
526 "use_recursive",
527 true,
528 "Localize All",
529 "Localize all appended data, including those indirectly linked from other libraries");
530}
531
533{
534 Main *bmain = CTX_data_main(C);
535 const char *blendfile_path = BKE_main_blendfile_path(bmain);
536 Scene *scene = CTX_data_scene(C);
537 ViewLayer *view_layer = CTX_data_view_layer(C);
538 BlendfileLinkAppendContext *lapp_context;
539 char filepath[FILE_MAX_LIBEXTRA], root[FILE_MAX], libname[FILE_MAX_LIBEXTRA], relname[FILE_MAX];
540 char *group, *name;
541
542 RNA_string_get(op->ptr, "filename", relname);
543 RNA_string_get(op->ptr, "directory", root);
544 if (BLI_path_is_rel(root)) {
545 BLI_path_abs(root, blendfile_path);
546 }
547
548 BLI_path_join(filepath, sizeof(filepath), root, relname);
549
550 /* Test if we have a valid data. */
551 const bool is_librarypath_valid = BKE_blendfile_library_path_explode(
552 filepath, libname, &group, &name);
553
554 {
555 if (blendfile_path[0] != '\0') {
556 /* NOTE: Need to also check `filepath`, as typically `libname` is an empty string here
557 * (when trying to append from current file from the file-browser e.g.). */
558 if (BLI_path_cmp(blendfile_path, filepath) == 0 ||
559 BLI_path_cmp(blendfile_path, libname) == 0)
560 {
561 BKE_reportf(op->reports, RPT_ERROR, "'%s': cannot use current file as library", filepath);
562 return OPERATOR_CANCELLED;
563 }
564 }
565 }
566
567 if (!is_librarypath_valid) {
568 BKE_reportf(op->reports, RPT_ERROR, "'%s': not a library", filepath);
569 return OPERATOR_CANCELLED;
570 }
571 if (!group || !name) {
572 BKE_reportf(op->reports, RPT_ERROR, "'%s': nothing indicated", filepath);
573 return OPERATOR_CANCELLED;
574 }
575
576 int flag = wm_link_append_flag(op);
577 BLI_assert(flag & FILE_LINK);
578
579 const short id_type_code = BKE_idtype_idcode_from_name(group);
580
581 const int tmp_id_session_uid = RNA_int_get(op->ptr, "id_session_uid");
582 const uint id_session_uid = *reinterpret_cast<const uint *>(&tmp_id_session_uid);
583 /* NOTE: Creating a full ID map for a single lookup is not worth it. */
584 ID *linked_id = BKE_libblock_find_session_uid(bmain, id_session_uid);
585
586 {
587 const char *linked_id_name = BKE_id_name(*linked_id);
588
589 if (!linked_id || !ID_IS_LINKED(linked_id)) {
590 BKE_reportf(op->reports, RPT_ERROR, "No valid existing linked ID given to relocate");
591 return OPERATOR_CANCELLED;
592 }
593 if (GS(linked_id->name) != id_type_code) {
595 RPT_ERROR,
596 "Selected ID '%s' is a %s, cannot be used to relocate existing linked ID '%s' "
597 "which is a %s",
598 name,
599 group,
600 linked_id_name,
601 BKE_idtype_idcode_to_name(GS(linked_id->name)));
602 return OPERATOR_CANCELLED;
603 }
604 if (STREQ(linked_id_name, name) && STREQ(linked_id->lib->runtime->filepath_abs, libname)) {
606 RPT_ERROR,
607 "Selected ID '%s' seems to be the same as the relocated ID '%s', use 'Reload' "
608 "operation instead",
609 name,
610 linked_id_name);
611 return OPERATOR_CANCELLED;
612 }
613 }
614
615 /* From here down, no error returns. */
616
617 if (view_layer && RNA_boolean_get(op->ptr, "autoselect")) {
618 BKE_view_layer_base_deselect_all(*bmain, scene, view_layer);
619 }
620
621 /* Never enforce instantiation of anything when relocating. */
623
624 /* Tag everything, its generally useful to know what is new.
625 *
626 * Take extra care `BKE_main_id_flag_all(bmain, ID_TAG_PRE_EXISTING, false)` is called after! */
628
629 /* We define our working data...
630 * Note that here, each item 'uses' one library, and only one. */
631 LibraryLink_Params lapp_params;
633 &lapp_params, bmain, flag, 0, scene, view_layer, CTX_wm_view3d(C));
634
635 lapp_context = BKE_blendfile_link_append_context_new(&lapp_params);
638
639 BKE_blendfile_link_append_context_library_add(lapp_context, libname, nullptr);
641 lapp_context, name, id_type_code, linked_id);
643
645
646 BKE_blendfile_id_relocate(*lapp_context, op->reports);
647
649
651
652 /* TODO(sergey): Use proper flag for tagging here. */
653
654 /* TODO(dalai): Temporary solution!
655 * Ideally we only need to tag the new objects themselves, not the scene.
656 * This way we'll avoid flush of collection properties
657 * to all objects and limit update to the particular object only.
658 * But afraid first we need to change collection evaluation in DEG
659 * according to depsgraph manifesto. */
660 if (scene) {
661 DEG_id_tag_update(&scene->id, 0);
662 }
663
664 /* Recreate dependency graph to include new objects. */
666
667 /* TODO: align `G.filepath_last_library` with other directory storage
668 * (like last opened image, etc). */
669 STRNCPY(G.filepath_last_library, root);
670
672
673 return OPERATOR_FINISHED;
674}
675
677{
678 ot->name = "Relocate Linked ID";
679 ot->idname = "WM_OT_id_linked_relocate";
680 ot->description =
681 "Relocate a linked ID, i.e. select another ID to link, and remap its local usages to that "
682 "newly linked data-block). Currently only designed as an internal operator, not directly "
683 "exposed to the user";
684
688
690
691 PropertyRNA *prop = RNA_def_int(ot->srna,
692 "id_session_uid",
694 0,
695 INT_MAX,
696 "Linked ID Session UID",
697 "Unique runtime identifier for the linked ID to relocate",
698 0,
699 INT_MAX);
701
710
711 wm_link_append_properties_common(ot, true, true);
712}
713
715
716/* -------------------------------------------------------------------- */
720
722 Scene *scene,
723 ViewLayer *view_layer,
724 View3D *v3d,
725 const char *filepath,
726 const short id_code,
727 const char *id_name,
728 const int flag,
729 ReportList *reports = nullptr)
730{
732 BLI_path_cmp(BKE_main_blendfile_path(bmain), filepath) != 0,
733 "Calling code should ensure it does not attempt to link/append from current blendfile");
734
735 const bool do_pack = (flag & BLO_LIBLINK_PACK) != 0;
736 const bool do_append = (flag & FILE_LINK) == 0;
737 /* Tag everything so we can make local only the new datablock. */
739
740 /* Define working data, with just the one item we want to link. */
741 LibraryLink_Params lapp_params;
742 BLO_library_link_params_init_with_context(&lapp_params, bmain, flag, 0, scene, view_layer, v3d);
743
747
748 BKE_blendfile_link_append_context_library_add(lapp_context, filepath, nullptr);
750 lapp_context, id_name, id_code, nullptr);
752
754
755 /* Link datablock. */
756 BKE_blendfile_link(lapp_context, reports);
757
758 if (do_pack) {
759 BKE_blendfile_link_pack(lapp_context, reports);
760 }
761 else if (do_append) {
762 BKE_blendfile_append(lapp_context, reports);
763 }
764
765 BKE_blendfile_link_append_instantiate_loose(lapp_context, reports);
766
768
769 /* Get linked datablock and free working data. */
771
773
775
776 return id;
777}
778
780 Scene *scene,
781 ViewLayer *view_layer,
782 View3D *v3d,
783 const char *filepath,
784 const short id_code,
785 const char *id_name,
786 int flag,
787 ReportList *reports)
788{
789 flag |= FILE_LINK;
791 bmain, scene, view_layer, v3d, filepath, id_code, id_name, flag, reports);
792}
793
795 Scene *scene,
796 ViewLayer *view_layer,
797 View3D *v3d,
798 const char *filepath,
799 const short id_code,
800 const char *id_name,
801 int flag,
802 ReportList *reports)
803{
804 BLI_assert((flag & FILE_LINK) == 0);
806 bmain, scene, view_layer, v3d, filepath, id_code, id_name, flag, reports);
807}
808
810
811/* -------------------------------------------------------------------- */
814
816 wmOperator *op,
817 const wmEvent * /*event*/)
818{
819 Library *lib;
820 char lib_name[MAX_NAME];
821
822 RNA_string_get(op->ptr, "library", lib_name);
823 lib = id_cast<Library *>(BKE_libblock_find_name(CTX_data_main(C), ID_LI, lib_name));
824
825 if (lib) {
826 if (lib->runtime->parent) {
829 "Cannot relocate indirectly linked library '%s'",
830 lib->runtime->filepath_abs);
831 return OPERATOR_CANCELLED;
832 }
833 RNA_string_set(op->ptr, "filepath", lib->runtime->filepath_abs);
834
836
838 }
839
840 return OPERATOR_CANCELLED;
841}
842
844{
847 reports, RPT_ERROR, "'%s' is not a valid library filepath", lib->runtime->filepath_abs);
848 return;
849 }
850
851 if (!BLI_exists(lib->runtime->filepath_abs)) {
852 BKE_reportf(reports,
853 RPT_ERROR,
854 "Trying to reload library '%s' from invalid path '%s'",
855 lib->id.name,
856 lib->runtime->filepath_abs);
857 return;
858 }
859
860 Main *bmain = CTX_data_main(C);
861 LibraryLink_Params lapp_params;
863 &lapp_params,
864 bmain,
866 0,
869 nullptr);
870
872
874
875 BKE_blendfile_library_relocate(lapp_context, reports, lib, true);
876
878
880
883
884 /* Important we unset, otherwise these object won't link into other scenes from this blend file.
885 */
887
888 /* Recreate dependency graph to include new IDs. */
890
892}
893
895{
896 Main *bmain = CTX_data_main(C);
897 const char *blendfile_path = BKE_main_blendfile_path(bmain);
898 char lib_name[MAX_NAME];
899
900 RNA_string_get(op->ptr, "library", lib_name);
901 Library *lib = id_cast<Library *>(BKE_libblock_find_name(bmain, ID_LI, lib_name));
902 if (lib == nullptr) {
903 return OPERATOR_CANCELLED;
904 }
905
906 PropertyRNA *prop;
907 BlendfileLinkAppendContext *lapp_context;
908
909 char filepath[FILE_MAX], root[FILE_MAX], libname[FILE_MAX], relname[FILE_MAX];
910
911 if (lib->runtime->parent && !do_reload) {
914 "Cannot relocate indirectly linked library '%s'",
915 lib->runtime->filepath_abs);
916 return OPERATOR_CANCELLED;
917 }
918
919 RNA_string_get(op->ptr, "filename", libname);
920 RNA_string_get(op->ptr, "directory", root);
921 if (BLI_path_is_rel(root)) {
922 BLI_path_abs(root, blendfile_path);
923 }
924
925 if (!BKE_blendfile_extension_check(libname)) {
926 BKE_report(op->reports, RPT_ERROR, "Not a library");
927 return OPERATOR_CANCELLED;
928 }
929
930 BLI_path_join(filepath, sizeof(filepath), root, libname);
931
932 if (!BLI_exists(filepath)) {
935 "Trying to reload or relocate library '%s' to invalid path '%s'",
936 lib->id.name,
937 filepath);
938 return OPERATOR_CANCELLED;
939 }
940
941 {
942 if ((blendfile_path[0] != '\0') && (BLI_path_cmp(blendfile_path, filepath) == 0)) {
945 "Cannot relocate library '%s' to current blend file '%s'",
946 lib->id.name,
947 filepath);
948 return OPERATOR_CANCELLED;
949 }
950 }
951
952 int flag = FILE_LINK;
953 if (RNA_boolean_get(op->ptr, "relative_path")) {
954 flag |= FILE_RELPATH;
955 }
957 if (do_reload) {
959 }
960
961 LibraryLink_Params lapp_params;
963 &lapp_params, bmain, flag, 0, CTX_data_scene(C), CTX_data_view_layer(C), nullptr);
964
965 if (BLI_path_cmp(lib->runtime->filepath_abs, filepath) == 0) {
966 CLOG_DEBUG(&LOG, "We are supposed to reload '%s' lib (%d)", lib->filepath, lib->id.us);
967
968 do_reload = true;
969
970 lapp_context = BKE_blendfile_link_append_context_new(&lapp_params);
971 BKE_blendfile_link_append_context_library_add(lapp_context, filepath, nullptr);
972 }
973 else {
974 int totfiles = 0;
975
977 &LOG, "We are supposed to relocate '%s' lib to new '%s' one", lib->filepath, libname);
978
979 /* Check if something is indicated for relocate. */
980 prop = RNA_struct_find_property(op->ptr, "files");
981 if (prop) {
982 totfiles = RNA_property_collection_length(op->ptr, prop);
983 if (totfiles == 0) {
984 if (!libname[0]) {
985 BKE_report(op->reports, RPT_ERROR, "Nothing indicated");
986 return OPERATOR_CANCELLED;
987 }
988 }
989 }
990
991 lapp_context = BKE_blendfile_link_append_context_new(&lapp_params);
992
993 if (totfiles) {
994 RNA_BEGIN (op->ptr, itemptr, "files") {
995 RNA_string_get(&itemptr, "name", relname);
996
997 BLI_path_join(filepath, sizeof(filepath), root, relname);
998
999 if (BLI_path_cmp(filepath, lib->runtime->filepath_abs) == 0 ||
1001 {
1002 continue;
1003 }
1004
1005 CLOG_DEBUG(&LOG, "\tCandidate new lib to reload data-blocks from: %s", filepath);
1006 BKE_blendfile_link_append_context_library_add(lapp_context, filepath, nullptr);
1007 }
1008 RNA_END;
1009 }
1010 else {
1011 CLOG_DEBUG(&LOG, "\tCandidate new lib to reload data-blocks from: %s", filepath);
1012 BKE_blendfile_link_append_context_library_add(lapp_context, filepath, nullptr);
1013 }
1014 }
1015
1016 BKE_blendfile_library_relocate(lapp_context, op->reports, lib, do_reload);
1017
1019
1021
1022 /* TODO: align `G.filepath_last_library` with other directory storage
1023 * (like last opened image, etc). */
1024 STRNCPY(G.filepath_last_library, root);
1025
1028
1029 /* Important we unset, otherwise these object won't link into other scenes from this blend
1030 * file.
1031 */
1033
1034 /* Recreate dependency graph to include new IDs. */
1036
1038
1039 return OPERATOR_FINISHED;
1040}
1041
1043{
1044 return wm_lib_relocate_exec_do(C, op, false);
1045}
1046
1048{
1049 PropertyRNA *prop;
1050
1051 ot->name = "Relocate Library";
1052 ot->idname = "WM_OT_lib_relocate";
1053 ot->description = "Relocate the given library to one or several others";
1054
1057
1058 ot->flag = OPTYPE_UNDO;
1059
1060 prop = RNA_def_string(ot->srna, "library", nullptr, MAX_NAME, "Library", "Library to relocate");
1062
1071}
1072
1074{
1075 return wm_lib_relocate_exec_do(C, op, true);
1076}
1077
1079{
1080 PropertyRNA *prop;
1081
1082 ot->name = "Reload Library";
1083 ot->idname = "WM_OT_lib_reload";
1084 ot->description = "Reload the given library";
1085
1087
1088 ot->flag = OPTYPE_UNDO;
1089
1090 prop = RNA_def_string(ot->srna, "library", nullptr, MAX_NAME, "Library", "Library to reload");
1092
1101}
1102
1104
1105} // namespace blender
#define MAIN_ID_SESSION_UID_UNSET
General operations, lookup, etc. for materials.
General operations, lookup, etc. for blender objects.
API for Blender-side Rigid Body stuff.
File and directory operations.
#define FILE_MAX
#define BLI_path_join(...)
#define BLI_path_cmp
external readfile function prototypes.
#define CLOG_DEBUG(clg_ref,...)
Definition CLG_log.h:191
#define CLOG_WARN(clg_ref,...)
Definition CLG_log.h:189
ID and Library types, which are fundamental for SDNA.
#define ID_IS_LINKED(_id)
Definition DNA_ID.h:697
#define MAX_NAME
Definition DNA_defs.h:63
#define FILE_MAX_LIBEXTRA
int datatoc_startup_blend_size
const char datatoc_startup_blend[]
#define RNA_BEGIN(sptr, itemptr, propname)
#define RNA_END
#define C
Definition RandGen.cpp:29
#define NC_WINDOW
Definition WM_types.hh:388
#define BLI_assert_msg(a, msg)
#define STREQ(a, b)
bool add(const Key &key, const Value &value)
Definition BLI_map.hh:295
const Value & lookup(const Key &key) const
Definition BLI_map.hh:545
#define INT_MAX
BMesh Iterators.
void BKE_blendfile_link_append_instantiate_loose(BlendfileLinkAppendContext *lapp_context, ReportList *reports)
const char * BKE_idtype_idcode_to_name(short idcode)
Definition idtype.cc:261
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
BlendfileLinkAppendContext * BKE_blendfile_link_append_context_new(LibraryLink_Params *params)
char * STRNCPY(char(&dst)[N], const char *src)
void BLO_library_link_params_init_with_context(LibraryLink_Params *params, Main *bmain, int flag, int id_tag_extra, Scene *scene, ViewLayer *view_layer, const View3D *v3d)
Definition readfile.cc:5758
PropertyRNA * RNA_def_string(StructOrFunctionRNA *cont_, const char *identifier, const char *default_value, const int maxlen, const char *ui_name, const char *ui_description)
ID * BKE_libblock_find_name(Main *bmain, short type, const char *name, const std::optional< Library * > lib=std::nullopt) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition lib_id.cc:1808
@ RPT_ERROR_INVALID_INPUT
Definition BKE_report.hh:42
@ RPT_WARNING
Definition BKE_report.hh:40
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
static bool wm_link_append_poll(bContext *C)
static wmOperatorStatus wm_link_append_exec(bContext *C, wmOperator *op)
unsigned int uint
Definition BKE_text.h:55
static wmOperatorStatus wm_link_append_invoke(bContext *C, wmOperator *op, const wmEvent *)
bool BLI_path_abs(char path[FILE_MAX], const char *basepath) ATTR_NONNULL(1
ID * BKE_libblock_find_session_uid(Main *bmain, short type, uint32_t session_uid)
Definition lib_id.cc:1826
ID * WM_file_link_datablock(Main *bmain, Scene *scene, ViewLayer *view_layer, View3D *v3d, const char *filepath, const short id_code, const char *id_name, int flag, ReportList *reports)
bool BKE_blendfile_library_path_explode(const char *path, char *r_dir, char **r_group, char **r_name)
Definition blendfile.cc:94
void WM_OT_link(wmOperatorType *ot)
static wmOperatorStatus wm_lib_relocate_exec_do(bContext *C, wmOperator *op, bool do_reload)
static wmOperatorStatus wm_lib_relocate_invoke(bContext *C, wmOperator *op, const wmEvent *)
void BKE_blendfile_link_append_context_free(BlendfileLinkAppendContext *lapp_context)
bool BKE_idtype_idcode_is_linkable(short idcode)
Definition idtype.cc:307
void RNA_string_set(PointerRNA *ptr, const char *name, const char *value)
bool BLI_exists(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:404
BlendfileLinkAppendContextItem * BKE_blendfile_link_append_context_item_add(BlendfileLinkAppendContext *lapp_context, const char *idname, short idcode, void *userdata)
static ID * wm_file_link_append_datablock_ex(Main *bmain, Scene *scene, ViewLayer *view_layer, View3D *v3d, const char *filepath, const short id_code, const char *id_name, const int flag, ReportList *reports=nullptr)
int RNA_int_get(PointerRNA *ptr, const char *name)
void BKE_blendfile_link_append_context_item_library_index_enable(BlendfileLinkAppendContext *lapp_context, BlendfileLinkAppendContextItem *item, int library_index)
const char * BKE_main_blendfile_path_from_global()
Definition main.cc:1063
void WM_OT_lib_reload(wmOperatorType *ot)
void BKE_blendfile_id_relocate(BlendfileLinkAppendContext &lapp_context, ReportList *reports)
int RNA_property_collection_length(PointerRNA *ptr, PropertyRNA *prop)
const char * BKE_id_name(const ID &id)
static bool wm_link_append_item_poll(ReportList *reports, const char *filepath, const char *group, const char *name, const bool do_append)
void WM_OT_append(wmOperatorType *ot)
@ 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
void WM_operator_properties_filesel(wmOperatorType *ot, const int filter, const short type, const eFileSel_Action action, const eFileSel_Flag flag, const short display, const short sort)
ViewLayer * CTX_data_view_layer(const bContext *C)
static wmOperatorStatus wm_id_linked_relocate_exec(bContext *C, wmOperator *op)
ID * BKE_blendfile_link_append_context_item_newid_get(BlendfileLinkAppendContext *lapp_context, BlendfileLinkAppendContextItem *item)
bool RNA_property_boolean_get(PointerRNA *ptr, PropertyRNA *prop)
Global G
Definition blender.cc:57
const char * BKE_main_blendfile_path(const Main *bmain) ATTR_NONNULL()
Definition main.cc:1058
static void wm_link_append_properties_common(wmOperatorType *ot, const bool is_link, const bool is_relocate)
void void BLI_path_split_dir_part(const char *filepath, char *dir, size_t dir_maxncpy) ATTR_NONNULL(1
void DEG_id_tag_update(ID *id, unsigned int flags)
void WM_OT_id_linked_relocate(wmOperatorType *ot)
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
void BKE_blendfile_link_append_context_embedded_blendfile_set(BlendfileLinkAppendContext *lapp_context, const void *blendfile_mem, int blendfile_memsize)
@ WM_FILESEL_DIRECTORY
Definition WM_api.hh:1330
@ WM_FILESEL_RELPATH
Definition WM_api.hh:1329
@ WM_FILESEL_SHOW_PROPS
Definition WM_api.hh:1335
@ WM_FILESEL_FILES
Definition WM_api.hh:1333
@ WM_FILESEL_FILENAME
Definition WM_api.hh:1331
@ WM_FILESEL_FILEPATH
Definition WM_api.hh:1332
Object * CTX_data_edit_object(const bContext *C)
bool BKE_blendfile_link_append_context_is_empty(BlendfileLinkAppendContext *lapp_context)
@ FILE_ACTIVE_COLLECTION
void BKE_blendfile_link_append_context_finalize(BlendfileLinkAppendContext *lapp_context)
bool RNA_struct_property_is_set(PointerRNA *ptr, const char *identifier)
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
void BKE_blendfile_append(BlendfileLinkAppendContext *lapp_context, ReportList *reports)
bool BKE_idtype_idcode_is_only_appendable(short idcode)
Definition idtype.cc:314
void WM_lib_reload(Library *lib, bContext *C, ReportList *reports)
void BKE_main_id_tag_all(Main *mainvar, int tag, bool value)
Definition lib_id.cc:1314
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
void BKE_view_layer_base_deselect_all(const Main &bmain, const Scene *scene, ViewLayer *view_layer)
ID * WM_file_append_datablock(Main *bmain, Scene *scene, ViewLayer *view_layer, View3D *v3d, const char *filepath, const short id_code, const char *id_name, int flag, ReportList *reports)
static wmOperatorStatus wm_lib_reload_exec(bContext *C, wmOperator *op)
void RNA_def_property_flag(PropertyRNA *prop, PropertyFlag flag)
PropertyRNA * RNA_def_int(StructOrFunctionRNA *cont_, const char *identifier, const int default_value, const int hardmin, const int hardmax, const char *ui_name, const char *ui_description, const int softmin, const int softmax)
void BKE_blendfile_link_pack(BlendfileLinkAppendContext *lapp_context, ReportList *reports)
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
Definition report.cc:191
@ FILE_OPENFILE
Definition WM_api.hh:1341
bool WM_operator_winactive(bContext *C)
static wmOperatorStatus wm_lib_relocate_exec(bContext *C, wmOperator *op)
Main * CTX_data_main(const bContext *C)
void BKE_blendfile_library_relocate(BlendfileLinkAppendContext *lapp_context, ReportList *reports, Library *library, bool do_reload)
void BKE_blendfile_link_append_context_library_add(BlendfileLinkAppendContext *lapp_context, const char *libname, BlendHandle *blo_handle)
bool BLI_path_is_rel(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
ID_Type GS(const char *name)
Definition DNA_ID.h:410
@ PROP_SKIP_SAVE
Definition RNA_types.hh:414
@ PROP_HIDDEN
Definition RNA_types.hh:408
static int wm_link_append_flag(wmOperator *op)
@ ID_TAG_PRE_EXISTING
Definition DNA_ID.h:929
bool BKE_blendfile_extension_check(const char *str)
Definition blendfile.cc:88
static CLG_LogRef LOG
Definition logging.cpp:12
void BKE_main_lib_objects_recalc_all(Main *bmain)
Definition lib_id.cc:1379
void DEG_relations_tag_update(Main *bmain)
void BKE_blendfile_link(BlendfileLinkAppendContext *lapp_context, ReportList *reports)
Scene * CTX_data_scene(const bContext *C)
short BKE_idtype_idcode_from_name(const char *idtype_name)
Definition idtype.cc:282
@ OPTYPE_REGISTER
Definition WM_types.hh:188
@ OPTYPE_UNDO
Definition WM_types.hh:190
@ OPTYPE_INTERNAL
Definition WM_types.hh:210
std::string RNA_string_get(PointerRNA *ptr, const char *name)
PropertyRNA * RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, const bool default_value, const char *ui_name, const char *ui_description)
void IMB_colormanagement_check_file_config(Main *bmain)
void BKE_blendfile_link_append_context_init_done(BlendfileLinkAppendContext *lapp_context)
void WM_OT_lib_relocate(wmOperatorType *ot)
View3D * CTX_wm_view3d(const bContext *C)
void WM_event_add_fileselect(bContext *C, wmOperator *op)
char name[258]
Definition DNA_ID.h:433
struct Library * lib
Definition DNA_ID.h:421
char filepath[1024]
Definition DNA_ID.h:562
bke::library::LibraryRuntime * runtime
Definition DNA_ID.h:591
wmOperatorStatus(*) exec(bContext *C, wmOperator *op) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1125
const char * description
Definition WM_types.hh:1115
bool(*) poll(bContext *C) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1165
wmOperatorStatus(*) invoke(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1141
struct ReportList * reports