[Bf-blender-cvs] [98efbb3] id-remap: Merge branch 'lib-link-rework-temp' into id-remap

Bastien Montagne noreply at git.blender.org
Fri Sep 18 22:42:40 CEST 2015


Commit: 98efbb312c53eb031f775f38c96787985693d04e
Author: Bastien Montagne
Date:   Fri Sep 18 22:42:05 2015 +0200
Branches: id-remap
https://developer.blender.org/rB98efbb312c53eb031f775f38c96787985693d04e

Merge branch 'lib-link-rework-temp' into id-remap

Also, fix some issues in previous code.

===================================================================



===================================================================

diff --cc source/blender/windowmanager/intern/wm_operators.c
index 7594cac,b8cb56d..1908610
--- a/source/blender/windowmanager/intern/wm_operators.c
+++ b/source/blender/windowmanager/intern/wm_operators.c
@@@ -2662,12 -2682,11 +2682,11 @@@ static void wm_link_do
  	BlendHandle *bh;
  	Library *lib;
  
 -	const int flag = lapp_data->flag;
 +	const short flag = lapp_data->flag;
  
- 	BLI_bitmap *done_items = BLI_BITMAP_NEW(lapp_data->num_items, __func__);
- 	char (*libname)[FILE_MAX];
+ 	BLI_bitmap *done_items = BLI_BITMAP_NEW_ALLOCA(lapp_data->num_items);
+ 	LinkNode *liblink, *itemlink;
  	int lib_idx, item_idx;
- 	int idcode;
  
  	BLI_assert(lapp_data->num_items && lapp_data->num_libraries);
  
@@@ -2964,253 -2974,6 +2974,213 @@@ static void WM_OT_append(wmOperatorTyp
  	wm_link_append_properties_common(ot, false);
  }
  
 +
 +static int wm_lib_relocate_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
 +{
 +	Library *lib;
 +	char lib_name[MAX_ID_NAME];
 +
 +	RNA_string_get(op->ptr, "library", lib_name);
 +	lib = (Library *)BKE_libblock_find_name_ex(CTX_data_main(C), ID_LI, lib_name + 2);
 +
 +	if (lib) {
 +		if (lib->parent) {
 +			BKE_reportf(op->reports, RPT_ERROR_INVALID_INPUT,
 +			            "Cannot relocate indirectly linked library '%s'", lib->filepath);
 +			return OPERATOR_CANCELLED;
 +		}
 +		RNA_string_set(op->ptr, "filepath", lib->filepath);
 +
 +		WM_event_add_fileselect(C, op);
 +
 +		return OPERATOR_RUNNING_MODAL;
 +	}
 +
 +	return OPERATOR_CANCELLED;
 +}
 +
 +static int wm_lib_relocate_exec(bContext *C, wmOperator *op)
 +{
 +	Library *lib;
 +	char lib_name[MAX_ID_NAME];
 +
 +	RNA_string_get(op->ptr, "library", lib_name);
 +	lib = (Library *)BKE_libblock_find_name_ex(CTX_data_main(C), ID_LI, lib_name + 2);
 +
 +	if (lib) {
 +		Main *bmain = CTX_data_main(C);
 +		Scene *scene = CTX_data_scene(C);
 +		PropertyRNA *prop;
- 		WMLinkAppendData lapp_data;
++		WMLinkAppendData *lapp_data;
 +		char path[FILE_MAX], root[FILE_MAXDIR], libname[FILE_MAX], relname[FILE_MAX];
 +		short flag = 0;
 +
 +		if (RNA_boolean_get(op->ptr, "relative_path")) {
 +			flag |= FILE_RELPATH;
 +		}
 +
 +		if (lib->parent) {
 +			BKE_reportf(op->reports, RPT_ERROR_INVALID_INPUT,
 +			            "Cannot relocate indirectly linked library '%s'", lib->filepath);
 +			return OPERATOR_CANCELLED;
 +		}
 +
 +		RNA_string_get(op->ptr, "directory", root);
 +		RNA_string_get(op->ptr, "filename", libname);
 +
 +		if (!BLO_has_bfile_extension(libname)) {
 +			BKE_report(op->reports, RPT_ERROR, "Not a library");
 +			return OPERATOR_CANCELLED;
 +		}
 +
 +		if (BLI_path_cmp(lib->filepath, libname) == 0) {
 +			printf("We are supposed to reload '%s' lib...\n", lib->filepath);
 +
 +			return OPERATOR_FINISHED;
 +		}
 +		else {
++			LinkNode *itemlink;
 +			ListBase *lbarray[MAX_LIBARRAY];
- 			int lba_idx, lba_size;
- 			int item_idx = 0;
- 			int lib_idx = 0;
++			int lba_idx;
++			int item_idx;
 +			int totfiles = 0;
 +
 +			printf("We are supposed to relocate '%s' lib to new '%s' one...\n", lib->filepath, libname);
 +
 +			/* Check if something is indicated for relocate. */
 +			prop = RNA_struct_find_property(op->ptr, "files");
 +			if (prop) {
 +				totfiles = RNA_property_collection_length(op->ptr, prop);
 +				if (totfiles == 0) {
 +					if (!libname[0]) {
 +						BKE_report(op->reports, RPT_ERROR, "Nothing indicated");
 +						return OPERATOR_CANCELLED;
 +					}
 +				}
 +			}
 +
- 			if (totfiles) {
- 				RNA_BEGIN (op->ptr, itemptr, "files")
- 				{
- 					RNA_string_get(&itemptr, "name", relname);
- 
- 					BLI_join_dirfile(path, sizeof(path), root, relname);
- 
- 					if (BLI_path_cmp(path, lib->filepath) == 0 || !BLO_has_bfile_extension(relname)) {
- 						continue;
- 					}
- 
- 					lib_idx++;
- 				}
- 				RNA_END;
- 			}
- 			else {
- 				lib_idx = 1;
- 			}
- 
- 			BKE_main_lock(bmain);
- 
- 			lba_size = lba_idx = set_listbasepointers(bmain, lbarray);
- 			while (lba_idx--) {
- 				ID *id = lbarray[lba_idx]->first;
- 
- 				if (!id || !BKE_idcode_is_linkable(GS(id->name))) {
- 					continue;
- 				}
- 				for (; id; id = id->next) {
- 					if (id->lib == lib) {
- 						item_idx++;
- 					}
- 				}
- 			}
- 
- 			wm_link_append_data_create(&lapp_data, lib_idx, item_idx, flag);
- 			lib_idx = item_idx = 0;
++			lapp_data = wm_link_append_data_new(flag);
 +
 +			if (totfiles) {
 +				RNA_BEGIN (op->ptr, itemptr, "files")
 +				{
 +					RNA_string_get(&itemptr, "name", relname);
 +
 +					BLI_join_dirfile(path, sizeof(path), root, relname);
 +
 +					if (BLI_path_cmp(path, lib->filepath) == 0 || !BLO_has_bfile_extension(relname)) {
 +						continue;
 +					}
 +
- 					BLI_strncpy(lapp_data.libraries[lib_idx], path, sizeof(*lapp_data.libraries));
 +					printf("\t candidate new lib to reload datablocks from: %s\n", path);
- 					lib_idx++;
++					wm_link_append_data_library_add(lapp_data, path);
 +				}
 +				RNA_END;
 +			}
 +			else {
 +				BLI_join_dirfile(path, sizeof(path), root, libname);
- 				BLI_strncpy(lapp_data.libraries[lib_idx], path, sizeof(*lapp_data.libraries));
 +				printf("\t candidate new lib to reload datablocks from: %s\n", path);
++				wm_link_append_data_library_add(lapp_data, path);
 +			}
 +
- 			for (lba_idx = lba_size; lba_idx--;) {
++			BKE_main_lock(bmain);
++
++			lba_idx = set_listbasepointers(bmain, lbarray);
++			while (lba_idx--) {
 +				ID *id = lbarray[lba_idx]->first;
 +				int idcode = id ? GS(id->name) : 0;
 +
 +				if (!id || !BKE_idcode_is_linkable(idcode)) {
 +					continue;
 +				}
 +				for (; id; id = id->next) {
 +					if (id->lib == lib) {
- #ifndef NDEBUG  /* Not required, but handy for debugging! */
- 						BLI_strncpy(lapp_data.items[item_idx].group, BKE_idcode_to_name(idcode),
- 						            sizeof(lapp_data.items[item_idx].group));
- #endif
- 						BLI_strncpy(lapp_data.items[item_idx].name, id->name, sizeof(lapp_data.items[item_idx].name));
- 						lapp_data.items[item_idx].idcode = idcode;
- 						BLI_BITMAP_SET_ALL(lapp_data.items[item_idx].libs, true, lapp_data.num_libraries);
- 						lapp_data.items[item_idx].data = id;
++						WMLinkAppendDataItem *item = wm_link_append_data_item_add(lapp_data, id->name + 2, idcode, id);
++						BLI_BITMAP_SET_ALL(item->libraries, true, lapp_data->num_libraries);
 +
 +						printf("\tdatablock to seek for: %s\n", id->name);
- 
- 						item_idx++;
 +					}
 +				}
 +			}
 +
 +			BKE_main_id_flag_all(bmain, LIB_PRE_EXISTING, true);
 +
 +			/* We do not want any instanciation here! */
- 			wm_link_do(&lapp_data, op->reports, bmain, NULL, NULL);
++			wm_link_do(lapp_data, op->reports, bmain, NULL, NULL);
 +
- 			for (item_idx = 0; item_idx < lapp_data.num_items; item_idx++) {
- 				ID *old_id = lapp_data.items[item_idx].data;
- 				ID *new_id = lapp_data.items[item_idx].new_id;
++			for (item_idx = 0, itemlink = lapp_data->items; itemlink; item_idx++, itemlink = itemlink->next) {
++				WMLinkAppendDataItem *item = itemlink->link;
++				ID *old_id = item->customdata;
++				ID *new_id = item->new_id;
 +
 +				BLI_assert(old_id);
 +				if (new_id) {
 +					printf("before remap, old_id users: %d, new_id users: %d\n", old_id->us, new_id->us);
 +					BKE_libblock_remap_locked(bmain, old_id, new_id, true);
 +					printf("after remap, old_id users: %d, new_id users: %d\n", old_id->us, new_id->us);
 +				}
 +			}
 +
 +			BKE_main_unlock(bmain);
 +
- 			for (item_idx = 0; item_idx < lapp_data.num_items; item_idx++) {
- 				ID *old_id = lapp_data.items[item_idx].data;
++			for (item_idx = 0, itemlink = lapp_data->items; itemlink; item_idx++, itemlink = itemlink->next) {
++				WMLinkAppendDataItem *item = itemlink->link;
++				ID *old_id = item->customdata;
 +
 +				if (old_id->us == 0) {
 +					BKE_libblock_free(bmain, old_id);
 +				}
 +			}
 +
- 			wm_link_append_data_delete(&lapp_data);
++			wm_link_append_data_free(lapp_data);
 +		}
 +
 +		BKE_main_lib_objects_recalc_all(bmain);
 +		IMB_colormanagement_check_file_config(bmain);
 +
 +		/* important we unset, otherwise these object wont
 +		 * link into other scenes from this blend file */
 +		BKE_main_id_flag_all(bmain, LIB_PRE_EXISTING, false);
 +
 +		/* recreate dependency graph to include new objects */
 +		DAG_scene_relations_rebuild(bmain, scene);
 +
 +		/* free gpu materials, some materials depend on existing objects, such as lamps so freeing correctly refreshes */
 +		GPU_materials_free();
 +
 +		/* XXX TODO: align G.lib with other directory storage (like last opened image etc...) */
 +		BLI_strncpy(G.lib, root, FILE_MAX);
 +
 +		WM_event_add_notifier(C, NC_WINDOW, NULL);
 +
 +		return OPERATOR_FINISHED;
 +	}
 +
 +	return OPERATOR_CANCELLED;
 +}
 +
 +static void WM_OT_lib_relocate(wmOperatorType *ot)
 +{
 +	PropertyRNA *prop;
 +
 +	ot->name = "Relocate Library";
 +	ot->idname = "WM_OT_lib_relocate";
 +	ot->description = "Relocate given library to another one";
 +
 +	ot->invoke = wm_lib_relocate_invoke;
 +	ot->exec = wm_lib_relocate_exec;
 +
 +	ot->flag |= OPTYPE_UNDO;
 +
 +	prop = RNA_def_string(ot->srna, "library", NULL, MAX_ID_NAME, "Library", "Library to relocate");
 +	RNA_def_property_flag(prop, PROP_HIDDEN);
 +
 +	WM_operator_properties_filesel(
 +	            ot, FILE_TYPE_FOLDER | FILE_TYPE_BLENDER, FILE_BLENDER, FILE_OPENFILE,
 +	            WM_FILESEL_FILEPATH | WM_FILESEL_DIRECTORY | WM_FILESEL_FILENAME | WM_FILESEL_FILES | WM_FILESEL_RELPATH,
 +	            FILE_DEFAULTDISPLAY, FILE_SORT_ALPHA);
 +}
 +
  /* *************** recover last session **************** */
  
  void WM_recover_last_session(bContext *C, ReportList *reports)




More information about the Bf-blender-cvs mailing list