fixed assignement to alembic mesh, code improvement

This commit is contained in:
aardschok 2017-07-27 11:46:01 +02:00
parent 3caf2472da
commit 6d7262d26b
2 changed files with 47 additions and 56 deletions

View file

@ -5,6 +5,7 @@ import os
import bson import bson
import json import json
import logging import logging
import pprint
import contextlib import contextlib
from collections import OrderedDict, defaultdict from collections import OrderedDict, defaultdict
@ -635,13 +636,9 @@ def filter_by_id(nodes, uuids):
if node is None: if node is None:
continue continue
if not cmds.attributeQuery("cbId", node=node, exists=True): attribute_value = _get_id(node)
continue
# Deformed shaped if attribute_value not in uuids or attribute_value is None:
attr = "{}.cbId".format(node)
attribute_value = cmds.getAttr(attr)
if attribute_value not in uuids:
continue continue
filtered_nodes.append(node) filtered_nodes.append(node)
@ -792,48 +789,45 @@ def assign_look(nodes, subset="lookDefault"):
assign_look_by_version(asset_nodes, version['_id']) assign_look_by_version(asset_nodes, version['_id'])
def apply_shaders(relationships, shader_nodes, nodes): def apply_shaders(relationships, shadernodes, nodes):
"""Apply all shaders to the nodes based on the relationship data """Link shadingEngine to the right nodes based on relationship data
Relationship data is constructed of a collection of `sets` and `attributes`
`sets` corresponds with the shaderEngines found in the lookdev.
Each set has the keys `name`, `members` and `uuid`, the `members`
hold a collection of node information `name` and `uuid`.
Args: Args:
relationships (dict): shader to node relationships relationships (dict): relationship data
shader_nodes (list): shader network nodes shadernodes (list): list of nodes of the shading engine
nodes (list): nodes to assign to nodes (list): list of nodes to apply shader to
Returns: Returns:
None None
""" """
# attributes = relationships.get("attributes", [])
shader_sets = relationships.get("sets", []) shader_sets = relationships.get("sets", [])
shading_engines = cmds.ls(shader_nodes, type="shadingEngine", long=True)
if isinstance(nodes, set):
nodes = list(nodes)
shading_engines = cmds.ls(shadernodes, type="shadingEngine", long=True)
assert len(shading_engines) > 0, ("Error in retrieving shading engine " assert len(shading_engines) > 0, ("Error in retrieving shading engine "
"from reference") "from reference")
# Pre-filter nodes and shader nodes # get all nodes which we need to link
nodes_by_id = defaultdict(list) ns_nodes = cmds.ls(nodes, long=True)
shader_nodes_by_id = defaultdict(list)
for node in nodes:
_id = _get_id(node)
nodes_by_id[_id].append(node)
for shader_node in shader_nodes:
_id = _get_id(shader_node)
shader_nodes_by_id[_id].append(shader_node)
# get all nodes which we need to link per shader
for shader_set in shader_sets: for shader_set in shader_sets:
# collect shading engine # collect all unique IDs of the set members
uuid = shader_set["uuid"] shader_uuid = shader_set["uuid"]
shading_engine = shader_nodes_by_id.get(uuid, []) member_uuids = [member["uuid"] for member in shader_set["members"]]
filtered_nodes = filter_by_id(ns_nodes, member_uuids)
shading_engine = filter_by_id(shading_engines, [shader_uuid])
assert len(shading_engine) == 1, ("Could not find the correct " assert len(shading_engine) == 1, ("Could not find the correct "
"shading engine with cbId " "shading engine with cbId "
"'{}'".format(uuid)) "'{}'".format(shader_uuid))
# collect members
filtered_nodes = list()
for member in shader_set["members"]:
member_uuid = member["uuid"]
members = nodes_by_id.get(member_uuid, [])
filtered_nodes.extend(members)
cmds.sets(filtered_nodes, forceElement=shading_engine[0]) cmds.sets(filtered_nodes, forceElement=shading_engine[0])

View file

@ -30,8 +30,6 @@ class LookLoader(api.Loader):
""" """
# improve readability of the namespace # improve readability of the namespace
assetname = context["asset"]["name"] assetname = context["asset"]["name"]
ns_assetname = "{}_".format(assetname) ns_assetname = "{}_".format(assetname)
@ -88,29 +86,25 @@ class LookLoader(api.Loader):
""" """
# types = ["transform", "mesh"]
list_nodes = [] list_nodes = []
namespaces = cmds.namespaceInfo(listOnlyNamespaces=True)
# remove basic namespaces # remove basic namespaces
namespaces.remove("UI") namespaces = [ns for ns in cmds.namespaceInfo(listOnlyNamespaces=True)
namespaces.remove("shared") if ns not in ["UI", "shared"] or not ns.endswith("look")]
for ns in namespaces: for namespace in namespaces:
if not ns.startswith(assetname): if not namespace.startswith(assetname):
continue continue
ns_nodes = cmds.namespaceInfo(namespace,
listOnlyDependencyNodes=True)
# get reference nodes # get reference nodes
ns_nodes = cmds.namespaceInfo(ns, listOnlyDependencyNodes=True) list_nodes.extend([self.has_default_shader(n) for n in ns_nodes])
# TODO: might need to extend the types
# check if any nodes are connected to something else than lambert1
list_nodes = cmds.ls(ns_nodes, long=True)
unassigned_nodes = [self.has_default_shader(n) for n in list_nodes]
nodes = [n for n in unassigned_nodes if n is not None]
list_nodes.extend(nodes) # ensure unique nodes and kick out any None types
result = [node for node in list_nodes if node is not None]
return set(list_nodes) return result
def has_default_shader(self, node): def has_default_shader(self, node):
"""Check if the nodes have `initialShadingGroup` shader assigned """Check if the nodes have `initialShadingGroup` shader assigned
@ -122,12 +116,15 @@ class LookLoader(api.Loader):
str str
""" """
shaders = cmds.listConnections(node, type="shadingEngine") or [] shaders = cmds.listConnections(node, type="shadingEngine")
if "initialShadingGroup" in shaders: if shaders is None or "initialShadingGroup" in shaders:
# return transform node # return transform node
transform = cmds.listRelatives(node, parent=True, type="transform", transform = cmds.listRelatives(node,
parent=True,
type="transform",
fullPath=True) fullPath=True)
if not transform: if not transform:
return [] return
return transform[0] return transform[0]