Rely more on update logic from parent ReferenceLoader class - reduce duplicated code

This commit is contained in:
Roy Nieterau 2022-01-26 17:06:55 +01:00
parent 7498fcb1bd
commit 58a559fc2c

View file

@ -65,38 +65,18 @@ class LookLoader(openpype.hosts.maya.api.plugin.ReferenceLoader):
Returns:
None
"""
import os
from maya import cmds
node = container["objectName"]
path = api.get_representation_path(representation)
# Get reference node from container members
members = openpype.hosts.maya.api.lib.get_container_members(container)
reference_node = openpype.hosts.maya.api.plugin.get_reference_node(
members, log=self.log
)
shader_nodes = cmds.ls(members, type='shadingEngine')
orig_nodes = set(self._get_nodes_with_shader(shader_nodes))
file_type = {
"ma": "mayaAscii",
"mb": "mayaBinary",
"abc": "Alembic"
}.get(representation["name"])
assert file_type, "Unsupported representation: %s" % representation
assert os.path.exists(path), "%s does not exist." % path
self._load_reference(file_type, node, path, reference_node)
# Remove any placeHolderList attribute entries from the set that
# are remaining from nodes being removed from the referenced file.
members = cmds.sets(node, query=True)
invalid = [x for x in members if ".placeHolderList" in x]
if invalid:
cmds.sets(invalid, remove=node)
# Trigger the regular reference update on the ReferenceLoader
super(LookLoader, self).update(container, representation)
# get new applied shaders and nodes from new version
shader_nodes = cmds.ls(members, type='shadingEngine')
@ -114,13 +94,11 @@ class LookLoader(openpype.hosts.maya.api.plugin.ReferenceLoader):
json_data = json.load(f)
# update of reference could result in failed edits - material is not
# present because of renaming etc.
# present because of renaming etc. If so highlight failed edits to user
failed_edits = cmds.referenceQuery(reference_node,
editStrings=True,
failedEdits=True,
successfulEdits=False)
# highlight failed edits to user
if failed_edits:
# clean references - removes failed reference edits
cmds.file(cr=reference_node) # cleanReference
@ -148,11 +126,6 @@ class LookLoader(openpype.hosts.maya.api.plugin.ReferenceLoader):
nodes_by_id[openpype.hosts.maya.api.lib.get_id(n)].append(n)
openpype.hosts.maya.api.lib.apply_attributes(attributes, nodes_by_id)
# Update metadata
cmds.setAttr("{}.representation".format(node),
str(representation["_id"]),
type="string")
def _get_nodes_with_shader(self, shader_nodes):
"""
Returns list of nodes belonging to specific shaders
@ -171,44 +144,4 @@ class LookLoader(openpype.hosts.maya.api.plugin.ReferenceLoader):
for connection in connections:
nodes_list.extend(cmds.listRelatives(connection,
shapes=True))
return nodes_list
def _load_reference(self, file_type, node, path, reference_node):
"""
Load reference from 'path' on 'reference_node'. Used when change
of look (version/update) is triggered.
Args:
file_type: extension of referenced file
node:
path: (string) location of referenced file
reference_node: (string) - name of node that should be applied
on
Returns:
None
"""
import maya.cmds as cmds
try:
content = cmds.file(path,
loadReference=reference_node,
type=file_type,
returnNewNodes=True)
except RuntimeError as exc:
# When changing a reference to a file that has load errors the
# command will raise an error even if the file is still loaded
# correctly (e.g. when raising errors on Arnold attributes)
# When the file is loaded and has content, we consider it's fine.
if not cmds.referenceQuery(reference_node, isLoaded=True):
raise
content = cmds.referenceQuery(reference_node,
nodes=True,
dagPath=True)
if not content:
raise
self.log.warning("Ignoring file read error:\n%s", exc)
# Fix PLN-40 for older containers created with Avalon that had the
# `.verticesOnlySet` set to True.
if cmds.getAttr("{}.verticesOnlySet".format(node)):
self.log.info("Setting %s.verticesOnlySet to False", node)
cmds.setAttr("{}.verticesOnlySet".format(node), False)
return nodes_list