improved functions and ready for look assignment tool

This commit is contained in:
aardschok 2017-08-03 15:36:53 +02:00
parent 076b7ff0c7
commit 8becf73791
4 changed files with 36 additions and 84 deletions

View file

@ -99,7 +99,8 @@ def on_save(nodes=None):
defaults = ["initialShadingGroup", "initialParticleSE"]
# the default items which always want to have an ID
types = ["mesh", "shadingEngine", "file", "nurbsCurve"]
# objectSets include: shading engines, vray object properties
types = ["mesh", "objectSet", "file", "nurbsCurve"]
# the items which need to pass the id to their parent
if not nodes:

View file

@ -740,7 +740,7 @@ def assign_look_by_version(nodes, version_id):
reference=True,
returnNewNodes=True)
else:
log.info("Reusing existing lookdev..")
log.info("Reusing existing lookdev '{}'".format(reference_node))
shader_nodes = cmds.referenceQuery(reference_node, nodes=True)
# Assign relationships
@ -768,26 +768,29 @@ def assign_look(nodes, subset="lookDefault"):
if not colorbleed_id:
continue
parts = colorbleed_id.split(":")
if len(parts) != 2:
continue
parts = colorbleed_id.split(":", 1)
grouped[parts[0]].append(node)
for asset_id, asset_nodes in grouped.items():
# create objectId for database
asset_id = bson.ObjectId(asset_id)
subset = io.find_one({"type": "subset",
"name": subset,
"parent": asset_id})
try:
asset_id = bson.ObjectId(asset_id)
except Exception:
log.warning("Asset ID is not compatible with bson")
continue
subset_data = io.find_one({"type": "subset",
"name": subset,
"parent": asset_id})
assert subset, "No subset found for {}".format(asset_id)
if not subset_data:
log.warning("No subset '{}' found for {}".format(subset, asset_id))
continue
# get last version
version = io.find_one({"parent": subset['_id'],
version = io.find_one({"parent": subset_data['_id'],
"type": "version",
"data.families":
{"$in":["colorbleed.lookdev"]}
{"$in": ["colorbleed.lookdev"]}
},
sort=[("name", -1)],
projection={"_id": True})
@ -816,28 +819,37 @@ def apply_shaders(relationships, shadernodes, nodes):
None
"""
# attributes = relationships.get("attributes", [])
shader_sets = relationships.get("sets", [])
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 engines "
"from reference")
# get all nodes which we need to link
ns_nodes = cmds.ls(nodes, long=True)
# region compute lookup
ns_nodes_by_id = defaultdict(list)
for node in nodes:
ns_nodes_by_id[_get_id(node)].append(node)
shading_engines_by_id = defaultdict(list)
for shad in shading_engines:
shading_engines_by_id[_get_id(shad)].append(shad)
# endregion
# region assign
for shader_set in shader_sets:
# collect all unique IDs of the set members
shader_uuid = shader_set["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])
filtered_nodes = list()
for uuid in member_uuids:
filtered_nodes.extend(ns_nodes_by_id[uuid])
shading_engine = shading_engines_by_id[shader_uuid]
assert len(shading_engine) == 1, ("Could not find the correct "
"shading engine with cbId "
"'{}'".format(shader_uuid))
cmds.sets(filtered_nodes, forceElement=shading_engine[0])
# endregion

View file

@ -69,62 +69,6 @@ class LookLoader(api.Loader):
# Get all nodes which belong to a matching name space
# Currently this is the safest way to get all the nodes
namespace_nodes = self.get_namespace_nodes(assetname)
lib.apply_shaders(relationships, nodes, namespace_nodes)
lib.apply_shaders(relationships, nodes)
self[:] = nodes
def get_namespace_nodes(self, assetname):
"""
Get all nodes of namespace `asset_*` and check if they have a shader
assigned, if not add to list
Args:
context (dict): current context of asset
Returns:
list
"""
list_nodes = []
# remove basic namespaces
namespaces = [ns for ns in cmds.namespaceInfo(listOnlyNamespaces=True)
if ns not in ["UI", "shared"] or not ns.endswith("look")]
for namespace in namespaces:
if not namespace.startswith(assetname):
continue
ns_nodes = cmds.namespaceInfo(namespace,
listOnlyDependencyNodes=True)
# get reference nodes
list_nodes.extend([self.has_default_shader(n) for n in ns_nodes])
# ensure unique nodes and kick out any None types
result = [node for node in list_nodes if node is not None]
return result
def has_default_shader(self, node):
"""Check if the nodes have `initialShadingGroup` shader assigned
Args:
node (str): node to check
Returns:
str
"""
shaders = cmds.listConnections(node, type="shadingEngine")
if shaders is None or "initialShadingGroup" in shaders:
# return transform node
transform = cmds.listRelatives(node,
parent=True,
type="transform",
fullPath=True)
if not transform:
return
return transform[0]

View file

@ -1,11 +1,6 @@
import os
import pprint
from maya import cmds
import pyblish.api
import avalon.io as io
from cb.utils.maya import context, shaders
import cbra.utils.maya.node_uuid as id_utils