From 2a7d05e090b6b3a5d30f52f7006f57f9137603bd Mon Sep 17 00:00:00 2001 From: Toke Stuart Jepsen Date: Tue, 20 Dec 2022 07:25:00 +0000 Subject: [PATCH] Working destructive updating --- openpype/hosts/maya/plugins/load/load_xgen.py | 63 ++++++++++++++++--- .../maya/plugins/publish/extract_xgen.py | 13 ++-- 2 files changed, 60 insertions(+), 16 deletions(-) diff --git a/openpype/hosts/maya/plugins/load/load_xgen.py b/openpype/hosts/maya/plugins/load/load_xgen.py index 3b7451f95c..2d58550bc7 100644 --- a/openpype/hosts/maya/plugins/load/load_xgen.py +++ b/openpype/hosts/maya/plugins/load/load_xgen.py @@ -2,11 +2,15 @@ import os import shutil import maya.cmds as cmds -import pymel.core as pm +import xgenm import openpype.hosts.maya.api.plugin -from openpype.hosts.maya.api.lib import maintained_selection +from openpype.hosts.maya.api.lib import ( + maintained_selection, get_container_members +) from openpype.hosts.maya.api import current_file +from openpype.hosts.maya.api.plugin import get_reference_node +from openpype.pipeline import get_representation_path class XgenLoader(openpype.hosts.maya.api.plugin.ReferenceLoader): @@ -19,13 +23,10 @@ class XgenLoader(openpype.hosts.maya.api.plugin.ReferenceLoader): icon = "code-fork" color = "orange" - def process_reference(self, context, name, namespace, options): - maya_filepath = self.prepare_root_value( - self.fname, context["project"]["name"] - ) + def setup_xgen_palette_file(self, maya_filepath, namespace, name): + # Setup xgen palette file. project_path = os.path.dirname(current_file()) - # Setup xgen palette file. # Copy the xgen palette file from published version. _, maya_extension = os.path.splitext(maya_filepath) source = maya_filepath.replace(maya_extension, ".xgen") @@ -34,9 +35,10 @@ class XgenLoader(openpype.hosts.maya.api.plugin.ReferenceLoader): "{basename}__{namespace}__{name}.xgen".format( basename=os.path.splitext(os.path.basename(current_file()))[0], namespace=namespace, - name=context["representation"]["data"]["xgenName"] + name=name ) - ) + ).replace("\\", "/") + self.log.info("Copying {} to {}".format(source, destination)) shutil.copy(source, destination) # Modify xgDataPath and xgProjectPath to have current workspace first @@ -66,6 +68,18 @@ class XgenLoader(openpype.hosts.maya.api.plugin.ReferenceLoader): with open(destination, "w") as f: f.write("\n".join(lines)) + return destination + + def process_reference(self, context, name, namespace, options): + maya_filepath = self.prepare_root_value( + self.fname, context["project"]["name"] + ) + + name = context["representation"]["data"]["xgenName"] + xgen_file = self.setup_xgen_palette_file( + maya_filepath, namespace, name + ) + # Reference xgen. Xgen does not like being referenced in under a group. new_nodes = [] @@ -78,6 +92,13 @@ class XgenLoader(openpype.hosts.maya.api.plugin.ReferenceLoader): returnNewNodes=True ) + xgen_palette = cmds.ls(nodes, type="xgmPalette", long=True)[0] + cmds.setAttr( + "{}.xgFileName".format(xgen_palette), + os.path.basename(xgen_file), + type="string" + ) + shapes = cmds.ls(nodes, shapes=True, long=True) new_nodes = (list(set(nodes) - set(shapes))) @@ -87,4 +108,26 @@ class XgenLoader(openpype.hosts.maya.api.plugin.ReferenceLoader): return new_nodes def update(self, container, representation): - pass + super().update(container, representation) + + # Get reference node from container members + node = container["objectName"] + members = get_container_members(node) + reference_node = get_reference_node(members, self.log) + namespace = cmds.referenceQuery(reference_node, namespace=True)[1:] + + xgen_file = self.setup_xgen_palette_file( + get_representation_path(representation), + namespace, + representation["data"]["xgenName"] + ) + + xgen_palette = cmds.ls(members, type="xgmPalette", long=True)[0] + cmds.setAttr( + "{}.xgFileName".format(xgen_palette), + os.path.basename(xgen_file), + type="string" + ) + + # Reload reference to update the xgen. + cmds.file(loadReference=reference_node, force=True) diff --git a/openpype/hosts/maya/plugins/publish/extract_xgen.py b/openpype/hosts/maya/plugins/publish/extract_xgen.py index 69d1d3db40..2daa6e238d 100644 --- a/openpype/hosts/maya/plugins/publish/extract_xgen.py +++ b/openpype/hosts/maya/plugins/publish/extract_xgen.py @@ -32,13 +32,10 @@ class ExtractXgenCache(publish.Extractor): templates = instance.context.data["anatomy"].templates["publish"] xgen_filename = StringTemplate(templates["file"]).format(template_data) name = instance.data["xgenPalette"].replace(":", "__").replace("|", "") - value = xgen_filename.replace(".xgen", "__" + name + ".xgen") - attribute_data = { - "{}.xgFileName".format(instance.data["xgenPalette"]): xgen_filename - } + xgen_filename = xgen_filename.replace(".xgen", "__" + name + ".xgen") # Export xgen palette files. - xgen_path = os.path.join(staging_dir, value).replace("\\", "/") + xgen_path = os.path.join(staging_dir, xgen_filename).replace("\\", "/") xgenm.exportPalette( instance.data["xgenPalette"].replace("|", ""), xgen_path ) @@ -47,7 +44,7 @@ class ExtractXgenCache(publish.Extractor): representation = { "name": name, "ext": "xgen", - "files": value, + "files": xgen_filename, "stagingDir": staging_dir, } instance.data["representations"].append(representation) @@ -83,6 +80,10 @@ class ExtractXgenCache(publish.Extractor): cmds.select(duplicate_nodes) collection = xgenm.importPalette(xgen_path, []) + attribute_data = { + "{}.xgFileName".format(collection): xgen_filename + } + # Export Maya file. type = "mayaAscii" if self.scene_type == "ma" else "mayaBinary" with attribute_values(attribute_data):