From 52e2d785c10a0fbf2271b654db58ea22a39f15e5 Mon Sep 17 00:00:00 2001 From: Simone Barbieri Date: Tue, 30 Jun 2020 11:35:24 +0100 Subject: [PATCH] Small improvements and Pep8 compliance --- pype/hosts/blender/plugin.py | 11 ++++++--- pype/plugins/blender/load/load_layout.py | 15 +++++------- pype/plugins/blender/load/load_model.py | 24 +++++++------------ pype/plugins/blender/load/load_rig.py | 30 ++++++++++-------------- 4 files changed, 35 insertions(+), 45 deletions(-) diff --git a/pype/hosts/blender/plugin.py b/pype/hosts/blender/plugin.py index eff41956e8..b0420fddfc 100644 --- a/pype/hosts/blender/plugin.py +++ b/pype/hosts/blender/plugin.py @@ -31,10 +31,8 @@ def asset_namespace( loaded_assets = [] for c in avalon_containers: loaded_assets.extend(c.children) - if avalon_containers is None: - return "1" collections_names = [ - c.name for c in loaded_assets + c.name for c in loaded_assets ] count = 1 name = f"{asset_name(asset, subset, str(count))}_CON" @@ -89,6 +87,13 @@ def get_parent_collection(collection): return None +def get_local_collection_with_name(name): + for collection in bpy.data.collections: + if collection.name == name and collection.library is None: + return collection + return None + + class AssetLoader(api.Loader): """A basic AssetLoader for Blender diff --git a/pype/plugins/blender/load/load_layout.py b/pype/plugins/blender/load/load_layout.py index 5d3f7d92cf..e9e128bb7c 100644 --- a/pype/plugins/blender/load/load_layout.py +++ b/pype/plugins/blender/load/load_layout.py @@ -9,9 +9,6 @@ from avalon import api, blender import bpy import pype.hosts.blender.plugin as plugin -logger = logging.getLogger("pype").getChild( - "blender").getChild("load_layout") - class BlendLayoutLoader(plugin.AssetLoader): """Load layout from a .blend file.""" @@ -65,15 +62,15 @@ class BlendLayoutLoader(plugin.AssetLoader): for container in layout_container.children: if container.name == blender.pipeline.AVALON_CONTAINERS: containers.remove(container) - + for container in containers: container.make_local() objects.extend([ - obj for obj in container.objects + obj for obj in container.objects if obj.type in objects_local_types ]) armatures.extend([ - obj for obj in container.objects + obj for obj in container.objects if obj.type == 'ARMATURE' ]) containers.extend(list(container.children)) @@ -175,7 +172,7 @@ class BlendLayoutLoader(plugin.AssetLoader): libpath = Path(api.get_representation_path(representation)) extension = libpath.suffix.lower() - logger.info( + self.log.info( "Container: %s\nRepresentation: %s", pformat(container, indent=2), pformat(representation, indent=2), @@ -210,13 +207,13 @@ class BlendLayoutLoader(plugin.AssetLoader): normalized_libpath = ( str(Path(bpy.path.abspath(str(libpath))).resolve()) ) - logger.debug( + self.log.debug( "normalized_collection_libpath:\n %s\nnormalized_libpath:\n %s", normalized_collection_libpath, normalized_libpath, ) if normalized_collection_libpath == normalized_libpath: - logger.info("Library already loaded, not updating...") + self.log.info("Library already loaded, not updating...") return actions = {} diff --git a/pype/plugins/blender/load/load_model.py b/pype/plugins/blender/load/load_model.py index 7b2ade4570..ad4b35eb03 100644 --- a/pype/plugins/blender/load/load_model.py +++ b/pype/plugins/blender/load/load_model.py @@ -9,8 +9,6 @@ from avalon import api, blender import bpy import pype.hosts.blender.plugin as plugin -logger = logging.getLogger("pype").getChild("blender").getChild("load_model") - class BlendModelLoader(plugin.AssetLoader): """Load models from a .blend file. @@ -145,7 +143,7 @@ class BlendModelLoader(plugin.AssetLoader): libpath = Path(api.get_representation_path(representation)) extension = libpath.suffix.lower() - logger.info( + self.log.info( "Container: %s\nRepresentation: %s", pformat(container, indent=2), pformat(representation, indent=2), @@ -172,11 +170,9 @@ class BlendModelLoader(plugin.AssetLoader): collection_libpath = collection_metadata["libpath"] lib_container = collection_metadata["lib_container"] - obj_container = [ - c for c in bpy.data.collections - if (c.name == collection_metadata["obj_container"].name and - c.library is None) - ][0] + obj_container = get_local_collection_with_name( + collection_metadata["obj_container"].name + ) objects = obj_container.all_objects container_name = obj_container.name @@ -187,13 +183,13 @@ class BlendModelLoader(plugin.AssetLoader): normalized_libpath = ( str(Path(bpy.path.abspath(str(libpath))).resolve()) ) - logger.debug( + self.log.debug( "normalized_collection_libpath:\n %s\nnormalized_libpath:\n %s", normalized_collection_libpath, normalized_libpath, ) if normalized_collection_libpath == normalized_libpath: - logger.info("Library already loaded, not updating...") + self.log.info("Library already loaded, not updating...") return parent = plugin.get_parent_collection(obj_container) @@ -234,11 +230,9 @@ class BlendModelLoader(plugin.AssetLoader): collection_metadata = collection.get( blender.pipeline.AVALON_PROPERTY) - obj_container = [ - c for c in bpy.data.collections - if (c.name == collection_metadata["obj_container"].name and - c.library is None) - ][0] + obj_container = get_local_collection_with_name( + collection_metadata["obj_container"].name + ) objects = obj_container.all_objects self._remove(objects, obj_container) diff --git a/pype/plugins/blender/load/load_rig.py b/pype/plugins/blender/load/load_rig.py index 7ac8a72993..860cae71ba 100644 --- a/pype/plugins/blender/load/load_rig.py +++ b/pype/plugins/blender/load/load_rig.py @@ -9,8 +9,6 @@ from avalon import api, blender import bpy import pype.hosts.blender.plugin as plugin -logger = logging.getLogger("pype").getChild("blender").getChild("load_rig") - class BlendRigLoader(plugin.AssetLoader): """Load rigs from a .blend file. @@ -39,7 +37,7 @@ class BlendRigLoader(plugin.AssetLoader): bpy.data.collections.remove(obj_container) def _process( - self, libpath, lib_container, container_name, + self, libpath, lib_container, container_name, action, parent_collection ): relative = bpy.context.preferences.filepaths.use_relative_paths @@ -52,7 +50,7 @@ class BlendRigLoader(plugin.AssetLoader): if parent is None: parent = bpy.context.scene.collection - + parent.children.link(bpy.data.collections[lib_container]) rig_container = parent.children[lib_container].make_local() @@ -60,7 +58,7 @@ class BlendRigLoader(plugin.AssetLoader): meshes = [] armatures = [ - obj for obj in rig_container.objects + obj for obj in rig_container.objects if obj.type == 'ARMATURE' ] @@ -161,7 +159,7 @@ class BlendRigLoader(plugin.AssetLoader): libpath = Path(api.get_representation_path(representation)) extension = libpath.suffix.lower() - logger.info( + self.log.info( "Container: %s\nRepresentation: %s", pformat(container, indent=2), pformat(representation, indent=2), @@ -188,11 +186,9 @@ class BlendRigLoader(plugin.AssetLoader): collection_libpath = collection_metadata["libpath"] lib_container = collection_metadata["lib_container"] - obj_container = [ - c for c in bpy.data.collections - if (c.name == collection_metadata["obj_container"].name and - c.library is None) - ][0] + obj_container = get_local_collection_with_name( + collection_metadata["obj_container"].name + ) objects = obj_container.all_objects container_name = obj_container.name @@ -203,13 +199,13 @@ class BlendRigLoader(plugin.AssetLoader): normalized_libpath = ( str(Path(bpy.path.abspath(str(libpath))).resolve()) ) - logger.debug( + self.log.debug( "normalized_collection_libpath:\n %s\nnormalized_libpath:\n %s", normalized_collection_libpath, normalized_libpath, ) if normalized_collection_libpath == normalized_libpath: - logger.info("Library already loaded, not updating...") + self.log.info("Library already loaded, not updating...") return # Get the armature of the rig @@ -259,11 +255,9 @@ class BlendRigLoader(plugin.AssetLoader): collection_metadata = collection.get( blender.pipeline.AVALON_PROPERTY) - obj_container = [ - c for c in bpy.data.collections - if (c.name == collection_metadata["obj_container"].name and - c.library is None) - ][0] + obj_container = get_local_collection_with_name( + collection_metadata["obj_container"].name + ) objects = obj_container.all_objects self._remove(objects, obj_container)