From 36acca2a0e28a184c0d04f28cfefc5bf1bfa56d2 Mon Sep 17 00:00:00 2001 From: aardschok Date: Mon, 30 Oct 2017 14:26:39 +0100 Subject: [PATCH 1/6] fallback logic for when reference is found --- colorbleed/maya/lib.py | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/colorbleed/maya/lib.py b/colorbleed/maya/lib.py index 95ca405feb..a17d710296 100644 --- a/colorbleed/maya/lib.py +++ b/colorbleed/maya/lib.py @@ -814,25 +814,23 @@ def assign_look_by_version(nodes, version_id): "name": "json"}) # Load file - shader_filepath = api.get_representation_path(look_representation) shader_relation = api.get_representation_path(connection_represenations) - reference_node = get_reference_node(shader_filepath) - if reference_node is None: - log.info("Loading lookdev for the first time..") + loaders = list(loaderlib.iter_loaders(look_representation["_id"])) + Loader = next((i for i in loaders if i.__name__ == "LookLoader"), None) + if Loader is None: + raise RuntimeError("Could not find LookLoader, this is a bug") - loaders = list(loaderlib.iter_loaders(look_representation["_id"])) - Loader = next((i for i in loaders if i.__name__ == "LookLoader"), None) - if Loader is None: - raise RuntimeError("Could not find LookLoader, this is a bug") - - # Reference the look file - with maya.maintained_selection(): - shader_nodes = pipeline.load(Loader, look_representation) - - else: - log.info("Reusing existing lookdev '{}'".format(reference_node)) - shader_nodes = cmds.referenceQuery(reference_node, nodes=True) + # Reference the look file + with maya.maintained_selection(): + container = pipeline.load(Loader, look_representation) + if not container: + log.info("Reusing loaded Lookdev ..") + look_file = api.get_representation_path(look_representation) + shader_nodes = cmds.referenceQuery(look_file, nodes=True) + else: + print(">>", container) + shader_nodes = cmds.sets(container, query=True) # Assign relationships with open(shader_relation, "r") as f: From d66f4b41e7fef45ffeac43f8f87d443829b7c7f2 Mon Sep 17 00:00:00 2001 From: aardschok Date: Mon, 30 Oct 2017 14:27:06 +0100 Subject: [PATCH 2/6] set no nodes when reference is found --- colorbleed/plugins/maya/load/load_look.py | 22 +++------------------- 1 file changed, 3 insertions(+), 19 deletions(-) diff --git a/colorbleed/plugins/maya/load/load_look.py b/colorbleed/plugins/maya/load/load_look.py index 3260737349..da277691b2 100644 --- a/colorbleed/plugins/maya/load/load_look.py +++ b/colorbleed/plugins/maya/load/load_look.py @@ -25,8 +25,6 @@ class LookLoader(colorbleed.maya.plugin.ReferenceLoader): """ - import json - import os import maya.cmds as cmds from avalon import maya import colorbleed.maya.lib as lib @@ -36,7 +34,8 @@ class LookLoader(colorbleed.maya.plugin.ReferenceLoader): reference_node = None try: reference_node = lib.get_reference_node(self.fname) - except: + except Exception as e: + self.log.error(e) pass if reference_node is None: @@ -48,21 +47,6 @@ class LookLoader(colorbleed.maya.plugin.ReferenceLoader): returnNewNodes=True) else: self.log.info("Reusing existing lookdev ...") - nodes = cmds.referenceQuery(reference_node, nodes=True) - - # Assign shaders - self.fname = self.fname.rsplit(".", 1)[0] + ".json" - if not os.path.isfile(self.fname): - self.log.warning("Look development asset " - "has no relationship data.") - return nodes - - with open(self.fname) as f: - relationships = json.load(f) - - # Get all nodes which belong to a matching name space - # Currently this is the safest way to get all the nodes - # Pass empty list as nodes to assign to in order to only load - lib.apply_shaders(relationships, nodes, []) + nodes = None self[:] = nodes From f9db6771f2f66e198975a16201e4ea3d49bdd325 Mon Sep 17 00:00:00 2001 From: aardschok Date: Mon, 30 Oct 2017 16:14:32 +0100 Subject: [PATCH 3/6] reduced code to simple loading of file --- colorbleed/plugins/maya/load/load_look.py | 25 +++++------------------ 1 file changed, 5 insertions(+), 20 deletions(-) diff --git a/colorbleed/plugins/maya/load/load_look.py b/colorbleed/plugins/maya/load/load_look.py index da277691b2..16428ef034 100644 --- a/colorbleed/plugins/maya/load/load_look.py +++ b/colorbleed/plugins/maya/load/load_look.py @@ -27,26 +27,11 @@ class LookLoader(colorbleed.maya.plugin.ReferenceLoader): import maya.cmds as cmds from avalon import maya - import colorbleed.maya.lib as lib - # try / except here is to ensure that the get_reference_node - # does not fail when the file doesn't exist yet - reference_node = None - try: - reference_node = lib.get_reference_node(self.fname) - except Exception as e: - self.log.error(e) - pass - - if reference_node is None: - self.log.info("Loading lookdev for the first time ...") - with maya.maintained_selection(): - nodes = cmds.file(self.fname, - namespace=namespace, - reference=True, - returnNewNodes=True) - else: - self.log.info("Reusing existing lookdev ...") - nodes = None + with maya.maintained_selection(): + nodes = cmds.file(self.fname, + namespace=namespace, + reference=True, + returnNewNodes=True) self[:] = nodes From 7c9ff40155c4f854bdf8d813e617072dc8abab46 Mon Sep 17 00:00:00 2001 From: aardschok Date: Mon, 30 Oct 2017 16:14:59 +0100 Subject: [PATCH 4/6] check for existing look based on representation --- colorbleed/maya/lib.py | 42 ++++++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/colorbleed/maya/lib.py b/colorbleed/maya/lib.py index a17d710296..f9cc95eea0 100644 --- a/colorbleed/maya/lib.py +++ b/colorbleed/maya/lib.py @@ -809,33 +809,39 @@ def assign_look_by_version(nodes, version_id): "parent": version_id, "name": "ma"}) - connection_represenations = io.find_one({"type": "representation", + connection_representions = io.find_one({"type": "representation", "parent": version_id, "name": "json"}) - # Load file - shader_relation = api.get_representation_path(connection_represenations) + # See if representation is already loaded, if so reuse it. + host = api.registered_host() + representation_id = str(look_representation['_id']) + for container in host.ls(): + if (container['loader'] == "LookLoader" and + container['representation'] == representation_id): + log.info("Reusing loaded look ..") + container_node = container['objectName'] + break + else: + log.info("Using look for the first time ..") + # Load file - loaders = list(loaderlib.iter_loaders(look_representation["_id"])) - Loader = next((i for i in loaders if i.__name__ == "LookLoader"), None) - if Loader is None: - raise RuntimeError("Could not find LookLoader, this is a bug") + loaders = list(loaderlib.iter_loaders(look_representation["_id"])) + Loader = next((i for i in loaders if i.__name__ == "LookLoader"), None) + if Loader is None: + raise RuntimeError("Could not find LookLoader, this is a bug") - # Reference the look file - with maya.maintained_selection(): - container = pipeline.load(Loader, look_representation) - if not container: - log.info("Reusing loaded Lookdev ..") - look_file = api.get_representation_path(look_representation) - shader_nodes = cmds.referenceQuery(look_file, nodes=True) - else: - print(">>", container) - shader_nodes = cmds.sets(container, query=True) + # Reference the look file + with maya.maintained_selection(): + container_node = pipeline.load(Loader, look_representation) + shader_nodes = cmds.sets(container_node, query=True) - # Assign relationships + # Load relationships + shader_relation = api.get_representation_path(connection_representions) with open(shader_relation, "r") as f: relationships = json.load(f) + # Assign relationships apply_shaders(relationships, shader_nodes, nodes) From 41716efd77ad1bcf10304440768173599579bb06 Mon Sep 17 00:00:00 2001 From: aardschok Date: Mon, 30 Oct 2017 16:21:04 +0100 Subject: [PATCH 5/6] cosmetics --- colorbleed/maya/lib.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/colorbleed/maya/lib.py b/colorbleed/maya/lib.py index f9cc95eea0..02ac315087 100644 --- a/colorbleed/maya/lib.py +++ b/colorbleed/maya/lib.py @@ -796,7 +796,7 @@ def assign_look_by_version(nodes, version_id): Args: nodes(list): nodes to assign look to - version_id (bson.ObjectId) + version_id (bson.ObjectId): database id of the version Returns: None @@ -804,14 +804,14 @@ def assign_look_by_version(nodes, version_id): # TODO: make `iter_loader` available in as a more global function from avalon.tools.cbloader import lib as loaderlib - # get representations of shader file and relationships + # Get representations of shader file and relationships look_representation = io.find_one({"type": "representation", "parent": version_id, "name": "ma"}) connection_representions = io.find_one({"type": "representation", - "parent": version_id, - "name": "json"}) + "parent": version_id, + "name": "json"}) # See if representation is already loaded, if so reuse it. host = api.registered_host() @@ -824,8 +824,8 @@ def assign_look_by_version(nodes, version_id): break else: log.info("Using look for the first time ..") - # Load file + # Load file loaders = list(loaderlib.iter_loaders(look_representation["_id"])) Loader = next((i for i in loaders if i.__name__ == "LookLoader"), None) if Loader is None: @@ -834,6 +834,8 @@ def assign_look_by_version(nodes, version_id): # Reference the look file with maya.maintained_selection(): container_node = pipeline.load(Loader, look_representation) + + # Get container nodes shader_nodes = cmds.sets(container_node, query=True) # Load relationships From 59941cf844c0be3cfbac2757ae946b2bb9f66681 Mon Sep 17 00:00:00 2001 From: aardschok Date: Mon, 30 Oct 2017 17:16:26 +0100 Subject: [PATCH 6/6] cosmetics --- colorbleed/maya/lib.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/colorbleed/maya/lib.py b/colorbleed/maya/lib.py index 02ac315087..bf8e3e4d47 100644 --- a/colorbleed/maya/lib.py +++ b/colorbleed/maya/lib.py @@ -809,9 +809,9 @@ def assign_look_by_version(nodes, version_id): "parent": version_id, "name": "ma"}) - connection_representions = io.find_one({"type": "representation", - "parent": version_id, - "name": "json"}) + json_representation = io.find_one({"type": "representation", + "parent": version_id, + "name": "json"}) # See if representation is already loaded, if so reuse it. host = api.registered_host() @@ -835,11 +835,11 @@ def assign_look_by_version(nodes, version_id): with maya.maintained_selection(): container_node = pipeline.load(Loader, look_representation) - # Get container nodes + # Get container members shader_nodes = cmds.sets(container_node, query=True) # Load relationships - shader_relation = api.get_representation_path(connection_representions) + shader_relation = api.get_representation_path(json_representation) with open(shader_relation, "r") as f: relationships = json.load(f)