[Bf-blender-cvs] [2e8fcac] master: Cleanup & rework of BLO_linking code.

Bastien Montagne noreply at git.blender.org
Mon Oct 12 15:25:26 CEST 2015


Commit: 2e8fcac15db50713ae3552e535be91ba331ad4bc
Author: Bastien Montagne
Date:   Mon Oct 12 15:07:07 2015 +0200
Branches: master
https://developer.blender.org/rB2e8fcac15db50713ae3552e535be91ba331ad4bc

Cleanup & rework of BLO_linking code.

This commits does mostly two things:
* Get rid of bContext parameter: I can see no real good reason to pass such a high-level data
  to such low-level code... It also makes it more difficult to call when you do not have
  a context available.
* Cleanup the instantiating part.

Last point is the most risky - previous code was sometimes quite confusing and hard to follow,
from tests nothing behaves differently in new code, but some hidden corner case may show up.

Anyway, no change in behavior is expected from this commit, if it happens please file a bugreport!

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

M	source/blender/blenkernel/intern/blender.c
M	source/blender/blenloader/BLO_readfile.h
M	source/blender/blenloader/intern/readfile.c
M	source/blender/python/intern/bpy_library.c
M	source/blender/windowmanager/intern/wm_operators.c
M	source/gameengine/Converter/KX_BlenderSceneConverter.cpp

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

diff --git a/source/blender/blenkernel/intern/blender.c b/source/blender/blenkernel/intern/blender.c
index e0f2219..f130069 100644
--- a/source/blender/blenkernel/intern/blender.c
+++ b/source/blender/blenkernel/intern/blender.c
@@ -1073,7 +1073,7 @@ int BKE_copybuffer_paste(bContext *C, const char *libname, ReportList *reports)
 	
 	BLO_library_link_all(mainl, bh);
 
-	BLO_library_link_end(C, mainl, &bh, 0, 0);
+	BLO_library_link_end(mainl, &bh, 0, scene, CTX_wm_view3d(C));
 	
 	/* mark all library linked objects to be updated */
 	BKE_main_lib_objects_recalc_all(bmain);
diff --git a/source/blender/blenloader/BLO_readfile.h b/source/blender/blenloader/BLO_readfile.h
index eda76ec..c203f65 100644
--- a/source/blender/blenloader/BLO_readfile.h
+++ b/source/blender/blenloader/BLO_readfile.h
@@ -44,6 +44,7 @@ struct MemFile;
 struct ReportList;
 struct Scene;
 struct UserDef;
+struct View3D;
 struct bContext;
 struct BHead;
 struct FileData;
@@ -97,9 +98,10 @@ bool BLO_library_path_explode(const char *path, char *r_dir, char **r_group, cha
 struct Main *BLO_library_link_begin(struct Main *mainvar, BlendHandle **bh, const char *filepath);
 struct ID *BLO_library_link_named_part(struct Main *mainl, BlendHandle **bh, const char *idname, const int idcode);
 struct ID *BLO_library_link_named_part_ex(
-        const struct bContext *C, struct Main *mainl, BlendHandle **bh,
-        const char *idname, const int idcode, const short flag);
-void BLO_library_link_end(const struct bContext *C, struct Main *mainl, BlendHandle **bh, int idcode, short flag);
+        struct Main *mainl, BlendHandle **bh,
+        const char *idname, const int idcode, const short flag,
+        struct Scene *scene, struct View3D *v3d);
+void BLO_library_link_end(struct Main *mainl, BlendHandle **bh, short flag, struct Scene *scene, struct View3D *v3d);
 
 void BLO_library_link_all(struct Main *mainl, BlendHandle *bh);
 
diff --git a/source/blender/blenloader/intern/readfile.c b/source/blender/blenloader/intern/readfile.c
index 721878d..470d45f 100644
--- a/source/blender/blenloader/intern/readfile.c
+++ b/source/blender/blenloader/intern/readfile.c
@@ -9536,93 +9536,77 @@ static bool object_in_any_scene(Main *mainvar, Object *ob)
 	return false;
 }
 
-static void give_base_to_objects(Main *mainvar, Scene *sce, Library *lib, const short idcode, const bool is_link, const short active_lay)
+static void give_base_to_objects(Main *mainvar, Scene *scene, View3D *v3d, Library *lib, const short flag)
 {
 	Object *ob;
 	Base *base;
-	const bool is_group_append = (is_link == false && idcode == ID_GR);
+	const unsigned int active_lay = (flag & FILE_ACTIVELAY) ? BKE_screen_view3d_layer_active(v3d, scene) : 0;
+	const bool is_link = (flag & FILE_LINK) != 0;
+
+	BLI_assert(scene);
 
 	/* give all objects which are LIB_INDIRECT a base, or for a group when *lib has been set */
 	for (ob = mainvar->object.first; ob; ob = ob->id.next) {
-		if (ob->id.flag & LIB_INDIRECT) {
-			/* IF below is quite confusing!
-			 * if we are appending, but this object wasnt just added along with a group,
-			 * then this is already used indirectly in the scene somewhere else and we didnt just append it.
-			 *
-			 * (ob->id.flag & LIB_PRE_EXISTING)==0 means that this is a newly appended object - Campbell */
-			if (is_group_append==0 || (ob->id.flag & LIB_PRE_EXISTING)==0) {
-				bool do_it = false;
-				
-				if (ob->id.us == 0) {
-					do_it = true;
-				}
-				else if (idcode==ID_GR) {
-					if ((is_link == false) && (ob->id.lib == lib)) {
-						if ((ob->flag & OB_FROMGROUP) && object_in_any_scene(mainvar, ob)==0) {
-							do_it = true;
-						}
-					}
-				}
-				else {
-					/* when appending, make sure any indirectly loaded objects
-					 * get a base else they cant be accessed at all [#27437] */
-					if ((is_link == false) && (ob->id.lib == lib)) {
-						/* we may be appending from a scene where we already
-						 *  have a linked object which is not in any scene [#27616] */
-						if ((ob->id.flag & LIB_PRE_EXISTING)==0) {
-							if (object_in_any_scene(mainvar, ob)==0) {
-								do_it = true;
-							}
-						}
-					}
-				}
-				
-				if (do_it) {
-					base = MEM_callocN(sizeof(Base), "add_ext_base");
-					BLI_addtail(&sce->base, base);
-					
-					if (active_lay) ob->lay = sce->lay;
-					
-					base->lay = ob->lay;
-					base->object = ob;
-					base->flag = ob->flag;
+		if ((ob->id.flag & LIB_INDIRECT) && (ob->id.flag & LIB_PRE_EXISTING) == 0) {
+			bool do_it = false;
 
-					CLAMP_MIN(ob->id.us, 0);
-					ob->id.us += 1;
-					
-					ob->id.flag -= LIB_INDIRECT;
-					ob->id.flag |= LIB_EXTERN;
+			if (ob->id.us == 0) {
+				do_it = true;
+			}
+			else if (!is_link && (ob->id.lib == lib) && (object_in_any_scene(mainvar, ob) == 0)) {
+				/* When appending, make sure any indirectly loaded objects get a base, else they cant be accessed at all
+				 * (see T27437). */
+				do_it = true;
+			}
+
+			if (do_it) {
+				base = MEM_callocN(sizeof(Base), __func__);
+				BLI_addtail(&scene->base, base);
+
+				if (active_lay) {
+					ob->lay = active_lay;
 				}
+
+				base->lay = ob->lay;
+				base->object = ob;
+				base->flag = ob->flag;
+
+				CLAMP_MIN(ob->id.us, 0);
+				ob->id.us += 1;
+
+				ob->id.flag &= ~LIB_INDIRECT;
+				ob->id.flag |= LIB_EXTERN;
 			}
 		}
 	}
 }
 
-static void give_base_to_groups(Main *mainvar, Scene *scene)
+static void give_base_to_groups(
+        Main *mainvar, Scene *scene, View3D *v3d, Library *UNUSED(lib), const short UNUSED(flag))
 {
 	Group *group;
-	
+	Base *base;
+	Object *ob;
+	const unsigned int active_lay = BKE_screen_view3d_layer_active(v3d, scene);
+
 	/* give all objects which are tagged a base */
 	for (group = mainvar->group.first; group; group = group->id.next) {
 		if (group->id.flag & LIB_DOIT) {
-			Base *base;
-			Object *ob;
-
 			/* any indirect group should not have been tagged */
-			BLI_assert((group->id.flag & LIB_INDIRECT)==0);
-			
+			BLI_assert((group->id.flag & LIB_INDIRECT) == 0);
+
 			/* BKE_object_add(...) messes with the selection */
 			ob = BKE_object_add_only_object(mainvar, OB_EMPTY, group->id.name + 2);
 			ob->type = OB_EMPTY;
-			ob->lay = scene->lay;
-			
+			ob->lay = active_lay;
+
 			/* assign the base */
 			base = BKE_scene_base_add(scene, ob);
 			base->flag |= SELECT;
-			base->object->flag= base->flag;
+			base->object->flag = base->flag;
 			DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
 			scene->basact = base;
-			
+
 			/* assign the group */
 			ob->dup_group = group;
 			ob->transflag |= OB_DUPLIGROUP;
@@ -9694,33 +9678,33 @@ void BLO_library_link_all(Main *mainl, BlendHandle *bh)
 	}
 }
 
-
-static ID *link_named_part_ex(const bContext *C, Main *mainl, FileData *fd, const char *idname, const int idcode, const int flag)
+static ID *link_named_part_ex(
+        Main *mainl, FileData *fd, const char *idname, const int idcode, const int flag,
+		Scene *scene, View3D *v3d)
 {
 	ID *id = link_named_part(mainl, fd, idname, idcode);
 
 	if (id && (GS(id->name) == ID_OB)) {	/* loose object: give a base */
-		Scene *scene = CTX_data_scene(C); /* can be NULL */
 		if (scene) {
 			Base *base;
 			Object *ob;
-			
-			base= MEM_callocN(sizeof(Base), "app_nam_part");
+
+			base = MEM_callocN(sizeof(Base), "app_nam_part");
 			BLI_addtail(&scene->base, base);
-			
+
 			ob = (Object *)id;
-			
+
 			/* link at active layer (view3d if available in context, else scene one */
-			if ((flag & FILE_ACTIVELAY)) {
-				View3D *v3d = CTX_wm_view3d(C);
+			if (flag & FILE_ACTIVELAY) {
 				ob->lay = BKE_screen_view3d_layer_active(v3d, scene);
 			}
-			
+
 			ob->mode = OB_MODE_OBJECT;
 			base->lay = ob->lay;
 			base->object = ob;
+			base->flag = ob->flag;
 			ob->id.us++;
-			
+
 			if (flag & FILE_AUTOSELECT) {
 				base->flag |= SELECT;
 				base->object->flag = base->flag;
@@ -9729,11 +9713,11 @@ static ID *link_named_part_ex(const bContext *C, Main *mainl, FileData *fd, cons
 		}
 	}
 	else if (id && (GS(id->name) == ID_GR)) {
-		/* tag as needing to be instanced */
+		/* tag as needing to be instantiated */
 		if (flag & FILE_GROUP_INSTANCE)
 			id->flag |= LIB_DOIT;
 	}
-	
+
 	return id;
 }
 
@@ -9756,20 +9740,21 @@ ID *BLO_library_link_named_part(Main *mainl, BlendHandle **bh, const char *idnam
  * Link a named datablock from an external blend file.
  * Optionally instantiate the object/group in the scene when the flags are set.
  *
- * \param C The context, when NULL instantiating object in the scene isn't done.
  * \param mainl The main database to link from (not the active one).
  * \param bh The blender file handle.
  * \param idname The name of the datablock (without the 2 char ID prefix).
  * \param idcode The kind of datablock to link.
  * \param flag Options for linking, used for instantiating.
+ * \param scene The scene in which to instantiate objects/groups (if NULL, no instantiation is done).
+ * \param v3d The active View3D (only to define active layers for instantiated objects & groups, can be NULL).
  * \return the appended ID when found.
  */
 ID *BLO_library_link_named_part_ex(
-        const bContext *C, Main *mainl, BlendHandle **bh,
-        const char *idname, const int idcode, const short flag)
+        Main *mainl, BlendHandle **bh, const char *idname, const int idcode, const short flag,
+        Scene *scene, View3D *v3d)
 {
 	FileData *fd = (FileData*)(*bh);
-	return link_named_part_ex(C, mainl, fd, idname, idcode, flag);
+	return link_named_part_ex(mainl, fd, idname, idcode, flag, scene, v3d);
 }
 
 static void link_id_part(FileData *fd, Main *mainvar, ID *id, ID **r_id)
@@ -9825,71 +9810,58 @@ Main *BLO_library_link_begin(Main *mainvar, BlendHandle **bh, const char *filepa
 	return library_link_begin(mainvar, &fd, filepath);
 }
 
-
-/* Context == NULL signifies not to do any scene manipulation */
-static void library_link_end(const bContext *C, Main *mainl, FileData **fd, int idcode, short flag)
+/* scene and v3d may be NULL. */
+static void library_link_end(Main *mainl, FileData **fd, const short flag, Scene *scene, View3D *v3d)
 {
 	Main *mainvar;
 	Library *curlib;
-	
+
 	/* expander now is callback function */
 	BLO_main_expander(expand_doit_library);
-

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list