WIP on shader assignment

This commit is contained in:
Ondrej Samohel 2021-04-30 17:24:48 +02:00 committed by Ondrej Samohel
parent 0b244d3b6d
commit 68f81b8a3d
No known key found for this signature in database
GPG key ID: 02376E18990A97C6
8 changed files with 531 additions and 18 deletions

View file

@ -1124,19 +1124,18 @@ def get_id_required_nodes(referenced_nodes=False, nodes=None):
def get_id(node):
"""
Get the `cbId` attribute of the given node
"""Get the `cbId` attribute of the given node.
Args:
node (str): the name of the node to retrieve the attribute from
Returns:
str
"""
if node is None:
return
print("><?>< {}".format(node))
sel = om.MSelectionList()
sel.add(node)
@ -2688,3 +2687,69 @@ def show_message(title, msg):
pass
else:
message_window.message(title=title, message=msg, parent=parent)
def iter_shader_edits(relationships, shader_nodes, nodes_by_id, label=None):
"""Yield edits as a set of actions."""
attributes = relationships.get("attributes", [])
shader_data = relationships.get("relationships", {})
shading_engines = cmds.ls(shader_nodes, type="objectSet", long=True)
assert shading_engines, "Error in retrieving objectSets from reference"
# region compute lookup
shading_engines_by_id = defaultdict(list)
for shad in shading_engines:
shading_engines_by_id[get_id(shad)].append(shad)
# endregion
# region assign shading engines and other sets
for data in shader_data.values():
# collect all unique IDs of the set members
shader_uuid = data["uuid"]
member_uuids = [
(member["uuid"], member.get("components"))
for member in data["members"]]
filtered_nodes = list()
for _uuid, components in member_uuids:
nodes = nodes_by_id.get(_uuid, None)
if nodes is None:
continue
if components:
# Assign to the components
nodes = [".".join([node, components]) for node in nodes]
filtered_nodes.extend(nodes)
id_shading_engines = shading_engines_by_id[shader_uuid]
if not id_shading_engines:
log.error("{} - No shader found with cbId "
"'{}'".format(label, shader_uuid))
continue
elif len(id_shading_engines) > 1:
log.error("{} - Skipping shader assignment. "
"More than one shader found with cbId "
"'{}'. (found: {})".format(label, shader_uuid,
id_shading_engines))
continue
if not filtered_nodes:
log.warning("{} - No nodes found for shading engine "
"'{}'".format(label, id_shading_engines[0]))
continue
yield {"action": "assign",
"uuid": data["uuid"],
"nodes": filtered_nodes,
"shader": id_shading_engines[0]}
for data in attributes:
nodes = nodes_by_id.get(data["uuid"], [])
attr_value = data["attributes"]
yield {"action": "setattr",
"uuid": data["uuid"],
"nodes": nodes,
"attributes": attr_value}