From 6af973fc8fc0c1ad5a5adf0996ef64c413629fce Mon Sep 17 00:00:00 2001 From: Toke Stuart Jepsen Date: Wed, 14 Dec 2022 08:59:14 +0000 Subject: [PATCH 01/67] Adjust xgen creation --- openpype/hosts/maya/plugins/create/create_xgen.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/openpype/hosts/maya/plugins/create/create_xgen.py b/openpype/hosts/maya/plugins/create/create_xgen.py index 8672c06a1e..70e23cf47b 100644 --- a/openpype/hosts/maya/plugins/create/create_xgen.py +++ b/openpype/hosts/maya/plugins/create/create_xgen.py @@ -2,9 +2,9 @@ from openpype.hosts.maya.api import plugin class CreateXgen(plugin.Creator): - """Xgen interactive export""" + """Xgen""" name = "xgen" - label = "Xgen Interactive" + label = "Xgen" family = "xgen" icon = "pagelines" From 91d935c948c18ddf85dbfa54db924907b0067fcb Mon Sep 17 00:00:00 2001 From: Toke Stuart Jepsen Date: Wed, 14 Dec 2022 08:59:26 +0000 Subject: [PATCH 02/67] Validate xgen --- .../maya/plugins/publish/validate_xgen.py | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 openpype/hosts/maya/plugins/publish/validate_xgen.py diff --git a/openpype/hosts/maya/plugins/publish/validate_xgen.py b/openpype/hosts/maya/plugins/publish/validate_xgen.py new file mode 100644 index 0000000000..7ccd02bb07 --- /dev/null +++ b/openpype/hosts/maya/plugins/publish/validate_xgen.py @@ -0,0 +1,28 @@ +import maya.cmds as cmds + +import pyblish.api + + +class ValidateXgen(pyblish.api.InstancePlugin): + """Ensure Xgen objectset only contains collections.""" + + label = "Xgen" + order = pyblish.api.ValidatorOrder + host = ["maya"] + families = ["xgen"] + + def process(self, instance): + nodes = ( + cmds.ls(instance, type="xgmPalette", long=True) + + cmds.ls(instance, type="transform", long=True) + + cmds.ls(instance, type="xgmDescription", long=True) + + cmds.ls(instance, type="xgmSubdPatch", long=True) + ) + remainder_nodes = [] + for node in instance: + if node in nodes: + continue + remainder_nodes.append(node) + + msg = "Invalid nodes in the objectset:\n{}".format(remainder_nodes) + assert not remainder_nodes, msg From 94f0f773f865857da894e824630fdacec8666b4c Mon Sep 17 00:00:00 2001 From: Toke Stuart Jepsen Date: Sun, 18 Dec 2022 21:08:25 +0000 Subject: [PATCH 03/67] Exclude xgen from default Maya loading. --- openpype/hosts/maya/plugins/load/actions.py | 15 ++++++++++++++- .../hosts/maya/plugins/load/load_reference.py | 1 - 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/openpype/hosts/maya/plugins/load/actions.py b/openpype/hosts/maya/plugins/load/actions.py index 9cc9180d6e..ced0f7ccbb 100644 --- a/openpype/hosts/maya/plugins/load/actions.py +++ b/openpype/hosts/maya/plugins/load/actions.py @@ -93,7 +93,20 @@ class ImportMayaLoader(load.LoaderPlugin): """ representations = ["ma", "mb", "obj"] - families = ["*"] + families = [ + "model", + "pointcache", + "proxyAbc", + "animation", + "mayaAscii", + "mayaScene", + "setdress", + "layout", + "camera", + "rig", + "camerarig", + "staticMesh" + ] label = "Import" order = 10 diff --git a/openpype/hosts/maya/plugins/load/load_reference.py b/openpype/hosts/maya/plugins/load/load_reference.py index c6b07b036d..31a36a587c 100644 --- a/openpype/hosts/maya/plugins/load/load_reference.py +++ b/openpype/hosts/maya/plugins/load/load_reference.py @@ -25,7 +25,6 @@ class ReferenceLoader(openpype.hosts.maya.api.plugin.ReferenceLoader): "camera", "rig", "camerarig", - "xgen", "staticMesh"] representations = ["ma", "abc", "fbx", "mb"] From bf2d4ee323eb3df631cdbb93fabeb58242da0886 Mon Sep 17 00:00:00 2001 From: Toke Stuart Jepsen Date: Sun, 18 Dec 2022 21:09:55 +0000 Subject: [PATCH 04/67] Initial working publish of xgen --- .../maya/plugins/publish/collect_xgen.py | 39 +++++ .../plugins/publish/extract_maya_scene_raw.py | 3 +- .../maya/plugins/publish/extract_xgen.py | 138 ++++++++++++++++++ .../maya/plugins/publish/validate_xgen.py | 11 +- .../plugins/publish/collect_resources_path.py | 3 +- 5 files changed, 185 insertions(+), 9 deletions(-) create mode 100644 openpype/hosts/maya/plugins/publish/collect_xgen.py create mode 100644 openpype/hosts/maya/plugins/publish/extract_xgen.py diff --git a/openpype/hosts/maya/plugins/publish/collect_xgen.py b/openpype/hosts/maya/plugins/publish/collect_xgen.py new file mode 100644 index 0000000000..fa7ec3776d --- /dev/null +++ b/openpype/hosts/maya/plugins/publish/collect_xgen.py @@ -0,0 +1,39 @@ +from maya import cmds + +import pyblish.api +from openpype.hosts.maya.api.lib import get_attribute_input + + +class CollectXgen(pyblish.api.InstancePlugin): + """Collect Xgen""" + + order = pyblish.api.CollectorOrder + 0.499999 + label = "Collect Xgen" + families = ["xgen"] + + def process(self, instance): + data = { + "xgenPalettes": cmds.ls(instance, type="xgmPalette", long=True), + "xgmDescriptions": cmds.ls( + instance, type="xgmDescription", long=True + ), + "xgmSubdPatches": cmds.ls(instance, type="xgmSubdPatch", long=True) + } + data["xgenNodes"] = ( + data["xgenPalettes"] + + data["xgmDescriptions"] + + data["xgmSubdPatches"] + ) + + if data["xgenPalettes"]: + data["xgenPalette"] = data["xgenPalettes"][0] + + data["xgenConnections"] = {} + for node in data["xgmSubdPatches"]: + data["xgenConnections"][node] = {} + for attr in ["transform", "geometry"]: + input = get_attribute_input("{}.{}".format(node, attr)) + data["xgenConnections"][node][attr] = input + + self.log.info(data) + instance.data.update(data) diff --git a/openpype/hosts/maya/plugins/publish/extract_maya_scene_raw.py b/openpype/hosts/maya/plugins/publish/extract_maya_scene_raw.py index 3769ec3605..c2411ca651 100644 --- a/openpype/hosts/maya/plugins/publish/extract_maya_scene_raw.py +++ b/openpype/hosts/maya/plugins/publish/extract_maya_scene_raw.py @@ -20,8 +20,7 @@ class ExtractMayaSceneRaw(publish.Extractor): "mayaScene", "setdress", "layout", - "camerarig", - "xgen"] + "camerarig"] scene_type = "ma" def process(self, instance): diff --git a/openpype/hosts/maya/plugins/publish/extract_xgen.py b/openpype/hosts/maya/plugins/publish/extract_xgen.py new file mode 100644 index 0000000000..69d1d3db40 --- /dev/null +++ b/openpype/hosts/maya/plugins/publish/extract_xgen.py @@ -0,0 +1,138 @@ +import os +import copy + +from maya import cmds +import pymel.core as pc +import xgenm + +from openpype.pipeline import publish +from openpype.hosts.maya.api.lib import maintained_selection, attribute_values +from openpype.lib import StringTemplate + + +class ExtractXgenCache(publish.Extractor): + """Extract Xgen""" + + label = "Extract Xgen" + hosts = ["maya"] + families = ["xgen"] + scene_type = "ma" + + def process(self, instance): + if "representations" not in instance.data: + instance.data["representations"] = [] + + staging_dir = self.staging_dir(instance) + maya_filename = "{}.{}".format(instance.data["name"], self.scene_type) + maya_filepath = os.path.join(staging_dir, maya_filename) + + # Get published xgen file name. + template_data = copy.deepcopy(instance.data["anatomyData"]) + template_data.update({"ext": "xgen"}) + 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 + } + + # Export xgen palette files. + xgen_path = os.path.join(staging_dir, value).replace("\\", "/") + xgenm.exportPalette( + instance.data["xgenPalette"].replace("|", ""), xgen_path + ) + self.log.info("Extracted to {}".format(xgen_path)) + + representation = { + "name": name, + "ext": "xgen", + "files": value, + "stagingDir": staging_dir, + } + instance.data["representations"].append(representation) + + # Collect nodes to export. + duplicate_nodes = [] + for node, connections in instance.data["xgenConnections"].items(): + transform_name = connections["transform"].split(".")[0] + + # Duplicate_transform subd patch geometry. + duplicate_transform = pc.duplicate(transform_name)[0] + pc.parent(duplicate_transform, world=True) + duplicate_transform.name(stripNamespace=True) + duplicate_shape = pc.listRelatives( + duplicate_transform, shapes=True + )[0] + + pc.connectAttr( + "{}.matrix".format(duplicate_transform), + "{}.transform".format(node), + force=True + ) + pc.connectAttr( + "{}.worldMesh".format(duplicate_shape), + "{}.geometry".format(node), + force=True + ) + + duplicate_nodes.append(duplicate_transform) + + # Import xgen onto the duplicate. + with maintained_selection(): + cmds.select(duplicate_nodes) + collection = xgenm.importPalette(xgen_path, []) + + # Export Maya file. + type = "mayaAscii" if self.scene_type == "ma" else "mayaBinary" + with attribute_values(attribute_data): + with maintained_selection(): + cmds.select(duplicate_nodes + [collection]) + cmds.file( + maya_filepath, + force=True, + type=type, + exportSelected=True, + preserveReferences=True, + constructionHistory=True, + shader=True, + constraints=True, + expressions=True + ) + + self.log.info("Extracted to {}".format(maya_filepath)) + + representation = { + "name": self.scene_type, + "ext": self.scene_type, + "files": maya_filename, + "stagingDir": staging_dir, + "data": {"xgenName": collection} + } + instance.data["representations"].append(representation) + + # Revert to original xgen connections. + for node, connections in instance.data["xgenConnections"].items(): + for attr, src in connections.items(): + cmds.connectAttr( + src, "{}.{}".format(node, attr), force=True + ) + + cmds.delete(duplicate_nodes + [collection]) + + # Setup transfers. + #needs to reduce resources to only what is used for the collections in + #the objectset + xgen_dir = os.path.join( + os.path.dirname(instance.context.data["currentFile"]), "xgen" + ) + transfers = [] + for root, dirs, files in os.walk(xgen_dir): + for file in files: + source = os.path.join(root, file) + destination = source.replace( + xgen_dir, instance.data["resourcesDir"] + ) + transfers.append((source, destination)) + + instance.data["transfers"] = transfers diff --git a/openpype/hosts/maya/plugins/publish/validate_xgen.py b/openpype/hosts/maya/plugins/publish/validate_xgen.py index 7ccd02bb07..fc93f62846 100644 --- a/openpype/hosts/maya/plugins/publish/validate_xgen.py +++ b/openpype/hosts/maya/plugins/publish/validate_xgen.py @@ -4,19 +4,18 @@ import pyblish.api class ValidateXgen(pyblish.api.InstancePlugin): - """Ensure Xgen objectset only contains collections.""" + """Validate Xgen data.""" - label = "Xgen" + label = "Validate Xgen" order = pyblish.api.ValidatorOrder host = ["maya"] families = ["xgen"] def process(self, instance): + # Validate only xgen collections are in objectset. nodes = ( - cmds.ls(instance, type="xgmPalette", long=True) + - cmds.ls(instance, type="transform", long=True) + - cmds.ls(instance, type="xgmDescription", long=True) + - cmds.ls(instance, type="xgmSubdPatch", long=True) + instance.data["xgenNodes"] + + cmds.ls(instance, type="transform", long=True) ) remainder_nodes = [] for node in instance: diff --git a/openpype/plugins/publish/collect_resources_path.py b/openpype/plugins/publish/collect_resources_path.py index a2d5b95ab2..52aba0eca3 100644 --- a/openpype/plugins/publish/collect_resources_path.py +++ b/openpype/plugins/publish/collect_resources_path.py @@ -57,7 +57,8 @@ class CollectResourcesPath(pyblish.api.InstancePlugin): "background", "effect", "staticMesh", - "skeletalMesh" + "skeletalMesh", + "xgen" ] def process(self, instance): From a1890ee04d9b06031a9c136a276ffba70b2f67b3 Mon Sep 17 00:00:00 2001 From: Toke Stuart Jepsen Date: Sun, 18 Dec 2022 21:10:07 +0000 Subject: [PATCH 05/67] Initial working loading of xgen --- openpype/hosts/maya/plugins/load/load_xgen.py | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 openpype/hosts/maya/plugins/load/load_xgen.py diff --git a/openpype/hosts/maya/plugins/load/load_xgen.py b/openpype/hosts/maya/plugins/load/load_xgen.py new file mode 100644 index 0000000000..3b7451f95c --- /dev/null +++ b/openpype/hosts/maya/plugins/load/load_xgen.py @@ -0,0 +1,90 @@ +import os +import shutil + +import maya.cmds as cmds +import pymel.core as pm + +import openpype.hosts.maya.api.plugin +from openpype.hosts.maya.api.lib import maintained_selection +from openpype.hosts.maya.api import current_file + + +class XgenLoader(openpype.hosts.maya.api.plugin.ReferenceLoader): + """Load Xgen as reference""" + + families = ["xgen"] + representations = ["ma", "mb"] + + label = "Reference Xgen" + icon = "code-fork" + color = "orange" + + def process_reference(self, context, name, namespace, options): + maya_filepath = self.prepare_root_value( + self.fname, context["project"]["name"] + ) + 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") + destination = os.path.join( + project_path, + "{basename}__{namespace}__{name}.xgen".format( + basename=os.path.splitext(os.path.basename(current_file()))[0], + namespace=namespace, + name=context["representation"]["data"]["xgenName"] + ) + ) + shutil.copy(source, destination) + + # Modify xgDataPath and xgProjectPath to have current workspace first + # and published version directory second. This ensure that any newly + # created xgen files are created in the current workspace. + resources_path = os.path.join(os.path.dirname(source), "resources") + lines = [] + with open(destination, "r") as f: + for line in [line.rstrip() for line in f]: + if line.startswith("\txgDataPath"): + data_path = line.split("\t")[-1] + line = "\txgDataPath\t\t{}{}{}".format( + data_path, + os.pathsep, + data_path.replace( + "${PROJECT}xgen", resources_path.replace("\\", "/") + ) + ) + + if line.startswith("\txgProjectPath"): + line = "\txgProjectPath\t\t{}/".format( + project_path.replace("\\", "/") + ) + + lines.append(line) + + with open(destination, "w") as f: + f.write("\n".join(lines)) + + # Reference xgen. Xgen does not like being referenced in under a group. + new_nodes = [] + + with maintained_selection(): + nodes = cmds.file( + maya_filepath, + namespace=namespace, + sharedReferenceFile=False, + reference=True, + returnNewNodes=True + ) + + shapes = cmds.ls(nodes, shapes=True, long=True) + + new_nodes = (list(set(nodes) - set(shapes))) + + self[:] = new_nodes + + return new_nodes + + def update(self, container, representation): + pass From 2a7d05e090b6b3a5d30f52f7006f57f9137603bd Mon Sep 17 00:00:00 2001 From: Toke Stuart Jepsen Date: Tue, 20 Dec 2022 07:25:00 +0000 Subject: [PATCH 06/67] 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): From 89ea46189e3f8fe6df6006804ec84227003641ad Mon Sep 17 00:00:00 2001 From: Toke Stuart Jepsen Date: Tue, 20 Dec 2022 11:46:16 +0000 Subject: [PATCH 07/67] Initial non-destructive updating --- openpype/hosts/maya/plugins/load/load_xgen.py | 56 ++++++++++++++++--- 1 file changed, 49 insertions(+), 7 deletions(-) diff --git a/openpype/hosts/maya/plugins/load/load_xgen.py b/openpype/hosts/maya/plugins/load/load_xgen.py index 2d58550bc7..99673440fd 100644 --- a/openpype/hosts/maya/plugins/load/load_xgen.py +++ b/openpype/hosts/maya/plugins/load/load_xgen.py @@ -79,6 +79,22 @@ class XgenLoader(openpype.hosts.maya.api.plugin.ReferenceLoader): xgen_file = self.setup_xgen_palette_file( maya_filepath, namespace, name ) + xgd_file = xgen_file.replace(".xgen", ".xgd") + + # Create a placeholder xgen delta file. + #change author and date + xgd_template = """ +# XGen Delta File +# +# Version: C:/Program Files/Autodesk/Maya2022/plug-ins/xgen/ +# Author: tokejepsen +# Date: Tue Dec 20 09:03:29 2022 + +FileVersion 18 + +""" + with open(xgd_file, "w") as f: + f.write(xgd_template) # Reference xgen. Xgen does not like being referenced in under a group. new_nodes = [] @@ -94,10 +110,16 @@ class XgenLoader(openpype.hosts.maya.api.plugin.ReferenceLoader): xgen_palette = cmds.ls(nodes, type="xgmPalette", long=True)[0] cmds.setAttr( - "{}.xgFileName".format(xgen_palette), + "{}.xgBaseFile".format(xgen_palette), os.path.basename(xgen_file), type="string" ) + cmds.setAttr( + "{}.xgFileName".format(xgen_palette), + os.path.basename(xgd_file), + type="string" + ) + cmds.setAttr("{}.xgExportAsDelta".format(xgen_palette), True) shapes = cmds.ls(nodes, shapes=True, long=True) @@ -108,11 +130,9 @@ class XgenLoader(openpype.hosts.maya.api.plugin.ReferenceLoader): return new_nodes def update(self, container, representation): - super().update(container, representation) - # Get reference node from container members - node = container["objectName"] - members = get_container_members(node) + container_node = container["objectName"] + members = get_container_members(container_node) reference_node = get_reference_node(members, self.log) namespace = cmds.referenceQuery(reference_node, namespace=True)[1:] @@ -121,13 +141,35 @@ class XgenLoader(openpype.hosts.maya.api.plugin.ReferenceLoader): namespace, representation["data"]["xgenName"] ) + xgd_file = xgen_file.replace(".xgen", ".xgd") xgen_palette = cmds.ls(members, type="xgmPalette", long=True)[0] + cmds.setAttr( "{}.xgFileName".format(xgen_palette), os.path.basename(xgen_file), type="string" ) + cmds.setAttr( + "{}.xgBaseFile".format(xgen_palette), + "", + type="string" + ) + cmds.setAttr("{}.xgExportAsDelta".format(xgen_palette), False) - # Reload reference to update the xgen. - cmds.file(loadReference=reference_node, force=True) + super().update(container, representation) + + # Apply xgen delta. + xgenm.applyDelta(xgen_palette.replace("|", ""), xgd_file) + + cmds.setAttr( + "{}.xgBaseFile".format(xgen_palette), + os.path.basename(xgen_file), + type="string" + ) + cmds.setAttr( + "{}.xgFileName".format(xgen_palette), + os.path.basename(xgd_file), + type="string" + ) + cmds.setAttr("{}.xgExportAsDelta".format(xgen_palette), True) From c58c0d98784d3579b72bfa721d0dcb35442bf296 Mon Sep 17 00:00:00 2001 From: Toke Stuart Jepsen Date: Tue, 20 Dec 2022 11:52:39 +0000 Subject: [PATCH 08/67] Refactor --- openpype/hosts/maya/plugins/load/load_xgen.py | 37 +++++-------------- 1 file changed, 10 insertions(+), 27 deletions(-) diff --git a/openpype/hosts/maya/plugins/load/load_xgen.py b/openpype/hosts/maya/plugins/load/load_xgen.py index 99673440fd..61204522e6 100644 --- a/openpype/hosts/maya/plugins/load/load_xgen.py +++ b/openpype/hosts/maya/plugins/load/load_xgen.py @@ -6,7 +6,7 @@ import xgenm import openpype.hosts.maya.api.plugin from openpype.hosts.maya.api.lib import ( - maintained_selection, get_container_members + maintained_selection, get_container_members, attribute_values ) from openpype.hosts.maya.api import current_file from openpype.hosts.maya.api.plugin import get_reference_node @@ -145,31 +145,14 @@ FileVersion 18 xgen_palette = cmds.ls(members, type="xgmPalette", long=True)[0] - cmds.setAttr( - "{}.xgFileName".format(xgen_palette), - os.path.basename(xgen_file), - type="string" - ) - cmds.setAttr( - "{}.xgBaseFile".format(xgen_palette), - "", - type="string" - ) - cmds.setAttr("{}.xgExportAsDelta".format(xgen_palette), False) + attribute_data = { + "{}.xgFileName".format(xgen_palette): os.path.basename(xgen_file), + "{}.xgBaseFile".format(xgen_palette): "", + "{}.xgExportAsDelta".format(xgen_palette): False + } - super().update(container, representation) + with attribute_values(attribute_data): + super().update(container, representation) - # Apply xgen delta. - xgenm.applyDelta(xgen_palette.replace("|", ""), xgd_file) - - cmds.setAttr( - "{}.xgBaseFile".format(xgen_palette), - os.path.basename(xgen_file), - type="string" - ) - cmds.setAttr( - "{}.xgFileName".format(xgen_palette), - os.path.basename(xgd_file), - type="string" - ) - cmds.setAttr("{}.xgExportAsDelta".format(xgen_palette), True) + # Apply xgen delta. + xgenm.applyDelta(xgen_palette.replace("|", ""), xgd_file) From 55e3ae2729248c5d9db380fa2f52c20624f3c892 Mon Sep 17 00:00:00 2001 From: Toke Stuart Jepsen Date: Wed, 21 Dec 2022 09:23:57 +0000 Subject: [PATCH 09/67] Fix xgd missing file --- openpype/hosts/maya/plugins/load/load_xgen.py | 26 +++++++++++++++---- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/openpype/hosts/maya/plugins/load/load_xgen.py b/openpype/hosts/maya/plugins/load/load_xgen.py index 61204522e6..f6b487e7e8 100644 --- a/openpype/hosts/maya/plugins/load/load_xgen.py +++ b/openpype/hosts/maya/plugins/load/load_xgen.py @@ -81,8 +81,12 @@ class XgenLoader(openpype.hosts.maya.api.plugin.ReferenceLoader): ) xgd_file = xgen_file.replace(".xgen", ".xgd") - # Create a placeholder xgen delta file. - #change author and date + # Create a placeholder xgen delta file. This create an expression + # attribute of float. If we did not add any changes to the delta file, + # then Xgen deletes the xgd file on save to clean up (?). This gives + # errors when launching the workfile again due to trying to find the + # xgd file. + #change author, date and version path xgd_template = """ # XGen Delta File # @@ -92,6 +96,8 @@ class XgenLoader(openpype.hosts.maya.api.plugin.ReferenceLoader): FileVersion 18 +Palette custom_float_ignore 0 + """ with open(xgd_file, "w") as f: f.write(xgd_template) @@ -130,7 +136,19 @@ FileVersion 18 return new_nodes def update(self, container, representation): - # Get reference node from container members + """Workflow for updating Xgen. + + - Copy and overwrite the workspace .xgen file. + - Set collection attributes to not include delta files. + - Update xgen maya file reference. + - Apply the delta file changes. + - Reset collection attributes to include delta files. + + We have to do this workflow because when using referencing of the xgen + collection, Maya implicitly imports the Xgen data from the xgen file so + we dont have any control over added the delta file changes. + """ + container_node = container["objectName"] members = get_container_members(container_node) reference_node = get_reference_node(members, self.log) @@ -150,9 +168,7 @@ FileVersion 18 "{}.xgBaseFile".format(xgen_palette): "", "{}.xgExportAsDelta".format(xgen_palette): False } - with attribute_values(attribute_data): super().update(container, representation) - # Apply xgen delta. xgenm.applyDelta(xgen_palette.replace("|", ""), xgd_file) From 87e72491898df9a29c0b44bc044c9f6e08de40fc Mon Sep 17 00:00:00 2001 From: Toke Stuart Jepsen Date: Wed, 21 Dec 2022 11:03:50 +0000 Subject: [PATCH 10/67] Avoid making placeholder xgd file --- openpype/hosts/maya/plugins/load/load_xgen.py | 29 +++++-------------- 1 file changed, 8 insertions(+), 21 deletions(-) diff --git a/openpype/hosts/maya/plugins/load/load_xgen.py b/openpype/hosts/maya/plugins/load/load_xgen.py index f6b487e7e8..23a22127e9 100644 --- a/openpype/hosts/maya/plugins/load/load_xgen.py +++ b/openpype/hosts/maya/plugins/load/load_xgen.py @@ -81,27 +81,6 @@ class XgenLoader(openpype.hosts.maya.api.plugin.ReferenceLoader): ) xgd_file = xgen_file.replace(".xgen", ".xgd") - # Create a placeholder xgen delta file. This create an expression - # attribute of float. If we did not add any changes to the delta file, - # then Xgen deletes the xgd file on save to clean up (?). This gives - # errors when launching the workfile again due to trying to find the - # xgd file. - #change author, date and version path - xgd_template = """ -# XGen Delta File -# -# Version: C:/Program Files/Autodesk/Maya2022/plug-ins/xgen/ -# Author: tokejepsen -# Date: Tue Dec 20 09:03:29 2022 - -FileVersion 18 - -Palette custom_float_ignore 0 - -""" - with open(xgd_file, "w") as f: - f.write(xgd_template) - # Reference xgen. Xgen does not like being referenced in under a group. new_nodes = [] @@ -127,6 +106,14 @@ Palette custom_float_ignore 0 ) cmds.setAttr("{}.xgExportAsDelta".format(xgen_palette), True) + # This create an expression attribute of float. If we did not add + # any changes to collection, then Xgen does not create an xgd file + # on save. This gives errors when launching the workfile again due + # to trying to find the xgd file. + xgenm.addCustomAttr( + "custom_float_ignore", xgen_palette.replace("|", "") + ) + shapes = cmds.ls(nodes, shapes=True, long=True) new_nodes = (list(set(nodes) - set(shapes))) From 2c9613b55e67e2854e149849dc3a057fc6583591 Mon Sep 17 00:00:00 2001 From: Toke Stuart Jepsen Date: Wed, 21 Dec 2022 11:38:22 +0000 Subject: [PATCH 11/67] Validate against deactive modifiers --- .../maya/plugins/publish/validate_xgen.py | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/openpype/hosts/maya/plugins/publish/validate_xgen.py b/openpype/hosts/maya/plugins/publish/validate_xgen.py index fc93f62846..9393520d6d 100644 --- a/openpype/hosts/maya/plugins/publish/validate_xgen.py +++ b/openpype/hosts/maya/plugins/publish/validate_xgen.py @@ -1,4 +1,7 @@ +import json + import maya.cmds as cmds +import xgenm import pyblish.api @@ -25,3 +28,27 @@ class ValidateXgen(pyblish.api.InstancePlugin): msg = "Invalid nodes in the objectset:\n{}".format(remainder_nodes) assert not remainder_nodes, msg + + # Cant have deactive modifiers in collection cause Xgen will try and + # look for them when loading. + palette = instance.data["xgenPalette"].replace("|", "") + deactive_modifiers = {} + for description in instance.data["xgmDescriptions"]: + description = description.split("|")[-2] + modifier_names = xgenm.fxModules(palette, description) + for name in modifier_names: + attr = xgenm.getAttr("active", palette, description, name) + # Attribute value are lowercase strings of false/true. + if attr == "false": + try: + deactive_modifiers[description].append(name) + except KeyError: + deactive_modifiers[description] = [name] + + msg = ( + "There are deactive modifiers on the collection. " + "Please delete these:\n{}".format( + json.dumps(deactive_modifiers, indent=4, sort_keys=True) + ) + ) + assert not deactive_modifiers, msg From 0fc1e357a35c32bf505f4f1661eb631ffe340585 Mon Sep 17 00:00:00 2001 From: Toke Stuart Jepsen Date: Wed, 21 Dec 2022 12:19:23 +0000 Subject: [PATCH 12/67] Validate against multiple collections per instance. --- openpype/hosts/maya/plugins/publish/validate_xgen.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/openpype/hosts/maya/plugins/publish/validate_xgen.py b/openpype/hosts/maya/plugins/publish/validate_xgen.py index 9393520d6d..1ef85c4cc6 100644 --- a/openpype/hosts/maya/plugins/publish/validate_xgen.py +++ b/openpype/hosts/maya/plugins/publish/validate_xgen.py @@ -29,6 +29,13 @@ class ValidateXgen(pyblish.api.InstancePlugin): msg = "Invalid nodes in the objectset:\n{}".format(remainder_nodes) assert not remainder_nodes, msg + # Only one collection per instance. + palette_amount = len(instance.data["xgenPalettes"]) + msg = "Only one collection per instance allow. Found {}:\n{}".format( + palette_amount, instance.data["xgenPalettes"] + ) + assert palette_amount == 1, msg + # Cant have deactive modifiers in collection cause Xgen will try and # look for them when loading. palette = instance.data["xgenPalette"].replace("|", "") From 1bb6d77e4eab876b354f14637f70ad3a2872c777 Mon Sep 17 00:00:00 2001 From: Toke Stuart Jepsen Date: Wed, 21 Dec 2022 12:37:24 +0000 Subject: [PATCH 13/67] Hound --- openpype/hosts/maya/plugins/publish/extract_xgen.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/openpype/hosts/maya/plugins/publish/extract_xgen.py b/openpype/hosts/maya/plugins/publish/extract_xgen.py index 2daa6e238d..4979f583b4 100644 --- a/openpype/hosts/maya/plugins/publish/extract_xgen.py +++ b/openpype/hosts/maya/plugins/publish/extract_xgen.py @@ -122,13 +122,11 @@ class ExtractXgenCache(publish.Extractor): cmds.delete(duplicate_nodes + [collection]) # Setup transfers. - #needs to reduce resources to only what is used for the collections in - #the objectset xgen_dir = os.path.join( os.path.dirname(instance.context.data["currentFile"]), "xgen" ) transfers = [] - for root, dirs, files in os.walk(xgen_dir): + for root, _, files in os.walk(xgen_dir): for file in files: source = os.path.join(root, file) destination = source.replace( From 2875bb81fbe98d9504f582b05ae75033360ba140 Mon Sep 17 00:00:00 2001 From: Toke Jepsen Date: Wed, 21 Dec 2022 12:51:49 +0000 Subject: [PATCH 14/67] Update openpype/hosts/maya/plugins/publish/extract_xgen.py Co-authored-by: Roy Nieterau --- .../hosts/maya/plugins/publish/extract_xgen.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/openpype/hosts/maya/plugins/publish/extract_xgen.py b/openpype/hosts/maya/plugins/publish/extract_xgen.py index 4979f583b4..003b6aaa85 100644 --- a/openpype/hosts/maya/plugins/publish/extract_xgen.py +++ b/openpype/hosts/maya/plugins/publish/extract_xgen.py @@ -55,24 +55,26 @@ class ExtractXgenCache(publish.Extractor): transform_name = connections["transform"].split(".")[0] # Duplicate_transform subd patch geometry. - duplicate_transform = pc.duplicate(transform_name)[0] - pc.parent(duplicate_transform, world=True) - duplicate_transform.name(stripNamespace=True) - duplicate_shape = pc.listRelatives( - duplicate_transform, shapes=True + duplicate_transform = cmds.duplicate(transform_name)[0] + duplicate_shape = cmds.listRelatives( + duplicate_transform, + shapes=True, + fullPath=True )[0] - pc.connectAttr( + cmds.connectAttr( "{}.matrix".format(duplicate_transform), "{}.transform".format(node), force=True ) - pc.connectAttr( + cmds.connectAttr( "{}.worldMesh".format(duplicate_shape), "{}.geometry".format(node), force=True ) + duplicate_transform = cmds.parent(duplicate_transform, world=True)[0] + duplicate_nodes.append(duplicate_transform) # Import xgen onto the duplicate. From b1bef86858d92239868cfcaffa8e55d5319cfecc Mon Sep 17 00:00:00 2001 From: Toke Stuart Jepsen Date: Wed, 21 Dec 2022 12:56:23 +0000 Subject: [PATCH 15/67] @BigRoy suggestion --- openpype/hosts/maya/plugins/publish/extract_xgen.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/openpype/hosts/maya/plugins/publish/extract_xgen.py b/openpype/hosts/maya/plugins/publish/extract_xgen.py index 003b6aaa85..d8045a90e7 100644 --- a/openpype/hosts/maya/plugins/publish/extract_xgen.py +++ b/openpype/hosts/maya/plugins/publish/extract_xgen.py @@ -131,9 +131,7 @@ class ExtractXgenCache(publish.Extractor): for root, _, files in os.walk(xgen_dir): for file in files: source = os.path.join(root, file) - destination = source.replace( - xgen_dir, instance.data["resourcesDir"] - ) + destination = os.path.join(instance.data["resourcesDir"], file) transfers.append((source, destination)) instance.data["transfers"] = transfers From 9fdd402fec2a6b84da8da64753bd03bb7e101ecd Mon Sep 17 00:00:00 2001 From: Toke Stuart Jepsen Date: Wed, 21 Dec 2022 12:56:52 +0000 Subject: [PATCH 16/67] Hound --- openpype/hosts/maya/plugins/publish/extract_xgen.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/openpype/hosts/maya/plugins/publish/extract_xgen.py b/openpype/hosts/maya/plugins/publish/extract_xgen.py index d8045a90e7..89b73a2d56 100644 --- a/openpype/hosts/maya/plugins/publish/extract_xgen.py +++ b/openpype/hosts/maya/plugins/publish/extract_xgen.py @@ -2,7 +2,6 @@ import os import copy from maya import cmds -import pymel.core as pc import xgenm from openpype.pipeline import publish @@ -57,8 +56,8 @@ class ExtractXgenCache(publish.Extractor): # Duplicate_transform subd patch geometry. duplicate_transform = cmds.duplicate(transform_name)[0] duplicate_shape = cmds.listRelatives( - duplicate_transform, - shapes=True, + duplicate_transform, + shapes=True, fullPath=True )[0] @@ -73,7 +72,9 @@ class ExtractXgenCache(publish.Extractor): force=True ) - duplicate_transform = cmds.parent(duplicate_transform, world=True)[0] + duplicate_transform = cmds.parent( + duplicate_transform, world=True + )[0] duplicate_nodes.append(duplicate_transform) From 2609508ced1e5076a91288d290204ac30f146c6d Mon Sep 17 00:00:00 2001 From: Toke Stuart Jepsen Date: Wed, 21 Dec 2022 13:01:16 +0000 Subject: [PATCH 17/67] deactive > inactive --- .../hosts/maya/plugins/publish/validate_xgen.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/openpype/hosts/maya/plugins/publish/validate_xgen.py b/openpype/hosts/maya/plugins/publish/validate_xgen.py index 1ef85c4cc6..442af794cd 100644 --- a/openpype/hosts/maya/plugins/publish/validate_xgen.py +++ b/openpype/hosts/maya/plugins/publish/validate_xgen.py @@ -36,10 +36,10 @@ class ValidateXgen(pyblish.api.InstancePlugin): ) assert palette_amount == 1, msg - # Cant have deactive modifiers in collection cause Xgen will try and + # Cant have inactive modifiers in collection cause Xgen will try and # look for them when loading. palette = instance.data["xgenPalette"].replace("|", "") - deactive_modifiers = {} + inactive_modifiers = {} for description in instance.data["xgmDescriptions"]: description = description.split("|")[-2] modifier_names = xgenm.fxModules(palette, description) @@ -48,14 +48,14 @@ class ValidateXgen(pyblish.api.InstancePlugin): # Attribute value are lowercase strings of false/true. if attr == "false": try: - deactive_modifiers[description].append(name) + inactive_modifiers[description].append(name) except KeyError: - deactive_modifiers[description] = [name] + inactive_modifiers[description] = [name] msg = ( - "There are deactive modifiers on the collection. " + "There are inactive modifiers on the collection. " "Please delete these:\n{}".format( - json.dumps(deactive_modifiers, indent=4, sort_keys=True) + json.dumps(inactive_modifiers, indent=4, sort_keys=True) ) ) - assert not deactive_modifiers, msg + assert not inactive_modifiers, msg From cbbb3dc8041994912ed6bf5d638ee9f06d36556d Mon Sep 17 00:00:00 2001 From: Toke Jepsen Date: Wed, 21 Dec 2022 13:04:01 +0000 Subject: [PATCH 18/67] Update openpype/hosts/maya/plugins/publish/validate_xgen.py Co-authored-by: Roy Nieterau --- openpype/hosts/maya/plugins/publish/validate_xgen.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openpype/hosts/maya/plugins/publish/validate_xgen.py b/openpype/hosts/maya/plugins/publish/validate_xgen.py index 442af794cd..bc25082aa8 100644 --- a/openpype/hosts/maya/plugins/publish/validate_xgen.py +++ b/openpype/hosts/maya/plugins/publish/validate_xgen.py @@ -16,7 +16,7 @@ class ValidateXgen(pyblish.api.InstancePlugin): def process(self, instance): # Validate only xgen collections are in objectset. - nodes = ( + nodes = set( instance.data["xgenNodes"] + cmds.ls(instance, type="transform", long=True) ) From 2a748047ed777067ada7e3b78eca8b6df0f65678 Mon Sep 17 00:00:00 2001 From: Toke Stuart Jepsen Date: Wed, 4 Jan 2023 12:41:12 +0000 Subject: [PATCH 19/67] Only collect resource files from instance collection --- .../maya/plugins/publish/extract_xgen.py | 25 +++++++++++++------ 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/openpype/hosts/maya/plugins/publish/extract_xgen.py b/openpype/hosts/maya/plugins/publish/extract_xgen.py index 89b73a2d56..5b4c6b29f2 100644 --- a/openpype/hosts/maya/plugins/publish/extract_xgen.py +++ b/openpype/hosts/maya/plugins/publish/extract_xgen.py @@ -124,15 +124,26 @@ class ExtractXgenCache(publish.Extractor): cmds.delete(duplicate_nodes + [collection]) - # Setup transfers. - xgen_dir = os.path.join( - os.path.dirname(instance.context.data["currentFile"]), "xgen" + # Collect all files under palette root as resources. + data_path = xgenm.getAttr( + "xgDataPath", instance.data["xgenPalette"].replace("|", "") + ).split(os.pathsep)[0] + data_path = data_path.replace( + "${PROJECT}", + xgenm.getAttr( + "xgProjectPath", instance.data["xgenPalette"].replace("|", "") + ) ) transfers = [] - for root, _, files in os.walk(xgen_dir): + for root, _, files in os.walk(data_path): for file in files: - source = os.path.join(root, file) - destination = os.path.join(instance.data["resourcesDir"], file) - transfers.append((source, destination)) + source = os.path.join(root, file).replace("\\", "/") + destination = os.path.join( + instance.data["resourcesDir"], + "collections", + os.path.basename(data_path), + source.replace(data_path, "")[1:] + ) + transfers.append((source, destination.replace("\\", "/"))) instance.data["transfers"] = transfers From 370bb96408c88e68273593a8ae7aabcbfff93f99 Mon Sep 17 00:00:00 2001 From: Toke Stuart Jepsen Date: Wed, 4 Jan 2023 15:03:30 +0000 Subject: [PATCH 20/67] Improve validation error message --- openpype/hosts/maya/plugins/publish/validate_xgen.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/openpype/hosts/maya/plugins/publish/validate_xgen.py b/openpype/hosts/maya/plugins/publish/validate_xgen.py index bc25082aa8..5ba9ddad52 100644 --- a/openpype/hosts/maya/plugins/publish/validate_xgen.py +++ b/openpype/hosts/maya/plugins/publish/validate_xgen.py @@ -26,7 +26,10 @@ class ValidateXgen(pyblish.api.InstancePlugin): continue remainder_nodes.append(node) - msg = "Invalid nodes in the objectset:\n{}".format(remainder_nodes) + msg = ( + "Only the collection is used when publishing. Found these invalid" + " nodes in the objectset:\n{}".format(remainder_nodes) + ) assert not remainder_nodes, msg # Only one collection per instance. From 8e233c46c8c1fceea3b0c05f0b31cf20172eb410 Mon Sep 17 00:00:00 2001 From: Toke Jepsen Date: Wed, 4 Jan 2023 16:54:25 +0000 Subject: [PATCH 21/67] Update openpype/hosts/maya/plugins/publish/validate_xgen.py Co-authored-by: Roy Nieterau --- .../maya/plugins/publish/validate_xgen.py | 20 ++++++++----------- 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/openpype/hosts/maya/plugins/publish/validate_xgen.py b/openpype/hosts/maya/plugins/publish/validate_xgen.py index 5ba9ddad52..b9d67dad25 100644 --- a/openpype/hosts/maya/plugins/publish/validate_xgen.py +++ b/openpype/hosts/maya/plugins/publish/validate_xgen.py @@ -16,21 +16,17 @@ class ValidateXgen(pyblish.api.InstancePlugin): def process(self, instance): # Validate only xgen collections are in objectset. - nodes = set( + valid_nodes = set( instance.data["xgenNodes"] + cmds.ls(instance, type="transform", long=True) ) - remainder_nodes = [] - for node in instance: - if node in nodes: - continue - remainder_nodes.append(node) - - msg = ( - "Only the collection is used when publishing. Found these invalid" - " nodes in the objectset:\n{}".format(remainder_nodes) - ) - assert not remainder_nodes, msg + invalid_nodes = [node for node in instance if node not in valid_nodes] + + if invalid_nodes: + raise KnownPublishError( + "Only the collection is used when publishing. Found these invalid" + " nodes in the objectset:\n{}".format(invalid_nodes) + ) # Only one collection per instance. palette_amount = len(instance.data["xgenPalettes"]) From 8918f8965c7d591aaec1b3da608dd8be77d68ea9 Mon Sep 17 00:00:00 2001 From: Toke Stuart Jepsen Date: Wed, 4 Jan 2023 16:56:32 +0000 Subject: [PATCH 22/67] Fix suggestion --- openpype/hosts/maya/plugins/publish/validate_xgen.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/openpype/hosts/maya/plugins/publish/validate_xgen.py b/openpype/hosts/maya/plugins/publish/validate_xgen.py index b9d67dad25..b5817d4920 100644 --- a/openpype/hosts/maya/plugins/publish/validate_xgen.py +++ b/openpype/hosts/maya/plugins/publish/validate_xgen.py @@ -4,6 +4,7 @@ import maya.cmds as cmds import xgenm import pyblish.api +from openpype.pipeline.publish import KnownPublishError class ValidateXgen(pyblish.api.InstancePlugin): @@ -21,11 +22,11 @@ class ValidateXgen(pyblish.api.InstancePlugin): cmds.ls(instance, type="transform", long=True) ) invalid_nodes = [node for node in instance if node not in valid_nodes] - + if invalid_nodes: raise KnownPublishError( - "Only the collection is used when publishing. Found these invalid" - " nodes in the objectset:\n{}".format(invalid_nodes) + "Only the collection is used when publishing. Found these " + "invalid nodes in the objectset:\n{}".format(invalid_nodes) ) # Only one collection per instance. From 6575e304b8a8127e4fa0d0989efcacb3d773be46 Mon Sep 17 00:00:00 2001 From: Toke Stuart Jepsen Date: Thu, 5 Jan 2023 18:20:03 +0000 Subject: [PATCH 23/67] Connect Geometry action --- .../plugins/inventory/connect_geometry.py | 139 ++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100644 openpype/hosts/maya/plugins/inventory/connect_geometry.py diff --git a/openpype/hosts/maya/plugins/inventory/connect_geometry.py b/openpype/hosts/maya/plugins/inventory/connect_geometry.py new file mode 100644 index 0000000000..375ad29ef0 --- /dev/null +++ b/openpype/hosts/maya/plugins/inventory/connect_geometry.py @@ -0,0 +1,139 @@ +from maya import cmds + +from openpype.pipeline import InventoryAction, get_representation_context + + +class ConnectGeometry(InventoryAction): + """Connect geometries within containers. + + Source container will connect to the target containers, by searching for + matching geometry IDs (cbid). + Source containers are of family; "animation" and "pointcache". + The connection with be done with a live world space blendshape. + """ + + label = "Connect Geometry" + icon = "link" + color = "white" + + def process(self, containers): + # Validate selection is more than 1. + message = ( + "Only 1 container selected. 2+ containers needed for this action." + ) + if len(containers) == 1: + self.display_warning(message) + return + + # Categorize containers by family. + containers_by_family = {} + for container in containers: + family = get_representation_context( + container["representation"] + )["subset"]["data"]["family"] + try: + containers_by_family[family].append(container) + except KeyError: + containers_by_family[family] = [container] + + # Validate to only 1 source container. + source_containers = containers_by_family.get("animation", []) + source_containers += containers_by_family.get("pointcache", []) + source_container_namespaces = [ + x["namespace"] for x in source_containers + ] + message = ( + "{} animation containers selected:\n\n{}\n\nOnly select 1 of type " + "\"animation\" or \"pointcache\".".format( + len(source_containers), source_container_namespaces + ) + ) + if len(source_containers) != 1: + self.display_warning(message) + return + + source_container = source_containers[0] + + # Collect matching geometry transforms based cbId attribute. + target_containers = [] + for family, containers in containers_by_family.items(): + if family in ["animation", "pointcache"]: + continue + + target_containers.extend(containers) + + source_data = self.get_container_data(source_container["objectName"]) + matches = [] + node_types = [] + for target_container in target_containers: + target_data = self.get_container_data( + target_container["objectName"] + ) + node_types.extend(target_data["node_types"]) + for id, transform in target_data["ids"].items(): + source_match = source_data["ids"].get(id) + if source_match: + matches.append([source_match, transform]) + + # Message user about what is about to happen. + if not matches: + self.display_warning("No matching geometries found.") + return + + message = "Linking geometries:\n\n" + for match in matches: + message += "{} > {}\n".format(match[0], match[1]) + + choice = self.display_warning(message, show_cancel=True) + if choice is False: + return + + # Setup live worldspace blendshape connection. + for match in matches: + source = match[0] + target = match[1] + blendshape = cmds.blendShape(source, target)[0] + cmds.setAttr(blendshape + ".origin", 0) + cmds.setAttr(blendshape + "." + target.split(":")[-1], 1) + + # Update Xgen if in any of the containers. + if "xgmPalette" in node_types: + cmds.xgmPreview() + + def get_container_data(self, container): + data = {"node_types": [], "ids": {}} + ref_node = cmds.sets(container, query=True, nodesOnly=True)[0] + for node in cmds.referenceQuery(ref_node, nodes=True): + node_type = cmds.nodeType(node) + data["node_types"].append(node_type) + if node_type == "mesh": + transform = cmds.listRelatives(node, parent=True)[0] + id = cmds.getAttr(transform + ".cbId") + data["ids"][id] = transform + + return data + + def display_warning(self, message, show_cancel=False): + """Show feedback to user. + + Returns: + bool + """ + + from Qt import QtWidgets + + accept = QtWidgets.QMessageBox.Ok + if show_cancel: + buttons = accept | QtWidgets.QMessageBox.Cancel + else: + buttons = accept + + state = QtWidgets.QMessageBox.warning( + None, + "", + message, + buttons=buttons, + defaultButton=accept + ) + + return state == accept From 2facf4d49eb3c18f3bfe39a3d50bb2bac51abca7 Mon Sep 17 00:00:00 2001 From: Toke Stuart Jepsen Date: Fri, 6 Jan 2023 10:19:21 +0000 Subject: [PATCH 24/67] Open workfile post initialization --- openpype/hooks/pre_add_last_workfile_arg.py | 12 ++++++++++ openpype/hosts/maya/startup/userSetup.py | 22 +++++++++++++++---- .../defaults/project_settings/maya.json | 1 + .../projects_schema/schema_project_maya.json | 5 +++++ 4 files changed, 36 insertions(+), 4 deletions(-) diff --git a/openpype/hooks/pre_add_last_workfile_arg.py b/openpype/hooks/pre_add_last_workfile_arg.py index 3609620917..ffb116d2e4 100644 --- a/openpype/hooks/pre_add_last_workfile_arg.py +++ b/openpype/hooks/pre_add_last_workfile_arg.py @@ -1,5 +1,7 @@ import os + from openpype.lib import PreLaunchHook +from openpype.settings import get_project_settings class AddLastWorkfileToLaunchArgs(PreLaunchHook): @@ -40,5 +42,15 @@ class AddLastWorkfileToLaunchArgs(PreLaunchHook): self.log.info("Current context does not have any workfile yet.") return + # Determine whether to open workfile post initialization. + if self.data["app"].host_name == "maya": + project_name = self.data["project_name"] + settings = get_project_settings(project_name) + key = "open_workfile_post_initialization" + if settings["maya"][key]: + self.log.debug("Opening workfile post initialization.") + self.data["env"]["OPENPYPE_" + key.upper()] = "1" + return + # Add path to workfile to arguments self.launch_context.launch_args.append(last_workfile) diff --git a/openpype/hosts/maya/startup/userSetup.py b/openpype/hosts/maya/startup/userSetup.py index 40cd51f2d8..b64ed93000 100644 --- a/openpype/hosts/maya/startup/userSetup.py +++ b/openpype/hosts/maya/startup/userSetup.py @@ -1,16 +1,27 @@ import os + from openpype.settings import get_project_settings from openpype.pipeline import install_host from openpype.hosts.maya.api import MayaHost + from maya import cmds host = MayaHost() install_host(host) -print("starting OpenPype usersetup") +print("Starting OpenPype usersetup...") -# build a shelf +# Open Workfile Post Initialization. +key = "OPENPYPE_OPEN_WORKFILE_POST_INITIALIZATION" +if bool(int(os.environ.get(key, "0"))): + cmds.evalDeferred( + "cmds.file(os.environ[\"AVALON_LAST_WORKFILE\"], open=True," + " force=True)", + lowestPriority=True + ) + +# Build a shelf. settings = get_project_settings(os.environ['AVALON_PROJECT']) shelf_preset = settings['maya'].get('project_shelf') @@ -26,7 +37,10 @@ if shelf_preset: print(import_string) exec(import_string) - cmds.evalDeferred("mlib.shelf(name=shelf_preset['name'], iconPath=icon_path, preset=shelf_preset)") + cmds.evalDeferred( + "mlib.shelf(name=shelf_preset['name'], iconPath=icon_path," + " preset=shelf_preset)" + ) -print("finished OpenPype usersetup") +print("Finished OpenPype usersetup.") diff --git a/openpype/settings/defaults/project_settings/maya.json b/openpype/settings/defaults/project_settings/maya.json index 5f40c2a10c..dbc3282ce8 100644 --- a/openpype/settings/defaults/project_settings/maya.json +++ b/openpype/settings/defaults/project_settings/maya.json @@ -1,4 +1,5 @@ { + "open_workfile_post_initialization": false, "imageio": { "colorManagementPreference_v2": { "enabled": true, diff --git a/openpype/settings/entities/schemas/projects_schema/schema_project_maya.json b/openpype/settings/entities/schemas/projects_schema/schema_project_maya.json index b2d79797a3..b668d74afd 100644 --- a/openpype/settings/entities/schemas/projects_schema/schema_project_maya.json +++ b/openpype/settings/entities/schemas/projects_schema/schema_project_maya.json @@ -5,6 +5,11 @@ "label": "Maya", "is_file": true, "children": [ + { + "type": "boolean", + "key": "open_workfile_post_initialization", + "label": "Open Workfile Post Initialization" + }, { "key": "imageio", "type": "dict", From c0da2c8afc711657b0b46eeba34b1742b9b5afa8 Mon Sep 17 00:00:00 2001 From: Toke Stuart Jepsen Date: Sat, 7 Jan 2023 09:33:38 +0000 Subject: [PATCH 25/67] Simplify validation --- .../maya/plugins/publish/validate_xgen.py | 30 +++++++++---------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/openpype/hosts/maya/plugins/publish/validate_xgen.py b/openpype/hosts/maya/plugins/publish/validate_xgen.py index b5817d4920..19cf612848 100644 --- a/openpype/hosts/maya/plugins/publish/validate_xgen.py +++ b/openpype/hosts/maya/plugins/publish/validate_xgen.py @@ -16,29 +16,27 @@ class ValidateXgen(pyblish.api.InstancePlugin): families = ["xgen"] def process(self, instance): - # Validate only xgen collections are in objectset. - valid_nodes = set( - instance.data["xgenNodes"] + - cmds.ls(instance, type="transform", long=True) - ) - invalid_nodes = [node for node in instance if node not in valid_nodes] + set_members = instance.data.get("setMembers") - if invalid_nodes: + # Only 1 collection/node per instance. + if len(set_members) != 1: raise KnownPublishError( - "Only the collection is used when publishing. Found these " - "invalid nodes in the objectset:\n{}".format(invalid_nodes) + "Only one collection per instance is allowed." + " Found:\n{}".format(set_members) ) - # Only one collection per instance. - palette_amount = len(instance.data["xgenPalettes"]) - msg = "Only one collection per instance allow. Found {}:\n{}".format( - palette_amount, instance.data["xgenPalettes"] - ) - assert palette_amount == 1, msg + # Only xgen palette node is allowed. + node_type = cmds.nodeType(set_members[0]) + if node_type != "xgmPalette": + raise KnownPublishError( + "Only node of type \"xgmPalette\" are allowed. Referred to as" + " \"collection\" in the Maya UI." + " Node type found: {}".format(node_type) + ) # Cant have inactive modifiers in collection cause Xgen will try and # look for them when loading. - palette = instance.data["xgenPalette"].replace("|", "") + palette = instance.data["xgmPalette"].replace("|", "") inactive_modifiers = {} for description in instance.data["xgmDescriptions"]: description = description.split("|")[-2] From 239995f715ea3df72af6cc9416152d8b043a3938 Mon Sep 17 00:00:00 2001 From: Toke Stuart Jepsen Date: Sat, 7 Jan 2023 09:34:20 +0000 Subject: [PATCH 26/67] Account for references and children of patches --- openpype/hosts/maya/plugins/publish/extract_xgen.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/openpype/hosts/maya/plugins/publish/extract_xgen.py b/openpype/hosts/maya/plugins/publish/extract_xgen.py index 5b4c6b29f2..99efcb34ce 100644 --- a/openpype/hosts/maya/plugins/publish/extract_xgen.py +++ b/openpype/hosts/maya/plugins/publish/extract_xgen.py @@ -61,6 +61,12 @@ class ExtractXgenCache(publish.Extractor): fullPath=True )[0] + # Discard the children. + shapes = cmds.listRelatives(duplicate_transform, shapes=True) + children = cmds.listRelatives(duplicate_transform, children=True) + cmds.delete(set(children) - set(shapes)) + + # Connect attributes. cmds.connectAttr( "{}.matrix".format(duplicate_transform), "{}.transform".format(node), @@ -97,7 +103,7 @@ class ExtractXgenCache(publish.Extractor): force=True, type=type, exportSelected=True, - preserveReferences=True, + preserveReferences=False, constructionHistory=True, shader=True, constraints=True, From efb7d42a7fa9d267b1f7e284bc1d659f3cd41583 Mon Sep 17 00:00:00 2001 From: Toke Stuart Jepsen Date: Sat, 7 Jan 2023 09:34:34 +0000 Subject: [PATCH 27/67] Code cosmetics --- openpype/hosts/maya/plugins/publish/collect_xgen.py | 8 ++++---- openpype/hosts/maya/plugins/publish/extract_xgen.py | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/openpype/hosts/maya/plugins/publish/collect_xgen.py b/openpype/hosts/maya/plugins/publish/collect_xgen.py index fa7ec3776d..5a48b1d221 100644 --- a/openpype/hosts/maya/plugins/publish/collect_xgen.py +++ b/openpype/hosts/maya/plugins/publish/collect_xgen.py @@ -13,20 +13,20 @@ class CollectXgen(pyblish.api.InstancePlugin): def process(self, instance): data = { - "xgenPalettes": cmds.ls(instance, type="xgmPalette", long=True), + "xgmPalettes": cmds.ls(instance, type="xgmPalette", long=True), "xgmDescriptions": cmds.ls( instance, type="xgmDescription", long=True ), "xgmSubdPatches": cmds.ls(instance, type="xgmSubdPatch", long=True) } data["xgenNodes"] = ( - data["xgenPalettes"] + + data["xgmPalettes"] + data["xgmDescriptions"] + data["xgmSubdPatches"] ) - if data["xgenPalettes"]: - data["xgenPalette"] = data["xgenPalettes"][0] + if data["xgmPalettes"]: + data["xgmPalette"] = data["xgmPalettes"][0] data["xgenConnections"] = {} for node in data["xgmSubdPatches"]: diff --git a/openpype/hosts/maya/plugins/publish/extract_xgen.py b/openpype/hosts/maya/plugins/publish/extract_xgen.py index 99efcb34ce..22260df2c7 100644 --- a/openpype/hosts/maya/plugins/publish/extract_xgen.py +++ b/openpype/hosts/maya/plugins/publish/extract_xgen.py @@ -30,13 +30,13 @@ class ExtractXgenCache(publish.Extractor): template_data.update({"ext": "xgen"}) templates = instance.context.data["anatomy"].templates["publish"] xgen_filename = StringTemplate(templates["file"]).format(template_data) - name = instance.data["xgenPalette"].replace(":", "__").replace("|", "") + name = instance.data["xgmPalette"].replace(":", "__").replace("|", "") xgen_filename = xgen_filename.replace(".xgen", "__" + name + ".xgen") # Export xgen palette files. xgen_path = os.path.join(staging_dir, xgen_filename).replace("\\", "/") xgenm.exportPalette( - instance.data["xgenPalette"].replace("|", ""), xgen_path + instance.data["xgmPalette"].replace("|", ""), xgen_path ) self.log.info("Extracted to {}".format(xgen_path)) @@ -132,12 +132,12 @@ class ExtractXgenCache(publish.Extractor): # Collect all files under palette root as resources. data_path = xgenm.getAttr( - "xgDataPath", instance.data["xgenPalette"].replace("|", "") + "xgDataPath", instance.data["xgmPalette"].replace("|", "") ).split(os.pathsep)[0] data_path = data_path.replace( "${PROJECT}", xgenm.getAttr( - "xgProjectPath", instance.data["xgenPalette"].replace("|", "") + "xgProjectPath", instance.data["xgmPalette"].replace("|", "") ) ) transfers = [] From 9ed90e67f8a2fbdc62d3c2a15cc89ed79111301d Mon Sep 17 00:00:00 2001 From: Toke Stuart Jepsen Date: Sun, 8 Jan 2023 18:03:34 +0000 Subject: [PATCH 28/67] Code cosmetics --- openpype/hosts/maya/plugins/inventory/connect_geometry.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/openpype/hosts/maya/plugins/inventory/connect_geometry.py b/openpype/hosts/maya/plugins/inventory/connect_geometry.py index 375ad29ef0..9fe4f9e195 100644 --- a/openpype/hosts/maya/plugins/inventory/connect_geometry.py +++ b/openpype/hosts/maya/plugins/inventory/connect_geometry.py @@ -52,7 +52,7 @@ class ConnectGeometry(InventoryAction): self.display_warning(message) return - source_container = source_containers[0] + source_object = source_containers[0]["objectName"] # Collect matching geometry transforms based cbId attribute. target_containers = [] @@ -62,7 +62,7 @@ class ConnectGeometry(InventoryAction): target_containers.extend(containers) - source_data = self.get_container_data(source_container["objectName"]) + source_data = self.get_container_data(source_object) matches = [] node_types = [] for target_container in target_containers: @@ -80,7 +80,7 @@ class ConnectGeometry(InventoryAction): self.display_warning("No matching geometries found.") return - message = "Linking geometries:\n\n" + message = "Connecting geometries:\n\n" for match in matches: message += "{} > {}\n".format(match[0], match[1]) From 0382ef29f5e9b02186d2c207063a3a15b66b3397 Mon Sep 17 00:00:00 2001 From: Toke Stuart Jepsen Date: Sun, 8 Jan 2023 18:04:01 +0000 Subject: [PATCH 29/67] Connect and update xgen animation. --- openpype/hosts/maya/api/plugin.py | 31 ++++ .../maya/plugins/inventory/connect_xgen.py | 168 ++++++++++++++++++ 2 files changed, 199 insertions(+) create mode 100644 openpype/hosts/maya/plugins/inventory/connect_xgen.py diff --git a/openpype/hosts/maya/api/plugin.py b/openpype/hosts/maya/api/plugin.py index 82df85a8be..f11757b2ab 100644 --- a/openpype/hosts/maya/api/plugin.py +++ b/openpype/hosts/maya/api/plugin.py @@ -300,6 +300,37 @@ class ReferenceLoader(Loader): str(representation["_id"]), type="string") + # Update any xgen containers. + compound_name = "xgenContainers" + object = "SplinePrimitive" + if cmds.objExists("{}.{}".format(node, compound_name)): + import xgenm + container_amount = cmds.getAttr( + "{}.{}".format(node, compound_name), size=True + ) + # loop through all compound children + for i in range(container_amount): + attr = "{}.{}[{}].container".format(node, compound_name, i) + objectset = cmds.listConnections(attr)[0] + reference_node = cmds.sets(objectset, query=True)[0] + palettes = cmds.ls( + cmds.referenceQuery(reference_node, nodes=True), + type="xgmPalette" + ) + for palette in palettes: + for description in xgenm.descriptions(palette): + xgenm.setAttr( + "cacheFileName", + path.replace("\\", "/"), + palette, + description, + object + ) + + # Refresh UI and viewport. + de = xgenm.xgGlobal.DescriptionEditor + de.refresh("Full") + def remove(self, container): """Remove an existing `container` from Maya scene diff --git a/openpype/hosts/maya/plugins/inventory/connect_xgen.py b/openpype/hosts/maya/plugins/inventory/connect_xgen.py new file mode 100644 index 0000000000..933a1b4025 --- /dev/null +++ b/openpype/hosts/maya/plugins/inventory/connect_xgen.py @@ -0,0 +1,168 @@ +from maya import cmds +import xgenm + +from openpype.pipeline import ( + InventoryAction, get_representation_context, get_representation_path +) + + +class ConnectXgen(InventoryAction): + """Connect Xgen with an animation or pointcache. + """ + + label = "Connect Xgen" + icon = "link" + color = "white" + + def process(self, containers): + # Validate selection is more than 1. + message = ( + "Only 1 container selected. 2+ containers needed for this action." + ) + if len(containers) == 1: + self.display_warning(message) + return + + # Categorize containers by family. + containers_by_family = {} + for container in containers: + family = get_representation_context( + container["representation"] + )["subset"]["data"]["family"] + try: + containers_by_family[family].append(container) + except KeyError: + containers_by_family[family] = [container] + + # Validate to only 1 source container. + source_containers = containers_by_family.get("animation", []) + source_containers += containers_by_family.get("pointcache", []) + source_container_namespaces = [ + x["namespace"] for x in source_containers + ] + message = ( + "{} animation containers selected:\n\n{}\n\nOnly select 1 of type " + "\"animation\" or \"pointcache\".".format( + len(source_containers), source_container_namespaces + ) + ) + if len(source_containers) != 1: + self.display_warning(message) + return + + source_container = source_containers[0] + source_object = source_container["objectName"] + + # Validate source representation is an alembic. + source_path = get_representation_path( + get_representation_context( + source_container["representation"] + )["representation"] + ).replace("\\", "/") + message = "Animation container \"{}\" is not an alembic:\n{}".format( + source_container["namespace"], source_path + ) + if not source_path.endswith(".abc"): + self.display_warning(message) + return + + # Target containers. + target_containers = [] + for family, containers in containers_by_family.items(): + if family in ["animation", "pointcache"]: + continue + + target_containers.extend(containers) + + # Inform user of connections from source representation to target + # descriptions. + descriptions_data = [] + connections_msg = "" + for target_container in target_containers: + reference_node = cmds.sets( + target_container["objectName"], query=True + )[0] + palettes = cmds.ls( + cmds.referenceQuery(reference_node, nodes=True), + type="xgmPalette" + ) + for palette in palettes: + for description in xgenm.descriptions(palette): + descriptions_data.append([palette, description]) + connections_msg += "\n{}/{}".format(palette, description) + + message = "Connecting \"{}\" to:\n".format( + source_container["namespace"] + ) + message += connections_msg + choice = self.display_warning(message, show_cancel=True) + if choice is False: + return + + # Recreate "xgenContainers" attribute to reset. + compound_name = "xgenContainers" + attr = "{}.{}".format(source_object, compound_name) + if cmds.objExists(attr): + cmds.deleteAttr(attr) + + cmds.addAttr( + source_object, + longName=compound_name, + attributeType="compound", + numberOfChildren=1, + multi=True + ) + + # Connect target containers. + for target_container in target_containers: + cmds.addAttr( + source_object, + longName="container", + attributeType="message", + parent=compound_name + ) + index = target_containers.index(target_container) + cmds.connectAttr( + target_container["objectName"] + ".message", + source_object + ".{}[{}].container".format( + compound_name, index + ) + ) + + # Setup cache on Xgen + object = "SplinePrimitive" + for palette, description in descriptions_data: + xgenm.setAttr("useCache", "true", palette, description, object) + xgenm.setAttr("liveMode", "false", palette, description, object) + xgenm.setAttr( + "cacheFileName", source_path, palette, description, object + ) + + # Refresh UI and viewport. + de = xgenm.xgGlobal.DescriptionEditor + de.refresh("Full") + + def display_warning(self, message, show_cancel=False): + """Show feedback to user. + + Returns: + bool + """ + + from Qt import QtWidgets + + accept = QtWidgets.QMessageBox.Ok + if show_cancel: + buttons = accept | QtWidgets.QMessageBox.Cancel + else: + buttons = accept + + state = QtWidgets.QMessageBox.warning( + None, + "", + message, + buttons=buttons, + defaultButton=accept + ) + + return state == accept From 0dd54b0a5c767a02bcb998e6740b235695f996c6 Mon Sep 17 00:00:00 2001 From: Toke Stuart Jepsen Date: Mon, 9 Jan 2023 15:40:50 +0000 Subject: [PATCH 30/67] Validate workfile has been saved. --- openpype/hosts/maya/plugins/load/load_xgen.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/openpype/hosts/maya/plugins/load/load_xgen.py b/openpype/hosts/maya/plugins/load/load_xgen.py index 23a22127e9..565d1306b4 100644 --- a/openpype/hosts/maya/plugins/load/load_xgen.py +++ b/openpype/hosts/maya/plugins/load/load_xgen.py @@ -4,6 +4,8 @@ import shutil import maya.cmds as cmds import xgenm +from Qt import QtWidgets + import openpype.hosts.maya.api.plugin from openpype.hosts.maya.api.lib import ( maintained_selection, get_container_members, attribute_values @@ -71,6 +73,15 @@ class XgenLoader(openpype.hosts.maya.api.plugin.ReferenceLoader): return destination def process_reference(self, context, name, namespace, options): + # Validate workfile has a path. + if current_file() is None: + QtWidgets.QMessageBox.warning( + None, + "", + "Current workfile has not been saved." + ) + return + maya_filepath = self.prepare_root_value( self.fname, context["project"]["name"] ) From 3021fea4762ec84e53091f1cb954883be6a53634 Mon Sep 17 00:00:00 2001 From: Toke Stuart Jepsen Date: Wed, 11 Jan 2023 10:04:17 +0000 Subject: [PATCH 31/67] Increment xgen files with workfile. --- openpype/hosts/maya/startup/userSetup.py | 42 +++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/openpype/hosts/maya/startup/userSetup.py b/openpype/hosts/maya/startup/userSetup.py index b64ed93000..5cd27500dc 100644 --- a/openpype/hosts/maya/startup/userSetup.py +++ b/openpype/hosts/maya/startup/userSetup.py @@ -1,8 +1,10 @@ import os +import shutil from openpype.settings import get_project_settings from openpype.pipeline import install_host -from openpype.hosts.maya.api import MayaHost +from openpype.hosts.maya.api import MayaHost, current_file +from openpype.lib import register_event_callback from maya import cmds @@ -21,6 +23,44 @@ if bool(int(os.environ.get(key, "0"))): lowestPriority=True ) + +# Setup Xgen save callback. +def xgen_on_save(): + """Increments the xgen side car files .xgen and .xgd + + Only works when incrementing to the same directory. + """ + + file_path = current_file() + current_dir = os.path.dirname(file_path) + basename = os.path.basename(file_path).split(".")[0] + attrs = ["xgFileName", "xgBaseFile"] + for palette in cmds.ls(type="xgmPalette"): + for attr in attrs: + source = os.path.join( + current_dir, cmds.getAttr(palette + "." + attr) + ) + if not os.path.exists(source): + continue + + destination_basename = "{}__{}{}".format( + basename, + palette.replace(":", "_"), + os.path.splitext(source)[1] + ) + destination = os.path.join(current_dir, destination_basename) + + if source == destination: + continue + + shutil.copy(source, destination) + cmds.setAttr( + palette + "." + attr, destination_basename, type="string" + ) + + +register_event_callback("save", xgen_on_save) + # Build a shelf. settings = get_project_settings(os.environ['AVALON_PROJECT']) shelf_preset = settings['maya'].get('project_shelf') From 4dc155d95fb89ccea3310721afcd534f91e07a10 Mon Sep 17 00:00:00 2001 From: Toke Stuart Jepsen Date: Thu, 12 Jan 2023 15:51:51 +0000 Subject: [PATCH 32/67] Support for rendering Opening the workfile in its published location will load the xgen correctly. --- .../plugins/publish/extract_workfile_xgen.py | 131 ++++++++++++++++++ .../plugins/publish/reset_xgen_attributes.py | 21 +++ 2 files changed, 152 insertions(+) create mode 100644 openpype/hosts/maya/plugins/publish/extract_workfile_xgen.py create mode 100644 openpype/hosts/maya/plugins/publish/reset_xgen_attributes.py diff --git a/openpype/hosts/maya/plugins/publish/extract_workfile_xgen.py b/openpype/hosts/maya/plugins/publish/extract_workfile_xgen.py new file mode 100644 index 0000000000..e12a870eaf --- /dev/null +++ b/openpype/hosts/maya/plugins/publish/extract_workfile_xgen.py @@ -0,0 +1,131 @@ +import os +import shutil + +from maya import cmds + +import pyblish.api +from openpype.hosts.maya.api import current_file +from openpype.pipeline import publish + + +class ExtractWorkfileXgen(publish.Extractor): + """Extract Workfile Xgen.""" + + # Offset to run before workfile scene save. + order = pyblish.api.ExtractorOrder - 0.499 + label = "Extract Workfile Xgen" + families = ["workfile"] + hosts = ["maya"] + + def process(self, instance): + # Validate to extract only when we are publishing a renderlayer as + # well. + renderlayer = False + for i in instance.context: + is_renderlayer = ( + "renderlayer" in i.data.get("families", []) or + i.data["family"] == "renderlayer" + ) + if is_renderlayer and i.data["publish"]: + renderlayer = True + break + + if not renderlayer: + self.log.debug( + "No publishable renderlayers found in context. Abort Xgen" + " extraction." + ) + return + + # Collect Xgen and Delta files. + xgen_files = [] + sources = [] + file_path = current_file() + current_dir = os.path.dirname(file_path) + attrs = ["xgFileName", "xgBaseFile"] + for palette in cmds.ls(type="xgmPalette"): + for attr in attrs: + source = os.path.join( + current_dir, cmds.getAttr(palette + "." + attr) + ) + if not os.path.exists(source): + continue + + ext = os.path.splitext(source)[1] + if ext == ".xgen": + xgen_files.append(source) + if ext == ".xgd": + sources.append(source) + + # Copy .xgen file to temporary location and modify. + staging_dir = self.staging_dir(instance) + for source in xgen_files: + destination = os.path.join(staging_dir, os.path.basename(source)) + shutil.copy(source, destination) + + lines = [] + with open(destination, "r") as f: + for line in [line.rstrip() for line in f]: + if line.startswith("\txgProjectPath"): + line = "\txgProjectPath\t\t{}/".format( + instance.data["resourcesDir"].replace("\\", "/") + ) + + lines.append(line) + + with open(destination, "w") as f: + f.write("\n".join(lines)) + + sources.append(destination) + + # Add resource files to workfile instance. + transfers = [] + for source in sources: + basename = os.path.basename(source) + destination = os.path.join(instance.data["resourcesDir"], basename) + transfers.append((source, destination)) + + import xgenm + for palette in cmds.ls(type="xgmPalette"): + relative_data_path = xgenm.getAttr( + "xgDataPath", palette.replace("|", "") + ).split(os.pathsep)[0] + absolute_data_path = relative_data_path.replace( + "${PROJECT}", + xgenm.getAttr("xgProjectPath", palette.replace("|", "")) + ) + + for root, _, files in os.walk(absolute_data_path): + for file in files: + source = os.path.join(root, file).replace("\\", "/") + destination = os.path.join( + instance.data["resourcesDir"], + relative_data_path.replace("${PROJECT}", ""), + source.replace(absolute_data_path, "")[1:] + ) + transfers.append((source, destination.replace("\\", "/"))) + + for source, destination in transfers: + self.log.debug("Transfer: {} > {}".format(source, destination)) + + instance.data["transfers"] = transfers + + # Set palette attributes in preparation for workfile publish. + attrs = ["xgFileName", "xgBaseFile"] + data = {} + for palette in cmds.ls(type="xgmPalette"): + for attr in attrs: + value = cmds.getAttr(palette + "." + attr) + if value: + new_value = "resources/{}".format(value) + node_attr = "{}.{}".format(palette, attr) + self.log.info( + "Setting \"{}\" on \"{}\"".format(new_value, node_attr) + ) + cmds.setAttr(node_attr, new_value, type="string") + try: + data[palette][attr] = value + except KeyError: + data[palette] = {attr: value} + + instance.data["xgenAttributes"] = data diff --git a/openpype/hosts/maya/plugins/publish/reset_xgen_attributes.py b/openpype/hosts/maya/plugins/publish/reset_xgen_attributes.py new file mode 100644 index 0000000000..9397d479d2 --- /dev/null +++ b/openpype/hosts/maya/plugins/publish/reset_xgen_attributes.py @@ -0,0 +1,21 @@ +from maya import cmds + +import pyblish.api + + +class ResetXgenAttributes(pyblish.api.InstancePlugin): + """Reset Xgen attributes.""" + + label = "Reset Xgen Attributes." + # Offset to run after global integrator. + order = pyblish.api.IntegratorOrder + 1.0 + families = ["workfile"] + + def process(self, instance): + for palette, data in instance.data.get("xgenAttributes", []).items(): + for attr, value in data.items(): + node_attr = "{}.{}".format(palette, attr) + self.log.info( + "Setting \"{}\" on \"{}\"".format(value, node_attr) + ) + cmds.setAttr(node_attr, value, type="string") From 05a8ef36f7472143bffa36837e3cf56c9593733e Mon Sep 17 00:00:00 2001 From: Toke Stuart Jepsen Date: Fri, 13 Jan 2023 12:59:25 +0000 Subject: [PATCH 33/67] Xgen documentation --- website/docs/artist_hosts_maya.md | 17 +++++ website/docs/artist_hosts_maya_xgen.md | 94 ++++++++++++++++++++++---- 2 files changed, 97 insertions(+), 14 deletions(-) diff --git a/website/docs/artist_hosts_maya.md b/website/docs/artist_hosts_maya.md index fb1af765d2..ced6887d08 100644 --- a/website/docs/artist_hosts_maya.md +++ b/website/docs/artist_hosts_maya.md @@ -601,3 +601,20 @@ about customizing review process refer to [admin section](project_settings/setti If you don't move `modelMain` into `reviewMain`, review will be generated but it will be published as separate entity. + + +## Inventory Actions + +### Connect Geometry + +This action will connect geometries between containers. + +#### Usage + +Select 1 container of type `animation` or `pointcache`, then 1+ container of any type. + +#### Details + +The action searches the selected containers for 1 animation container of type `animation` or `pointcache`. This animation container will be connected to the rest of the selected containers. Matching geometries between containers is done by comparing the attribute `cbId`. + +The connection between geometries is done with a live blendshape. diff --git a/website/docs/artist_hosts_maya_xgen.md b/website/docs/artist_hosts_maya_xgen.md index 8b0174a29f..629b600458 100644 --- a/website/docs/artist_hosts_maya_xgen.md +++ b/website/docs/artist_hosts_maya_xgen.md @@ -4,26 +4,92 @@ title: Xgen for Maya sidebar_label: Xgen --- -## Working with Xgen in OpenPype +## Setup -OpenPype support publishing and loading of Xgen interactive grooms. You can publish -them as mayaAscii files with scalps that can be loaded into another maya scene, or as -alembic caches. +### Settings -### Publishing Xgen Grooms +Go to project settings > Maya > enable "Open Workfile Post Initialization"; -To prepare xgen for publishing just select all the descriptions that should be published together and the create Xgen Subset in the scene using - **OpenPype menu** → **Create**... and select **Xgen Interactive**. Leave Use selection checked. +`project_settings/maya/open_workfile_post_initialization` -For actual publishing of your groom to go **OpenPype → Publish** and then press ▶ to publish. This will export `.ma` file containing your grooms with any geometries they are attached to and also a baked cache in `.abc` format +This is due to two errors occurring when opening workfile contaiing referenced xgen nodes on launch of Maya; +- ``Critical``: Duplicate collection errors on launching workfile. This is because Maya first imports Xgen when referencing in external Maya files, then imports Xgen again when the reference edits are applied. +``` +Importing XGen Collections... +# Error: XGen: Failed to find description ball_xgenMain_01_:parent in collection ball_xgenMain_01_:collection. Abort applying delta: P:/PROJECTS/OP01_CG_demo/shots/sh040/work/Lighting/cg_sh040_Lighting_v001__ball_xgenMain_01___collection.xgen # +# Error: XGen: Tried to import a duplicate collection, ball_xgenMain_02_:collection, from file P:/PROJECTS/OP01_CG_demo/shots/sh040/work/Lighting/cg_sh040_Lighting_v001__ball_xgenMain_02___collection.xgen. Aborting import. # +``` +- ``Non-critical``: Errors on opening workfile and failed opening of published xgen. This is because Maya imports Xgen when referencing in external Maya files but the reference edits that ensure the location of the Xgen files are correct, has not been applied yet. +``` +Importing XGen Collections... +# Error: XGen: Failed to open file: P:/PROJECTS/OP01_CG_demo/shots/sh040/work/Lighting/cg_ball_xgenMain_v035__ball_rigMain_01___collection.xgen # +# Error: XGen: Failed to import collection from file P:/PROJECTS/OP01_CG_demo/shots/sh040/work/Lighting/cg_ball_xgenMain_v035__ball_rigMain_01___collection.xgen # +``` -:::tip adding more descriptions -You can add multiple xgen description into the subset you are about to publish, simply by -adding them to the maya set that was created for you. Please make sure that only xgen description nodes are present inside of the set and not the scalp geometry. -::: +### Workfile Incremental Save -### Loading Xgen +When you increment the Maya workfile to a new version, all `.xgen` and `.xgd` files referenced by the Xgen collection in the workspace is incremented as well. -You can use published xgens by loading them using OpenPype Publisher. You can choose to reference or import xgen. We don't have any automatic mesh linking at the moment and it is expected, that groom is published with a scalp, that can then be manually attached to your animated mesh for example. +## Create -The alembic representation can be loaded too and it contains the groom converted to curves. Keep in mind that the density of the alembic directly depends on your viewport xgen density at the point of export. +Create an Xgen instance to publish. This needs to contain 1 Xgen collection only, but validation will check for this: + +`OpenPype > Create... > Xgen` + +You can create multiple Xgen instances if you have multiple collections to publish. + +### Publish + +The publishing process will grab geometry used for Xgen along with any external files used in the collection's descriptions. This creates an isolated Maya file with just the Xgen collection's dependencies, so you can use any nested geometry when creating the Xgen description. An Xgen version will consist of: + +- Maya file (`.ma`) - this contains the geometry and the connections to the Xgen collection and descriptions. +- Xgen file (`.xgen`) - this contains the Xgen collection and description. +- Resource files (`.ptx`, `.xuv`) - this contains Xgen side car files used in the collection and descriptions. + +## Load + +Open the Loader tool, `OpenPype > Loader...`, and navigate to the published Xgen version. On right-click you'll get the option `Reference Xgen (ma)` +When loading an Xgen version the following happens: + +- References in the Maya file. +- Copies the Xgen file (`.xgen`) to the current workspace. +- Modifies the Xgen file copy to load the current workspace first then the published Xgen collection. +- Makes a custom attribute on the Xgen collection, `float_ignore`, which can be seen under the `Expressions` tab of the `Xgen` UI. This is done to initialize the Xgen delta file workflow. +- Setup an Xgen delta file (`.xgd`) to store any workspace changes of the published Xgen version. + +When the loading is done, Xgen collection will be in the Xgen delta file workflow which means any changes done in the Maya workfile will be stored in the current workspace. The published Xgen collection will remain intact, even if the user assigns maps to any attributes or otherwise modifies any attribute. + +### Updating + +When there are changes to the Xgen version, the user will be notified when opening the workfile or publishing. Since the Xgen is referenced, it follows the standard Maya referencing system and overrides. + +For example publishing `xgenMain` version 1 with the attribute `renderer` set to `None`, then version 2 has `renderer` set to `Arnold Renderer`. When updating from version 1 to 2, the `renderer` attribute will be updated to `Arnold Renderer` unless there is an override. + +### Connect Patches + +When loading in an Xgen version, it does not have any connections to anything in the workfile, so its static in the position it was published in. Use the [Connect Geometry](artist_hosts_maya#connect-geometry) action to connect Xgen to any matching loaded animated geometry. + +### Connect Guides + +Along with patches you can also connect the Xgen guides to an Alembic cache. + +#### Usage + +Select 1 animation container, of family `animation` or `pointcache`, then the Xgen containers to connect to. Right-click > `Actions` > `Connect Xgen`. + +***Note: Only alembic (`.abc`) representations are allowed.*** + +#### Details + +Connecting the guide will make Xgen use the Alembic directly, setting the attributes under `Guide Animation`, so the Alembic needs to contain the same amount of curves as guides in the Xgen. + +The animation container gets connected with the Xgen container, so if the animation container is updated so will the Xgen container's attribute. + +## Rendering + +To render with Xgen, follow the [Rendering With OpenPype](artist_hosts_maya#rendering-with-openpype) guide. + +### Details + +When submitting a workfile with Xgen, all Xgen related files will be collected and published as the workfiles resources. This means the published workfile is no longer referencing the workspace Xgen files. From 27fc690209493147ef965ce0c364465fe5acf223 Mon Sep 17 00:00:00 2001 From: Toke Jepsen Date: Mon, 16 Jan 2023 10:04:55 +0000 Subject: [PATCH 34/67] Update website/docs/artist_hosts_maya_xgen.md Co-authored-by: Roy Nieterau --- website/docs/artist_hosts_maya_xgen.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/artist_hosts_maya_xgen.md b/website/docs/artist_hosts_maya_xgen.md index 629b600458..cde56888d4 100644 --- a/website/docs/artist_hosts_maya_xgen.md +++ b/website/docs/artist_hosts_maya_xgen.md @@ -12,7 +12,7 @@ Go to project settings > Maya > enable "Open Workfile Post Initialization"; `project_settings/maya/open_workfile_post_initialization` -This is due to two errors occurring when opening workfile contaiing referenced xgen nodes on launch of Maya; +This is due to two errors occurring when opening workfile containing referenced xgen nodes on launch of Maya, specifically: - ``Critical``: Duplicate collection errors on launching workfile. This is because Maya first imports Xgen when referencing in external Maya files, then imports Xgen again when the reference edits are applied. ``` From 1b1cd149065a561741b5783823aceb88a82e5f62 Mon Sep 17 00:00:00 2001 From: Toke Jepsen Date: Mon, 16 Jan 2023 10:12:32 +0000 Subject: [PATCH 35/67] Update website/docs/artist_hosts_maya_xgen.md Co-authored-by: Roy Nieterau --- website/docs/artist_hosts_maya_xgen.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/artist_hosts_maya_xgen.md b/website/docs/artist_hosts_maya_xgen.md index cde56888d4..e0c008929c 100644 --- a/website/docs/artist_hosts_maya_xgen.md +++ b/website/docs/artist_hosts_maya_xgen.md @@ -33,7 +33,7 @@ When you increment the Maya workfile to a new version, all `.xgen` and `.xgd` fi ## Create -Create an Xgen instance to publish. This needs to contain 1 Xgen collection only, but validation will check for this: +Create an Xgen instance to publish. This needs to contain only **one Xgen collection**. `OpenPype > Create... > Xgen` From 86012906f753e48dd1015516c0d795ca3ccd556f Mon Sep 17 00:00:00 2001 From: Toke Jepsen Date: Mon, 16 Jan 2023 10:13:30 +0000 Subject: [PATCH 36/67] Update website/docs/artist_hosts_maya_xgen.md Co-authored-by: Roy Nieterau --- website/docs/artist_hosts_maya_xgen.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/artist_hosts_maya_xgen.md b/website/docs/artist_hosts_maya_xgen.md index e0c008929c..3f47428abe 100644 --- a/website/docs/artist_hosts_maya_xgen.md +++ b/website/docs/artist_hosts_maya_xgen.md @@ -64,7 +64,7 @@ When the loading is done, Xgen collection will be in the Xgen delta file workflo When there are changes to the Xgen version, the user will be notified when opening the workfile or publishing. Since the Xgen is referenced, it follows the standard Maya referencing system and overrides. -For example publishing `xgenMain` version 1 with the attribute `renderer` set to `None`, then version 2 has `renderer` set to `Arnold Renderer`. When updating from version 1 to 2, the `renderer` attribute will be updated to `Arnold Renderer` unless there is an override. +For example publishing `xgenMain` version 1 with the attribute `renderer` set to `None`, then version 2 has `renderer` set to `Arnold Renderer`. When updating from version 1 to 2, the `renderer` attribute will be updated to `Arnold Renderer` unless there is a local override. ### Connect Patches From 9f749edd8a13be373d8d76e15ac8c09729adaa60 Mon Sep 17 00:00:00 2001 From: Toke Stuart Jepsen Date: Tue, 17 Jan 2023 09:58:23 +0000 Subject: [PATCH 37/67] Fix When there is no xgenAttributes --- openpype/hosts/maya/plugins/publish/reset_xgen_attributes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openpype/hosts/maya/plugins/publish/reset_xgen_attributes.py b/openpype/hosts/maya/plugins/publish/reset_xgen_attributes.py index 9397d479d2..0f763613c9 100644 --- a/openpype/hosts/maya/plugins/publish/reset_xgen_attributes.py +++ b/openpype/hosts/maya/plugins/publish/reset_xgen_attributes.py @@ -12,7 +12,7 @@ class ResetXgenAttributes(pyblish.api.InstancePlugin): families = ["workfile"] def process(self, instance): - for palette, data in instance.data.get("xgenAttributes", []).items(): + for palette, data in instance.data.get("xgenAttributes", {}).items(): for attr, value in data.items(): node_attr = "{}.{}".format(palette, attr) self.log.info( From ff6fe13e2a6049e61b829905f2e3bb9f1b0b4cc5 Mon Sep 17 00:00:00 2001 From: Toke Stuart Jepsen Date: Tue, 17 Jan 2023 16:05:58 +0000 Subject: [PATCH 38/67] Code cosmetics --- openpype/hooks/pre_add_last_workfile_arg.py | 6 ++---- openpype/hosts/maya/startup/userSetup.py | 9 +++++++-- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/openpype/hooks/pre_add_last_workfile_arg.py b/openpype/hooks/pre_add_last_workfile_arg.py index ffb116d2e4..ce59c0b706 100644 --- a/openpype/hooks/pre_add_last_workfile_arg.py +++ b/openpype/hooks/pre_add_last_workfile_arg.py @@ -43,11 +43,9 @@ class AddLastWorkfileToLaunchArgs(PreLaunchHook): return # Determine whether to open workfile post initialization. - if self.data["app"].host_name == "maya": - project_name = self.data["project_name"] - settings = get_project_settings(project_name) + if self.host_name == "maya": key = "open_workfile_post_initialization" - if settings["maya"][key]: + if self.data["project_settings"]["maya"][key]: self.log.debug("Opening workfile post initialization.") self.data["env"]["OPENPYPE_" + key.upper()] = "1" return diff --git a/openpype/hosts/maya/startup/userSetup.py b/openpype/hosts/maya/startup/userSetup.py index 5cd27500dc..87025637b6 100644 --- a/openpype/hosts/maya/startup/userSetup.py +++ b/openpype/hosts/maya/startup/userSetup.py @@ -1,5 +1,6 @@ import os import shutil +from functools import partial from openpype.settings import get_project_settings from openpype.pipeline import install_host @@ -18,8 +19,12 @@ print("Starting OpenPype usersetup...") key = "OPENPYPE_OPEN_WORKFILE_POST_INITIALIZATION" if bool(int(os.environ.get(key, "0"))): cmds.evalDeferred( - "cmds.file(os.environ[\"AVALON_LAST_WORKFILE\"], open=True," - " force=True)", + partial( + cmds.file, + os.environ["AVALON_LAST_WORKFILE"], + open=True, + force=True + ), lowestPriority=True ) From 40f8bc25f5adcfb2b70ce219f555b2f09a74e079 Mon Sep 17 00:00:00 2001 From: Toke Stuart Jepsen Date: Tue, 17 Jan 2023 16:06:16 +0000 Subject: [PATCH 39/67] Remove Xgen incremental save feature --- openpype/hosts/maya/startup/userSetup.py | 37 ------------------------ website/docs/artist_hosts_maya_xgen.md | 4 --- 2 files changed, 41 deletions(-) diff --git a/openpype/hosts/maya/startup/userSetup.py b/openpype/hosts/maya/startup/userSetup.py index 87025637b6..a2f6405980 100644 --- a/openpype/hosts/maya/startup/userSetup.py +++ b/openpype/hosts/maya/startup/userSetup.py @@ -29,43 +29,6 @@ if bool(int(os.environ.get(key, "0"))): ) -# Setup Xgen save callback. -def xgen_on_save(): - """Increments the xgen side car files .xgen and .xgd - - Only works when incrementing to the same directory. - """ - - file_path = current_file() - current_dir = os.path.dirname(file_path) - basename = os.path.basename(file_path).split(".")[0] - attrs = ["xgFileName", "xgBaseFile"] - for palette in cmds.ls(type="xgmPalette"): - for attr in attrs: - source = os.path.join( - current_dir, cmds.getAttr(palette + "." + attr) - ) - if not os.path.exists(source): - continue - - destination_basename = "{}__{}{}".format( - basename, - palette.replace(":", "_"), - os.path.splitext(source)[1] - ) - destination = os.path.join(current_dir, destination_basename) - - if source == destination: - continue - - shutil.copy(source, destination) - cmds.setAttr( - palette + "." + attr, destination_basename, type="string" - ) - - -register_event_callback("save", xgen_on_save) - # Build a shelf. settings = get_project_settings(os.environ['AVALON_PROJECT']) shelf_preset = settings['maya'].get('project_shelf') diff --git a/website/docs/artist_hosts_maya_xgen.md b/website/docs/artist_hosts_maya_xgen.md index 3f47428abe..fc75959f2b 100644 --- a/website/docs/artist_hosts_maya_xgen.md +++ b/website/docs/artist_hosts_maya_xgen.md @@ -27,10 +27,6 @@ Importing XGen Collections... # Error: XGen: Failed to import collection from file P:/PROJECTS/OP01_CG_demo/shots/sh040/work/Lighting/cg_ball_xgenMain_v035__ball_rigMain_01___collection.xgen # ``` -### Workfile Incremental Save - -When you increment the Maya workfile to a new version, all `.xgen` and `.xgd` files referenced by the Xgen collection in the workspace is incremented as well. - ## Create Create an Xgen instance to publish. This needs to contain only **one Xgen collection**. From 59b1caacd2f4e662c44d57c23a8deca2d3fe07fb Mon Sep 17 00:00:00 2001 From: Toke Stuart Jepsen Date: Tue, 17 Jan 2023 16:06:39 +0000 Subject: [PATCH 40/67] Account for not using the published workfile. --- .../hosts/maya/plugins/publish/extract_workfile_xgen.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/openpype/hosts/maya/plugins/publish/extract_workfile_xgen.py b/openpype/hosts/maya/plugins/publish/extract_workfile_xgen.py index e12a870eaf..13a04615eb 100644 --- a/openpype/hosts/maya/plugins/publish/extract_workfile_xgen.py +++ b/openpype/hosts/maya/plugins/publish/extract_workfile_xgen.py @@ -37,6 +37,13 @@ class ExtractWorkfileXgen(publish.Extractor): ) return + publish_settings = instance.context["deadline"]["publish"] + if not publish_settings["MayaSubmitDeadline"]["use_published"]: + self.log.debug( + "Not using the published workfile. Abort Xgen extraction." + ) + return + # Collect Xgen and Delta files. xgen_files = [] sources = [] From 1c1a41ba5efd0e883d8985832dd7371554597384 Mon Sep 17 00:00:00 2001 From: Toke Stuart Jepsen Date: Tue, 17 Jan 2023 16:10:39 +0000 Subject: [PATCH 41/67] Hound --- openpype/hooks/pre_add_last_workfile_arg.py | 1 - openpype/hosts/maya/startup/userSetup.py | 4 +--- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/openpype/hooks/pre_add_last_workfile_arg.py b/openpype/hooks/pre_add_last_workfile_arg.py index ce59c0b706..1c8746c559 100644 --- a/openpype/hooks/pre_add_last_workfile_arg.py +++ b/openpype/hooks/pre_add_last_workfile_arg.py @@ -1,7 +1,6 @@ import os from openpype.lib import PreLaunchHook -from openpype.settings import get_project_settings class AddLastWorkfileToLaunchArgs(PreLaunchHook): diff --git a/openpype/hosts/maya/startup/userSetup.py b/openpype/hosts/maya/startup/userSetup.py index a2f6405980..bfa5e6e60d 100644 --- a/openpype/hosts/maya/startup/userSetup.py +++ b/openpype/hosts/maya/startup/userSetup.py @@ -1,11 +1,9 @@ import os -import shutil from functools import partial from openpype.settings import get_project_settings from openpype.pipeline import install_host -from openpype.hosts.maya.api import MayaHost, current_file -from openpype.lib import register_event_callback +from openpype.hosts.maya.api import MayaHost from maya import cmds From 5f709b08929e81f5e7c48a077320fdabee08f946 Mon Sep 17 00:00:00 2001 From: Toke Stuart Jepsen Date: Tue, 17 Jan 2023 16:22:12 +0000 Subject: [PATCH 42/67] Code cosmetics --- openpype/hosts/maya/api/plugin.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/openpype/hosts/maya/api/plugin.py b/openpype/hosts/maya/api/plugin.py index f11757b2ab..b7adf6edfc 100644 --- a/openpype/hosts/maya/api/plugin.py +++ b/openpype/hosts/maya/api/plugin.py @@ -302,7 +302,6 @@ class ReferenceLoader(Loader): # Update any xgen containers. compound_name = "xgenContainers" - object = "SplinePrimitive" if cmds.objExists("{}.{}".format(node, compound_name)): import xgenm container_amount = cmds.getAttr( @@ -324,7 +323,7 @@ class ReferenceLoader(Loader): path.replace("\\", "/"), palette, description, - object + "SplinePrimitive" ) # Refresh UI and viewport. From cdc0a80846f465d0f12ee6bfa83d28313676d327 Mon Sep 17 00:00:00 2001 From: Toke Stuart Jepsen Date: Wed, 18 Jan 2023 11:21:43 +0000 Subject: [PATCH 43/67] Fix When there is no Deadline setting avaliable. --- .../maya/plugins/publish/extract_workfile_xgen.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/openpype/hosts/maya/plugins/publish/extract_workfile_xgen.py b/openpype/hosts/maya/plugins/publish/extract_workfile_xgen.py index 13a04615eb..d37a03d1f6 100644 --- a/openpype/hosts/maya/plugins/publish/extract_workfile_xgen.py +++ b/openpype/hosts/maya/plugins/publish/extract_workfile_xgen.py @@ -37,12 +37,14 @@ class ExtractWorkfileXgen(publish.Extractor): ) return - publish_settings = instance.context["deadline"]["publish"] - if not publish_settings["MayaSubmitDeadline"]["use_published"]: - self.log.debug( - "Not using the published workfile. Abort Xgen extraction." - ) - return + deadline_settings = instance.context.get("deadline") + if deadline_settings: + publish_settings = deadline_settings["publish"] + if not publish_settings["MayaSubmitDeadline"]["use_published"]: + self.log.debug( + "Not using the published workfile. Abort Xgen extraction." + ) + return # Collect Xgen and Delta files. xgen_files = [] From 8c626920ccc0ff81145cab1f994478a2c3ef4a44 Mon Sep 17 00:00:00 2001 From: Toke Stuart Jepsen Date: Thu, 19 Jan 2023 11:01:47 +0000 Subject: [PATCH 44/67] Fix Updating --- openpype/hosts/maya/plugins/load/load_xgen.py | 62 +++++++++++-------- 1 file changed, 37 insertions(+), 25 deletions(-) diff --git a/openpype/hosts/maya/plugins/load/load_xgen.py b/openpype/hosts/maya/plugins/load/load_xgen.py index 565d1306b4..8249c9092e 100644 --- a/openpype/hosts/maya/plugins/load/load_xgen.py +++ b/openpype/hosts/maya/plugins/load/load_xgen.py @@ -32,7 +32,7 @@ class XgenLoader(openpype.hosts.maya.api.plugin.ReferenceLoader): # Copy the xgen palette file from published version. _, maya_extension = os.path.splitext(maya_filepath) source = maya_filepath.replace(maya_extension, ".xgen") - destination = os.path.join( + xgen_file = os.path.join( project_path, "{basename}__{namespace}__{name}.xgen".format( basename=os.path.splitext(os.path.basename(current_file()))[0], @@ -40,15 +40,15 @@ class XgenLoader(openpype.hosts.maya.api.plugin.ReferenceLoader): name=name ) ).replace("\\", "/") - self.log.info("Copying {} to {}".format(source, destination)) - shutil.copy(source, destination) + self.log.info("Copying {} to {}".format(source, xgen_file)) + shutil.copy(source, xgen_file) # Modify xgDataPath and xgProjectPath to have current workspace first # and published version directory second. This ensure that any newly # created xgen files are created in the current workspace. resources_path = os.path.join(os.path.dirname(source), "resources") lines = [] - with open(destination, "r") as f: + with open(xgen_file, "r") as f: for line in [line.rstrip() for line in f]: if line.startswith("\txgDataPath"): data_path = line.split("\t")[-1] @@ -67,10 +67,12 @@ class XgenLoader(openpype.hosts.maya.api.plugin.ReferenceLoader): lines.append(line) - with open(destination, "w") as f: + with open(xgen_file, "w") as f: f.write("\n".join(lines)) - return destination + xgd_file = xgen_file.replace(".xgen", ".xgd") + + return xgen_file, xgd_file def process_reference(self, context, name, namespace, options): # Validate workfile has a path. @@ -78,7 +80,8 @@ class XgenLoader(openpype.hosts.maya.api.plugin.ReferenceLoader): QtWidgets.QMessageBox.warning( None, "", - "Current workfile has not been saved." + "Current workfile has not been saved. Please save the workfile" + " before loading an Xgen." ) return @@ -87,10 +90,9 @@ class XgenLoader(openpype.hosts.maya.api.plugin.ReferenceLoader): ) name = context["representation"]["data"]["xgenName"] - xgen_file = self.setup_xgen_palette_file( + xgen_file, xgd_file = self.setup_xgen_palette_file( maya_filepath, namespace, name ) - xgd_file = xgen_file.replace(".xgen", ".xgd") # Reference xgen. Xgen does not like being referenced in under a group. new_nodes = [] @@ -105,17 +107,7 @@ class XgenLoader(openpype.hosts.maya.api.plugin.ReferenceLoader): ) xgen_palette = cmds.ls(nodes, type="xgmPalette", long=True)[0] - cmds.setAttr( - "{}.xgBaseFile".format(xgen_palette), - os.path.basename(xgen_file), - type="string" - ) - cmds.setAttr( - "{}.xgFileName".format(xgen_palette), - os.path.basename(xgd_file), - type="string" - ) - cmds.setAttr("{}.xgExportAsDelta".format(xgen_palette), True) + self.set_palette_attributes(xgen_palette, xgen_file, xgd_file) # This create an expression attribute of float. If we did not add # any changes to collection, then Xgen does not create an xgd file @@ -133,10 +125,24 @@ class XgenLoader(openpype.hosts.maya.api.plugin.ReferenceLoader): return new_nodes + def set_palette_attributes(self, xgen_palette, xgen_file, xgd_file): + cmds.setAttr( + "{}.xgBaseFile".format(xgen_palette), + os.path.basename(xgen_file), + type="string" + ) + cmds.setAttr( + "{}.xgFileName".format(xgen_palette), + os.path.basename(xgd_file), + type="string" + ) + cmds.setAttr("{}.xgExportAsDelta".format(xgen_palette), True) + def update(self, container, representation): """Workflow for updating Xgen. - - Copy and overwrite the workspace .xgen file. + - Copy and potentially overwrite the workspace .xgen file. + - Export changes to delta file. - Set collection attributes to not include delta files. - Update xgen maya file reference. - Apply the delta file changes. @@ -144,22 +150,28 @@ class XgenLoader(openpype.hosts.maya.api.plugin.ReferenceLoader): We have to do this workflow because when using referencing of the xgen collection, Maya implicitly imports the Xgen data from the xgen file so - we dont have any control over added the delta file changes. + we dont have any control over when adding the delta file changes. + + There is an implicit increment of the xgen and delta files, due to + using the workfile basename. """ container_node = container["objectName"] members = get_container_members(container_node) + xgen_palette = cmds.ls(members, type="xgmPalette", long=True)[0] reference_node = get_reference_node(members, self.log) namespace = cmds.referenceQuery(reference_node, namespace=True)[1:] - xgen_file = self.setup_xgen_palette_file( + xgen_file, xgd_file = self.setup_xgen_palette_file( get_representation_path(representation), namespace, representation["data"]["xgenName"] ) - xgd_file = xgen_file.replace(".xgen", ".xgd") - xgen_palette = cmds.ls(members, type="xgmPalette", long=True)[0] + # Export current changes to apply later. + xgenm.createDelta(xgen_palette.replace("|", ""), xgd_file) + + self.set_palette_attributes(xgen_palette, xgen_file, xgd_file) attribute_data = { "{}.xgFileName".format(xgen_palette): os.path.basename(xgen_file), From e4cd14c2a633f13146920a2c0cb4a70a09154e8e Mon Sep 17 00:00:00 2001 From: Toke Stuart Jepsen Date: Mon, 23 Jan 2023 17:44:11 +0000 Subject: [PATCH 45/67] Clean up --- .../maya/plugins/publish/extract_xgen.py | 43 ++++++------------- 1 file changed, 13 insertions(+), 30 deletions(-) diff --git a/openpype/hosts/maya/plugins/publish/extract_xgen.py b/openpype/hosts/maya/plugins/publish/extract_xgen.py index 22260df2c7..80b62275cd 100644 --- a/openpype/hosts/maya/plugins/publish/extract_xgen.py +++ b/openpype/hosts/maya/plugins/publish/extract_xgen.py @@ -10,7 +10,14 @@ from openpype.lib import StringTemplate class ExtractXgenCache(publish.Extractor): - """Extract Xgen""" + """Extract Xgen + + Workflow: + - Duplicate nodes used for patches. + - Export palette and import onto duplicate nodes. + - Export/Publish duplicate nodes and palette. + - Publish all xgen files as resources. + """ label = "Extract Xgen" hosts = ["maya"] @@ -55,29 +62,12 @@ class ExtractXgenCache(publish.Extractor): # Duplicate_transform subd patch geometry. duplicate_transform = cmds.duplicate(transform_name)[0] - duplicate_shape = cmds.listRelatives( - duplicate_transform, - shapes=True, - fullPath=True - )[0] # Discard the children. shapes = cmds.listRelatives(duplicate_transform, shapes=True) children = cmds.listRelatives(duplicate_transform, children=True) cmds.delete(set(children) - set(shapes)) - # Connect attributes. - cmds.connectAttr( - "{}.matrix".format(duplicate_transform), - "{}.transform".format(node), - force=True - ) - cmds.connectAttr( - "{}.worldMesh".format(duplicate_shape), - "{}.geometry".format(node), - force=True - ) - duplicate_transform = cmds.parent( duplicate_transform, world=True )[0] @@ -87,17 +77,17 @@ class ExtractXgenCache(publish.Extractor): # Import xgen onto the duplicate. with maintained_selection(): cmds.select(duplicate_nodes) - collection = xgenm.importPalette(xgen_path, []) + palette = xgenm.importPalette(xgen_path, []) attribute_data = { - "{}.xgFileName".format(collection): xgen_filename + "{}.xgFileName".format(palette): xgen_filename } # Export Maya file. type = "mayaAscii" if self.scene_type == "ma" else "mayaBinary" with attribute_values(attribute_data): with maintained_selection(): - cmds.select(duplicate_nodes + [collection]) + cmds.select(duplicate_nodes + [palette]) cmds.file( maya_filepath, force=True, @@ -117,18 +107,11 @@ class ExtractXgenCache(publish.Extractor): "ext": self.scene_type, "files": maya_filename, "stagingDir": staging_dir, - "data": {"xgenName": collection} + "data": {"xgenName": palette} } instance.data["representations"].append(representation) - # Revert to original xgen connections. - for node, connections in instance.data["xgenConnections"].items(): - for attr, src in connections.items(): - cmds.connectAttr( - src, "{}.{}".format(node, attr), force=True - ) - - cmds.delete(duplicate_nodes + [collection]) + cmds.delete(duplicate_nodes + [palette]) # Collect all files under palette root as resources. data_path = xgenm.getAttr( From 6ef2b4be9a5c458364f480325483ead90febdcf9 Mon Sep 17 00:00:00 2001 From: Toke Stuart Jepsen Date: Tue, 24 Jan 2023 16:09:56 +0000 Subject: [PATCH 46/67] Working import --- openpype/hosts/maya/plugins/load/load_xgen.py | 76 +++++++++++++------ .../maya/plugins/publish/extract_xgen.py | 43 ++++------- 2 files changed, 70 insertions(+), 49 deletions(-) diff --git a/openpype/hosts/maya/plugins/load/load_xgen.py b/openpype/hosts/maya/plugins/load/load_xgen.py index 8249c9092e..fec1b389fa 100644 --- a/openpype/hosts/maya/plugins/load/load_xgen.py +++ b/openpype/hosts/maya/plugins/load/load_xgen.py @@ -1,5 +1,6 @@ import os import shutil +import tempfile import maya.cmds as cmds import xgenm @@ -25,6 +26,18 @@ class XgenLoader(openpype.hosts.maya.api.plugin.ReferenceLoader): icon = "code-fork" color = "orange" + def write_xgen_file(self, file_path, data): + lines = [] + with open(file_path, "r") as f: + for key, value in data.items(): + for line in [line.rstrip() for line in f]: + if line.startswith("\t" + key): + line = "\t{}\t\t{}".format(key, value) + lines.append(line) + + with open(file_path, "w") as f: + f.write("\n".join(lines)) + def setup_xgen_palette_file(self, maya_filepath, namespace, name): # Setup xgen palette file. project_path = os.path.dirname(current_file()) @@ -47,28 +60,26 @@ class XgenLoader(openpype.hosts.maya.api.plugin.ReferenceLoader): # and published version directory second. This ensure that any newly # created xgen files are created in the current workspace. resources_path = os.path.join(os.path.dirname(source), "resources") - lines = [] + with open(xgen_file, "r") as f: for line in [line.rstrip() for line in f]: if line.startswith("\txgDataPath"): data_path = line.split("\t")[-1] - line = "\txgDataPath\t\t{}{}{}".format( - data_path, - os.pathsep, - data_path.replace( - "${PROJECT}xgen", resources_path.replace("\\", "/") - ) + + data = { + "xgDataPath": ( + "${{PROJECT}}xgen/collections/{}__ns__{}/{}{}".format( + namespace, + name, + os.pathsep, + data_path.replace( + "${PROJECT}xgen", resources_path.replace("\\", "/") ) - - if line.startswith("\txgProjectPath"): - line = "\txgProjectPath\t\t{}/".format( - project_path.replace("\\", "/") - ) - - lines.append(line) - - with open(xgen_file, "w") as f: - f.write("\n".join(lines)) + ) + ), + "xgProjectPath": project_path.replace("\\", "/") + } + self.write_xgen_file(xgen_file, data) xgd_file = xgen_file.replace(".xgen", ".xgd") @@ -89,14 +100,31 @@ class XgenLoader(openpype.hosts.maya.api.plugin.ReferenceLoader): self.fname, context["project"]["name"] ) - name = context["representation"]["data"]["xgenName"] xgen_file, xgd_file = self.setup_xgen_palette_file( - maya_filepath, namespace, name + maya_filepath, namespace, "collection" ) + # Making temporary copy of xgen file from published so we can + # modify the paths. + temp_xgen_file = os.path.join(tempfile.gettempdir(), "temp.xgen") + _, maya_extension = os.path.splitext(maya_filepath) + source = maya_filepath.replace(maya_extension, ".xgen") + shutil.copy(source, temp_xgen_file) + + resources_path = os.path.join(os.path.dirname(source), "resources") + with open(xgen_file, "r") as f: + for line in [line.rstrip() for line in f]: + if line.startswith("\txgDataPath"): + data_path = line.split("\t")[-1] + data = { + "xgDataPath": data_path.replace( + "${PROJECT}xgen", resources_path.replace("\\", "/") + ) + } + self.write_xgen_file(temp_xgen_file, data) + # Reference xgen. Xgen does not like being referenced in under a group. new_nodes = [] - with maintained_selection(): nodes = cmds.file( maya_filepath, @@ -106,7 +134,11 @@ class XgenLoader(openpype.hosts.maya.api.plugin.ReferenceLoader): returnNewNodes=True ) - xgen_palette = cmds.ls(nodes, type="xgmPalette", long=True)[0] + xgen_palette = xgenm.importPalette( + temp_xgen_file.replace("\\", "/"), [], nameSpace=namespace + ) + os.remove(temp_xgen_file) + self.set_palette_attributes(xgen_palette, xgen_file, xgd_file) # This create an expression attribute of float. If we did not add @@ -119,7 +151,7 @@ class XgenLoader(openpype.hosts.maya.api.plugin.ReferenceLoader): shapes = cmds.ls(nodes, shapes=True, long=True) - new_nodes = (list(set(nodes) - set(shapes))) + new_nodes = (list(set(nodes) - set(shapes)) + [xgen_palette]) self[:] = new_nodes diff --git a/openpype/hosts/maya/plugins/publish/extract_xgen.py b/openpype/hosts/maya/plugins/publish/extract_xgen.py index 80b62275cd..91d4352449 100644 --- a/openpype/hosts/maya/plugins/publish/extract_xgen.py +++ b/openpype/hosts/maya/plugins/publish/extract_xgen.py @@ -5,7 +5,7 @@ from maya import cmds import xgenm from openpype.pipeline import publish -from openpype.hosts.maya.api.lib import maintained_selection, attribute_values +from openpype.hosts.maya.api.lib import maintained_selection from openpype.lib import StringTemplate @@ -74,31 +74,21 @@ class ExtractXgenCache(publish.Extractor): duplicate_nodes.append(duplicate_transform) - # Import xgen onto the duplicate. - with maintained_selection(): - cmds.select(duplicate_nodes) - palette = xgenm.importPalette(xgen_path, []) - - attribute_data = { - "{}.xgFileName".format(palette): xgen_filename - } - # Export Maya file. type = "mayaAscii" if self.scene_type == "ma" else "mayaBinary" - with attribute_values(attribute_data): - with maintained_selection(): - cmds.select(duplicate_nodes + [palette]) - cmds.file( - maya_filepath, - force=True, - type=type, - exportSelected=True, - preserveReferences=False, - constructionHistory=True, - shader=True, - constraints=True, - expressions=True - ) + with maintained_selection(): + cmds.select(duplicate_nodes) + cmds.file( + maya_filepath, + force=True, + type=type, + exportSelected=True, + preserveReferences=False, + constructionHistory=True, + shader=True, + constraints=True, + expressions=True + ) self.log.info("Extracted to {}".format(maya_filepath)) @@ -106,12 +96,11 @@ class ExtractXgenCache(publish.Extractor): "name": self.scene_type, "ext": self.scene_type, "files": maya_filename, - "stagingDir": staging_dir, - "data": {"xgenName": palette} + "stagingDir": staging_dir } instance.data["representations"].append(representation) - cmds.delete(duplicate_nodes + [palette]) + cmds.delete(duplicate_nodes) # Collect all files under palette root as resources. data_path = xgenm.getAttr( From 40cf86ea970970a8977f2bc92d85889f654b7cb6 Mon Sep 17 00:00:00 2001 From: Toke Stuart Jepsen Date: Wed, 25 Jan 2023 09:38:17 +0000 Subject: [PATCH 47/67] Revert "Working import" This reverts commit 6ef2b4be9a5c458364f480325483ead90febdcf9. --- openpype/hosts/maya/plugins/load/load_xgen.py | 76 ++++++------------- .../maya/plugins/publish/extract_xgen.py | 43 +++++++---- 2 files changed, 49 insertions(+), 70 deletions(-) diff --git a/openpype/hosts/maya/plugins/load/load_xgen.py b/openpype/hosts/maya/plugins/load/load_xgen.py index fec1b389fa..8249c9092e 100644 --- a/openpype/hosts/maya/plugins/load/load_xgen.py +++ b/openpype/hosts/maya/plugins/load/load_xgen.py @@ -1,6 +1,5 @@ import os import shutil -import tempfile import maya.cmds as cmds import xgenm @@ -26,18 +25,6 @@ class XgenLoader(openpype.hosts.maya.api.plugin.ReferenceLoader): icon = "code-fork" color = "orange" - def write_xgen_file(self, file_path, data): - lines = [] - with open(file_path, "r") as f: - for key, value in data.items(): - for line in [line.rstrip() for line in f]: - if line.startswith("\t" + key): - line = "\t{}\t\t{}".format(key, value) - lines.append(line) - - with open(file_path, "w") as f: - f.write("\n".join(lines)) - def setup_xgen_palette_file(self, maya_filepath, namespace, name): # Setup xgen palette file. project_path = os.path.dirname(current_file()) @@ -60,26 +47,28 @@ class XgenLoader(openpype.hosts.maya.api.plugin.ReferenceLoader): # and published version directory second. This ensure that any newly # created xgen files are created in the current workspace. resources_path = os.path.join(os.path.dirname(source), "resources") - + lines = [] with open(xgen_file, "r") as f: for line in [line.rstrip() for line in f]: if line.startswith("\txgDataPath"): data_path = line.split("\t")[-1] - - data = { - "xgDataPath": ( - "${{PROJECT}}xgen/collections/{}__ns__{}/{}{}".format( - namespace, - name, - os.pathsep, - data_path.replace( - "${PROJECT}xgen", resources_path.replace("\\", "/") + line = "\txgDataPath\t\t{}{}{}".format( + data_path, + os.pathsep, + data_path.replace( + "${PROJECT}xgen", resources_path.replace("\\", "/") + ) ) - ) - ), - "xgProjectPath": project_path.replace("\\", "/") - } - self.write_xgen_file(xgen_file, data) + + if line.startswith("\txgProjectPath"): + line = "\txgProjectPath\t\t{}/".format( + project_path.replace("\\", "/") + ) + + lines.append(line) + + with open(xgen_file, "w") as f: + f.write("\n".join(lines)) xgd_file = xgen_file.replace(".xgen", ".xgd") @@ -100,31 +89,14 @@ class XgenLoader(openpype.hosts.maya.api.plugin.ReferenceLoader): self.fname, context["project"]["name"] ) + name = context["representation"]["data"]["xgenName"] xgen_file, xgd_file = self.setup_xgen_palette_file( - maya_filepath, namespace, "collection" + maya_filepath, namespace, name ) - # Making temporary copy of xgen file from published so we can - # modify the paths. - temp_xgen_file = os.path.join(tempfile.gettempdir(), "temp.xgen") - _, maya_extension = os.path.splitext(maya_filepath) - source = maya_filepath.replace(maya_extension, ".xgen") - shutil.copy(source, temp_xgen_file) - - resources_path = os.path.join(os.path.dirname(source), "resources") - with open(xgen_file, "r") as f: - for line in [line.rstrip() for line in f]: - if line.startswith("\txgDataPath"): - data_path = line.split("\t")[-1] - data = { - "xgDataPath": data_path.replace( - "${PROJECT}xgen", resources_path.replace("\\", "/") - ) - } - self.write_xgen_file(temp_xgen_file, data) - # Reference xgen. Xgen does not like being referenced in under a group. new_nodes = [] + with maintained_selection(): nodes = cmds.file( maya_filepath, @@ -134,11 +106,7 @@ class XgenLoader(openpype.hosts.maya.api.plugin.ReferenceLoader): returnNewNodes=True ) - xgen_palette = xgenm.importPalette( - temp_xgen_file.replace("\\", "/"), [], nameSpace=namespace - ) - os.remove(temp_xgen_file) - + xgen_palette = cmds.ls(nodes, type="xgmPalette", long=True)[0] self.set_palette_attributes(xgen_palette, xgen_file, xgd_file) # This create an expression attribute of float. If we did not add @@ -151,7 +119,7 @@ class XgenLoader(openpype.hosts.maya.api.plugin.ReferenceLoader): shapes = cmds.ls(nodes, shapes=True, long=True) - new_nodes = (list(set(nodes) - set(shapes)) + [xgen_palette]) + new_nodes = (list(set(nodes) - set(shapes))) self[:] = new_nodes diff --git a/openpype/hosts/maya/plugins/publish/extract_xgen.py b/openpype/hosts/maya/plugins/publish/extract_xgen.py index 91d4352449..80b62275cd 100644 --- a/openpype/hosts/maya/plugins/publish/extract_xgen.py +++ b/openpype/hosts/maya/plugins/publish/extract_xgen.py @@ -5,7 +5,7 @@ from maya import cmds import xgenm from openpype.pipeline import publish -from openpype.hosts.maya.api.lib import maintained_selection +from openpype.hosts.maya.api.lib import maintained_selection, attribute_values from openpype.lib import StringTemplate @@ -74,21 +74,31 @@ class ExtractXgenCache(publish.Extractor): duplicate_nodes.append(duplicate_transform) - # Export Maya file. - type = "mayaAscii" if self.scene_type == "ma" else "mayaBinary" + # Import xgen onto the duplicate. with maintained_selection(): cmds.select(duplicate_nodes) - cmds.file( - maya_filepath, - force=True, - type=type, - exportSelected=True, - preserveReferences=False, - constructionHistory=True, - shader=True, - constraints=True, - expressions=True - ) + palette = xgenm.importPalette(xgen_path, []) + + attribute_data = { + "{}.xgFileName".format(palette): xgen_filename + } + + # Export Maya file. + type = "mayaAscii" if self.scene_type == "ma" else "mayaBinary" + with attribute_values(attribute_data): + with maintained_selection(): + cmds.select(duplicate_nodes + [palette]) + cmds.file( + maya_filepath, + force=True, + type=type, + exportSelected=True, + preserveReferences=False, + constructionHistory=True, + shader=True, + constraints=True, + expressions=True + ) self.log.info("Extracted to {}".format(maya_filepath)) @@ -96,11 +106,12 @@ class ExtractXgenCache(publish.Extractor): "name": self.scene_type, "ext": self.scene_type, "files": maya_filename, - "stagingDir": staging_dir + "stagingDir": staging_dir, + "data": {"xgenName": palette} } instance.data["representations"].append(representation) - cmds.delete(duplicate_nodes) + cmds.delete(duplicate_nodes + [palette]) # Collect all files under palette root as resources. data_path = xgenm.getAttr( From 1f08a2734339ae7c40424df778d7402807beed10 Mon Sep 17 00:00:00 2001 From: Toke Stuart Jepsen Date: Mon, 30 Jan 2023 10:15:04 +0000 Subject: [PATCH 48/67] Workaround for motion blur --- .../plugins/publish/extract_workfile_xgen.py | 147 +++++++++++++----- .../maya/plugins/publish/extract_xgen.py | 62 ++++---- 2 files changed, 146 insertions(+), 63 deletions(-) diff --git a/openpype/hosts/maya/plugins/publish/extract_workfile_xgen.py b/openpype/hosts/maya/plugins/publish/extract_workfile_xgen.py index d37a03d1f6..5847563e5b 100644 --- a/openpype/hosts/maya/plugins/publish/extract_workfile_xgen.py +++ b/openpype/hosts/maya/plugins/publish/extract_workfile_xgen.py @@ -1,11 +1,14 @@ import os import shutil +import copy from maya import cmds import pyblish.api from openpype.hosts.maya.api import current_file +from openpype.hosts.maya.api.lib import extract_alembic from openpype.pipeline import publish +from openpype.lib import StringTemplate class ExtractWorkfileXgen(publish.Extractor): @@ -18,9 +21,22 @@ class ExtractWorkfileXgen(publish.Extractor): hosts = ["maya"] def process(self, instance): + transfers = [] + + # Validate there is any palettes in the scene. + if not cmds.ls(type="xgmPalette"): + self.log.debug( + "No collections found in the scene. Abort Xgen extraction." + ) + return + else: + import xgenm + # Validate to extract only when we are publishing a renderlayer as # well. renderlayer = False + start_frame = None + end_frame = None for i in instance.context: is_renderlayer = ( "renderlayer" in i.data.get("families", []) or @@ -28,6 +44,17 @@ class ExtractWorkfileXgen(publish.Extractor): ) if is_renderlayer and i.data["publish"]: renderlayer = True + + if start_frame is None: + start_frame = i.data["frameStart"] + if end_frame is None: + end_frame = i.data["frameEnd"] + + if i.data["frameStart"] < start_frame: + start_frame = i.data["frameStart"] + if i.data["frameEnd"] > end_frame: + end_frame = i.data["frameEnd"] + break if not renderlayer: @@ -37,6 +64,51 @@ class ExtractWorkfileXgen(publish.Extractor): ) return + # We decrement start frame and increment end frame so motion blur will + # render correctly. + start_frame -= 1 + end_frame += 1 + + # Extract patches alembic. + basename, _ = os.path.splitext(current_file()) + dirname = os.path.dirname(current_file()) + kwargs = {"attrPrefix": ["xgen"], "stripNamespaces": True} + alembic_files = [] + for palette in cmds.ls(type="xgmPalette"): + patch_names = [] + for description in xgenm.descriptions(palette): + for name in xgenm.boundGeometry(palette, description): + patch_names.append(name) + + alembic_file = os.path.join( + dirname, + "{}__{}.abc".format(basename, palette.replace(":", "__ns__")) + ) + extract_alembic( + alembic_file, + root=patch_names, + selection=False, + startFrame=float(start_frame), + endFrame=float(end_frame), + verbose=True, + **kwargs + ) + alembic_files.append(alembic_file) + + template_data = copy.deepcopy(instance.data["anatomyData"]) + published_maya_path = StringTemplate( + instance.context.data["anatomy"].templates["publish"]["file"] + ).format(template_data) + published_basename, _ = os.path.splitext(published_maya_path) + + for source in alembic_files: + destination = os.path.join( + os.path.dirname(instance.data["resourcesDir"]), + os.path.basename(source.replace(basename, published_basename)) + ) + transfers.append((source, destination)) + + # Validate that we are using the published workfile. deadline_settings = instance.context.get("deadline") if deadline_settings: publish_settings = deadline_settings["publish"] @@ -76,8 +148,9 @@ class ExtractWorkfileXgen(publish.Extractor): with open(destination, "r") as f: for line in [line.rstrip() for line in f]: if line.startswith("\txgProjectPath"): + path = os.path.dirname(instance.data["resourcesDir"]) line = "\txgProjectPath\t\t{}/".format( - instance.data["resourcesDir"].replace("\\", "/") + path.replace("\\", "/") ) lines.append(line) @@ -88,31 +161,30 @@ class ExtractWorkfileXgen(publish.Extractor): sources.append(destination) # Add resource files to workfile instance. - transfers = [] for source in sources: basename = os.path.basename(source) - destination = os.path.join(instance.data["resourcesDir"], basename) + destination = os.path.join( + os.path.dirname(instance.data["resourcesDir"]), basename + ) transfers.append((source, destination)) - import xgenm + destination_dir = os.path.join( + instance.data["resourcesDir"], "collections" + ) for palette in cmds.ls(type="xgmPalette"): - relative_data_path = xgenm.getAttr( - "xgDataPath", palette.replace("|", "") - ).split(os.pathsep)[0] - absolute_data_path = relative_data_path.replace( - "${PROJECT}", - xgenm.getAttr("xgProjectPath", palette.replace("|", "")) - ) - - for root, _, files in os.walk(absolute_data_path): - for file in files: - source = os.path.join(root, file).replace("\\", "/") - destination = os.path.join( - instance.data["resourcesDir"], - relative_data_path.replace("${PROJECT}", ""), - source.replace(absolute_data_path, "")[1:] - ) - transfers.append((source, destination.replace("\\", "/"))) + project_path = xgenm.getAttr("xgProjectPath", palette) + data_path = xgenm.getAttr("xgDataPath", palette) + data_path = data_path.replace("${PROJECT}", project_path) + for path in data_path.split(os.pathsep): + for root, _, files in os.walk(path): + for f in files: + source = os.path.join(root, f) + destination = "{}/{}{}".format( + destination_dir, + palette.replace(":", "__ns__"), + source.replace(path, "") + ) + transfers.append((source, destination)) for source, destination in transfers: self.log.debug("Transfer: {} > {}".format(source, destination)) @@ -120,21 +192,26 @@ class ExtractWorkfileXgen(publish.Extractor): instance.data["transfers"] = transfers # Set palette attributes in preparation for workfile publish. - attrs = ["xgFileName", "xgBaseFile"] + attrs = {"xgFileName": None, "xgBaseFile": ""} data = {} for palette in cmds.ls(type="xgmPalette"): - for attr in attrs: - value = cmds.getAttr(palette + "." + attr) - if value: - new_value = "resources/{}".format(value) - node_attr = "{}.{}".format(palette, attr) - self.log.info( - "Setting \"{}\" on \"{}\"".format(new_value, node_attr) - ) - cmds.setAttr(node_attr, new_value, type="string") - try: - data[palette][attr] = value - except KeyError: - data[palette] = {attr: value} + attrs["xgFileName"] = "resources/{}.xgen".format( + palette.replace(":", "__ns__") + ) + for attr, value in attrs.items(): + node_attr = palette + "." + attr + + old_value = cmds.getAttr(node_attr) + try: + data[palette][attr] = old_value + except KeyError: + data[palette] = {attr: old_value} + + cmds.setAttr(node_attr, value, type="string") + self.log.info( + "Setting \"{}\" on \"{}\"".format(value, node_attr) + ) + + cmds.setAttr(palette + "." + "xgExportAsDelta", False) instance.data["xgenAttributes"] = data diff --git a/openpype/hosts/maya/plugins/publish/extract_xgen.py b/openpype/hosts/maya/plugins/publish/extract_xgen.py index 80b62275cd..fd85cadcac 100644 --- a/openpype/hosts/maya/plugins/publish/extract_xgen.py +++ b/openpype/hosts/maya/plugins/publish/extract_xgen.py @@ -1,5 +1,6 @@ import os import copy +import tempfile from maya import cmds import xgenm @@ -16,6 +17,7 @@ class ExtractXgenCache(publish.Extractor): - Duplicate nodes used for patches. - Export palette and import onto duplicate nodes. - Export/Publish duplicate nodes and palette. + - Export duplicate palette to .xgen file and add to publish. - Publish all xgen files as resources. """ @@ -32,29 +34,6 @@ class ExtractXgenCache(publish.Extractor): maya_filename = "{}.{}".format(instance.data["name"], self.scene_type) maya_filepath = os.path.join(staging_dir, maya_filename) - # Get published xgen file name. - template_data = copy.deepcopy(instance.data["anatomyData"]) - template_data.update({"ext": "xgen"}) - templates = instance.context.data["anatomy"].templates["publish"] - xgen_filename = StringTemplate(templates["file"]).format(template_data) - name = instance.data["xgmPalette"].replace(":", "__").replace("|", "") - xgen_filename = xgen_filename.replace(".xgen", "__" + name + ".xgen") - - # Export xgen palette files. - xgen_path = os.path.join(staging_dir, xgen_filename).replace("\\", "/") - xgenm.exportPalette( - instance.data["xgmPalette"].replace("|", ""), xgen_path - ) - self.log.info("Extracted to {}".format(xgen_path)) - - representation = { - "name": name, - "ext": "xgen", - "files": xgen_filename, - "stagingDir": staging_dir, - } - instance.data["representations"].append(representation) - # Collect nodes to export. duplicate_nodes = [] for node, connections in instance.data["xgenConnections"].items(): @@ -74,17 +53,43 @@ class ExtractXgenCache(publish.Extractor): duplicate_nodes.append(duplicate_transform) + # Export temp xgen palette files. + temp_xgen_path = os.path.join( + tempfile.gettempdir(), "temp.xgen" + ).replace("\\", "/") + xgenm.exportPalette( + instance.data["xgmPalette"].replace("|", ""), temp_xgen_path + ) + self.log.info("Extracted to {}".format(temp_xgen_path)) + # Import xgen onto the duplicate. with maintained_selection(): cmds.select(duplicate_nodes) - palette = xgenm.importPalette(xgen_path, []) + palette = xgenm.importPalette(temp_xgen_path, []) - attribute_data = { - "{}.xgFileName".format(palette): xgen_filename + # Get published xgen file name. + template_data = copy.deepcopy(instance.data["anatomyData"]) + template_data.update({"ext": "xgen"}) + templates = instance.context.data["anatomy"].templates["publish"] + xgen_filename = StringTemplate(templates["file"]).format(template_data) + + # Export duplicated palette. + xgen_path = os.path.join(staging_dir, xgen_filename).replace("\\", "/") + xgenm.exportPalette(palette, xgen_path) + + representation = { + "name": "xgen", + "ext": "xgen", + "files": xgen_filename, + "stagingDir": staging_dir, } + instance.data["representations"].append(representation) # Export Maya file. type = "mayaAscii" if self.scene_type == "ma" else "mayaBinary" + attribute_data = { + "{}.xgFileName".format(palette): xgen_filename + } with attribute_values(attribute_data): with maintained_selection(): cmds.select(duplicate_nodes + [palette]) @@ -106,12 +111,13 @@ class ExtractXgenCache(publish.Extractor): "name": self.scene_type, "ext": self.scene_type, "files": maya_filename, - "stagingDir": staging_dir, - "data": {"xgenName": palette} + "stagingDir": staging_dir } instance.data["representations"].append(representation) + # Clean up. cmds.delete(duplicate_nodes + [palette]) + os.remove(temp_xgen_path) # Collect all files under palette root as resources. data_path = xgenm.getAttr( From c908bd2abaeb2a44b8beb88a4ed30aa1f2dad56c Mon Sep 17 00:00:00 2001 From: Toke Stuart Jepsen Date: Mon, 30 Jan 2023 10:15:27 +0000 Subject: [PATCH 49/67] Workaround for motion blur --- openpype/hosts/maya/plugins/load/load_xgen.py | 76 +++++++------------ 1 file changed, 28 insertions(+), 48 deletions(-) diff --git a/openpype/hosts/maya/plugins/load/load_xgen.py b/openpype/hosts/maya/plugins/load/load_xgen.py index 8249c9092e..0f2e13dd79 100644 --- a/openpype/hosts/maya/plugins/load/load_xgen.py +++ b/openpype/hosts/maya/plugins/load/load_xgen.py @@ -1,5 +1,4 @@ import os -import shutil import maya.cmds as cmds import xgenm @@ -12,7 +11,6 @@ from openpype.hosts.maya.api.lib import ( ) 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): @@ -25,55 +23,19 @@ class XgenLoader(openpype.hosts.maya.api.plugin.ReferenceLoader): icon = "code-fork" color = "orange" - def setup_xgen_palette_file(self, maya_filepath, namespace, name): - # Setup xgen palette file. - project_path = os.path.dirname(current_file()) - - # Copy the xgen palette file from published version. - _, maya_extension = os.path.splitext(maya_filepath) - source = maya_filepath.replace(maya_extension, ".xgen") - xgen_file = os.path.join( - project_path, - "{basename}__{namespace}__{name}.xgen".format( - basename=os.path.splitext(os.path.basename(current_file()))[0], - namespace=namespace, - name=name - ) - ).replace("\\", "/") - self.log.info("Copying {} to {}".format(source, xgen_file)) - shutil.copy(source, xgen_file) - - # Modify xgDataPath and xgProjectPath to have current workspace first - # and published version directory second. This ensure that any newly - # created xgen files are created in the current workspace. - resources_path = os.path.join(os.path.dirname(source), "resources") + def write_xgen_file(self, data, xgen_file): lines = [] with open(xgen_file, "r") as f: for line in [line.rstrip() for line in f]: - if line.startswith("\txgDataPath"): - data_path = line.split("\t")[-1] - line = "\txgDataPath\t\t{}{}{}".format( - data_path, - os.pathsep, - data_path.replace( - "${PROJECT}xgen", resources_path.replace("\\", "/") - ) - ) - - if line.startswith("\txgProjectPath"): - line = "\txgProjectPath\t\t{}/".format( - project_path.replace("\\", "/") - ) + for key, value in data.items(): + if line.startswith("\t{}".format(key)): + line = "\t{}\t\t{}".format(key, value) lines.append(line) with open(xgen_file, "w") as f: f.write("\n".join(lines)) - xgd_file = xgen_file.replace(".xgen", ".xgd") - - return xgen_file, xgd_file - def process_reference(self, context, name, namespace, options): # Validate workfile has a path. if current_file() is None: @@ -89,11 +51,6 @@ class XgenLoader(openpype.hosts.maya.api.plugin.ReferenceLoader): self.fname, context["project"]["name"] ) - name = context["representation"]["data"]["xgenName"] - xgen_file, xgd_file = self.setup_xgen_palette_file( - maya_filepath, namespace, name - ) - # Reference xgen. Xgen does not like being referenced in under a group. new_nodes = [] @@ -106,9 +63,32 @@ class XgenLoader(openpype.hosts.maya.api.plugin.ReferenceLoader): returnNewNodes=True ) - xgen_palette = cmds.ls(nodes, type="xgmPalette", long=True)[0] + xgen_palette = cmds.ls( + nodes, type="xgmPalette", long=True + )[0].replace("|", "") + + _, maya_extension = os.path.splitext(current_file()) + xgen_file = current_file().replace( + maya_extension, + "__{}.xgen".format(xgen_palette.replace(":", "__")) + ) + xgd_file = xgen_file.replace(".xgen", ".xgd") self.set_palette_attributes(xgen_palette, xgen_file, xgd_file) + # Change the cache and disk values of xgDataPath and xgProjectPath + # to ensure paths are setup correctly. + project_path = os.path.dirname(current_file()).replace("\\", "/") + xgenm.setAttr("xgProjectPath", project_path, xgen_palette) + data_path = "${{PROJECT}}xgen/collections/{}{}{}".format( + xgen_palette.replace(":", "__ns__"), + os.pathsep, + xgenm.getAttr("xgDataPath", xgen_palette) + ) + xgenm.setAttr("xgDataPath", data_path, xgen_palette) + + data = {"xgProjectPath": project_path, "xgDataPath": data_path} + self.write_xgen_file(data, xgen_file) + # This create an expression attribute of float. If we did not add # any changes to collection, then Xgen does not create an xgd file # on save. This gives errors when launching the workfile again due From a2176420b7ed584d10b7bff37d729a0924000b39 Mon Sep 17 00:00:00 2001 From: Toke Stuart Jepsen Date: Mon, 30 Jan 2023 17:20:39 +0000 Subject: [PATCH 50/67] Working extraction --- .../hosts/maya/plugins/publish/extract_xgen.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/openpype/hosts/maya/plugins/publish/extract_xgen.py b/openpype/hosts/maya/plugins/publish/extract_xgen.py index fd85cadcac..9549aba76d 100644 --- a/openpype/hosts/maya/plugins/publish/extract_xgen.py +++ b/openpype/hosts/maya/plugins/publish/extract_xgen.py @@ -6,7 +6,9 @@ from maya import cmds import xgenm from openpype.pipeline import publish -from openpype.hosts.maya.api.lib import maintained_selection, attribute_values +from openpype.hosts.maya.api.lib import ( + maintained_selection, attribute_values, write_xgen_file +) from openpype.lib import StringTemplate @@ -77,6 +79,18 @@ class ExtractXgenCache(publish.Extractor): xgen_path = os.path.join(staging_dir, xgen_filename).replace("\\", "/") xgenm.exportPalette(palette, xgen_path) + data = { + "xgDataPath": os.path.join( + instance.data["resourcesDir"], + "collections", + palette.replace(":", "__ns__") + ).replace("\\", "/"), + "xgProjectPath": os.path.dirname( + instance.data["resourcesDir"] + ).replace("\\", "/") + } + write_xgen_file(data, xgen_path) + representation = { "name": "xgen", "ext": "xgen", @@ -136,7 +150,7 @@ class ExtractXgenCache(publish.Extractor): destination = os.path.join( instance.data["resourcesDir"], "collections", - os.path.basename(data_path), + palette, source.replace(data_path, "")[1:] ) transfers.append((source, destination.replace("\\", "/"))) From 5a280a32bbec68d6a9284f005f062bf387a2a476 Mon Sep 17 00:00:00 2001 From: Toke Stuart Jepsen Date: Mon, 30 Jan 2023 17:20:46 +0000 Subject: [PATCH 51/67] Working updating --- openpype/hosts/maya/plugins/load/load_xgen.py | 68 +++++++++++-------- 1 file changed, 38 insertions(+), 30 deletions(-) diff --git a/openpype/hosts/maya/plugins/load/load_xgen.py b/openpype/hosts/maya/plugins/load/load_xgen.py index 0f2e13dd79..35f7c21c58 100644 --- a/openpype/hosts/maya/plugins/load/load_xgen.py +++ b/openpype/hosts/maya/plugins/load/load_xgen.py @@ -7,10 +7,13 @@ from Qt import QtWidgets import openpype.hosts.maya.api.plugin from openpype.hosts.maya.api.lib import ( - maintained_selection, get_container_members, attribute_values + maintained_selection, + get_container_members, + attribute_values, + write_xgen_file ) 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): @@ -23,18 +26,14 @@ class XgenLoader(openpype.hosts.maya.api.plugin.ReferenceLoader): icon = "code-fork" color = "orange" - def write_xgen_file(self, data, xgen_file): - lines = [] - with open(xgen_file, "r") as f: - for line in [line.rstrip() for line in f]: - for key, value in data.items(): - if line.startswith("\t{}".format(key)): - line = "\t{}\t\t{}".format(key, value) - - lines.append(line) - - with open(xgen_file, "w") as f: - f.write("\n".join(lines)) + def get_xgen_xgd_paths(self, palette): + _, maya_extension = os.path.splitext(current_file()) + xgen_file = current_file().replace( + maya_extension, + "__{}.xgen".format(palette.replace("|", "").replace(":", "__")) + ) + xgd_file = xgen_file.replace(".xgen", ".xgd") + return xgen_file, xgd_file def process_reference(self, context, name, namespace, options): # Validate workfile has a path. @@ -67,12 +66,7 @@ class XgenLoader(openpype.hosts.maya.api.plugin.ReferenceLoader): nodes, type="xgmPalette", long=True )[0].replace("|", "") - _, maya_extension = os.path.splitext(current_file()) - xgen_file = current_file().replace( - maya_extension, - "__{}.xgen".format(xgen_palette.replace(":", "__")) - ) - xgd_file = xgen_file.replace(".xgen", ".xgd") + xgen_file, xgd_file = self.get_xgen_xgd_paths(xgen_palette) self.set_palette_attributes(xgen_palette, xgen_file, xgd_file) # Change the cache and disk values of xgDataPath and xgProjectPath @@ -87,7 +81,7 @@ class XgenLoader(openpype.hosts.maya.api.plugin.ReferenceLoader): xgenm.setAttr("xgDataPath", data_path, xgen_palette) data = {"xgProjectPath": project_path, "xgDataPath": data_path} - self.write_xgen_file(data, xgen_file) + write_xgen_file(data, xgen_file) # This create an expression attribute of float. If we did not add # any changes to collection, then Xgen does not create an xgd file @@ -138,21 +132,35 @@ class XgenLoader(openpype.hosts.maya.api.plugin.ReferenceLoader): container_node = container["objectName"] members = get_container_members(container_node) - xgen_palette = cmds.ls(members, type="xgmPalette", long=True)[0] - reference_node = get_reference_node(members, self.log) - namespace = cmds.referenceQuery(reference_node, namespace=True)[1:] - - xgen_file, xgd_file = self.setup_xgen_palette_file( - get_representation_path(representation), - namespace, - representation["data"]["xgenName"] - ) + xgen_palette = cmds.ls( + members, type="xgmPalette", long=True + )[0].replace("|", "") + xgen_file, xgd_file = self.get_xgen_xgd_paths(xgen_palette) # Export current changes to apply later. xgenm.createDelta(xgen_palette.replace("|", ""), xgd_file) self.set_palette_attributes(xgen_palette, xgen_file, xgd_file) + maya_file = get_representation_path(representation) + _, extension = os.path.splitext(maya_file) + new_xgen_file = maya_file.replace(extension, ".xgen") + data_path = "" + with open(new_xgen_file, "r") as f: + for line in [line.rstrip() for line in f]: + if line.startswith("\txgDataPath"): + data_path = line.split("\t")[-1] + break + + project_path = os.path.dirname(current_file()).replace("\\", "/") + data_path = "${{PROJECT}}xgen/collections/{}{}{}".format( + xgen_palette.replace(":", "__ns__"), + os.pathsep, + data_path + ) + data = {"xgProjectPath": project_path, "xgDataPath": data_path} + write_xgen_file(data, xgen_file) + attribute_data = { "{}.xgFileName".format(xgen_palette): os.path.basename(xgen_file), "{}.xgBaseFile".format(xgen_palette): "", From 22ddb58cca40a320af68688e073cf98bb9e73fbc Mon Sep 17 00:00:00 2001 From: Toke Stuart Jepsen Date: Mon, 30 Jan 2023 17:20:58 +0000 Subject: [PATCH 52/67] Refactor to lib --- openpype/hosts/maya/api/lib.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/openpype/hosts/maya/api/lib.py b/openpype/hosts/maya/api/lib.py index dd5da275e8..65f39270f5 100644 --- a/openpype/hosts/maya/api/lib.py +++ b/openpype/hosts/maya/api/lib.py @@ -3446,3 +3446,17 @@ def iter_visible_nodes_in_range(nodes, start, end): def get_attribute_input(attr): connections = cmds.listConnections(attr, plugs=True, destination=False) return connections[0] if connections else None + + +def write_xgen_file(data, filepath): + lines = [] + with open(filepath, "r") as f: + for line in [line.rstrip() for line in f]: + for key, value in data.items(): + if line.startswith("\t{}".format(key)): + line = "\t{}\t\t{}".format(key, value) + + lines.append(line) + + with open(filepath, "w") as f: + f.write("\n".join(lines)) From 2e9f60693b2da0b824ae8ad5283f445673e0c29a Mon Sep 17 00:00:00 2001 From: Toke Stuart Jepsen Date: Mon, 30 Jan 2023 18:05:52 +0000 Subject: [PATCH 53/67] Fix Edge case of loading xgen while existing xgen in cache. --- openpype/hosts/maya/plugins/load/load_xgen.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/openpype/hosts/maya/plugins/load/load_xgen.py b/openpype/hosts/maya/plugins/load/load_xgen.py index 35f7c21c58..d0ed3e05b3 100644 --- a/openpype/hosts/maya/plugins/load/load_xgen.py +++ b/openpype/hosts/maya/plugins/load/load_xgen.py @@ -87,9 +87,11 @@ class XgenLoader(openpype.hosts.maya.api.plugin.ReferenceLoader): # any changes to collection, then Xgen does not create an xgd file # on save. This gives errors when launching the workfile again due # to trying to find the xgd file. - xgenm.addCustomAttr( - "custom_float_ignore", xgen_palette.replace("|", "") - ) + name = "custom_float_ignore" + if name not in xgenm.customAttrs(xgen_palette): + xgenm.addCustomAttr( + "custom_float_ignore", xgen_palette + ) shapes = cmds.ls(nodes, shapes=True, long=True) From 8ec87cba01a8c0d91c67843d6c575cf647cd18e3 Mon Sep 17 00:00:00 2001 From: Toke Stuart Jepsen Date: Mon, 30 Jan 2023 18:12:53 +0000 Subject: [PATCH 54/67] Fix Resetting Xgen attributes after incremental save. --- .../maya/plugins/publish/reset_xgen_attributes.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/openpype/hosts/maya/plugins/publish/reset_xgen_attributes.py b/openpype/hosts/maya/plugins/publish/reset_xgen_attributes.py index 0f763613c9..d3408f2c76 100644 --- a/openpype/hosts/maya/plugins/publish/reset_xgen_attributes.py +++ b/openpype/hosts/maya/plugins/publish/reset_xgen_attributes.py @@ -7,8 +7,8 @@ class ResetXgenAttributes(pyblish.api.InstancePlugin): """Reset Xgen attributes.""" label = "Reset Xgen Attributes." - # Offset to run after global integrator. - order = pyblish.api.IntegratorOrder + 1.0 + # Offset to run after workfile increment plugin. + order = pyblish.api.IntegratorOrder + 10.0 families = ["workfile"] def process(self, instance): @@ -19,3 +19,9 @@ class ResetXgenAttributes(pyblish.api.InstancePlugin): "Setting \"{}\" on \"{}\"".format(value, node_attr) ) cmds.setAttr(node_attr, value, type="string") + + cmds.setAttr(palette + "." + "xgExportAsDelta", True) + + if instance.data.get("xgenAttributes", {}): + self.log.info("Saving changes.") + cmds.file(save=True) From f1e8803f590e90e0cf38e5601cad97b12a5f97ae Mon Sep 17 00:00:00 2001 From: Toke Stuart Jepsen Date: Mon, 30 Jan 2023 18:18:37 +0000 Subject: [PATCH 55/67] Update docs --- website/docs/artist_hosts_maya_xgen.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/website/docs/artist_hosts_maya_xgen.md b/website/docs/artist_hosts_maya_xgen.md index fc75959f2b..191826f49c 100644 --- a/website/docs/artist_hosts_maya_xgen.md +++ b/website/docs/artist_hosts_maya_xgen.md @@ -8,7 +8,7 @@ sidebar_label: Xgen ### Settings -Go to project settings > Maya > enable "Open Workfile Post Initialization"; +Go to project settings > `Maya` > enable `Open Workfile Post Initialization`; `project_settings/maya/open_workfile_post_initialization` @@ -27,6 +27,12 @@ Importing XGen Collections... # Error: XGen: Failed to import collection from file P:/PROJECTS/OP01_CG_demo/shots/sh040/work/Lighting/cg_ball_xgenMain_v035__ball_rigMain_01___collection.xgen # ``` +Go to project settings > `Deadline` > `Publish plugins` > `Maya Submit to Deadline` > disable `Use Published scene`; + +`project_settings/deadline/publish/MayaSubmitDeadline/use_published` + +This is due to temporary workaround while fixing rendering with published scenes. + ## Create Create an Xgen instance to publish. This needs to contain only **one Xgen collection**. From aad5a4f6eb84f6d131dc0c7e9f3f2c66b9772837 Mon Sep 17 00:00:00 2001 From: Toke Jepsen Date: Tue, 31 Jan 2023 11:36:07 +0000 Subject: [PATCH 56/67] Update openpype/hosts/maya/plugins/publish/reset_xgen_attributes.py Co-authored-by: Roy Nieterau --- .../plugins/publish/reset_xgen_attributes.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/openpype/hosts/maya/plugins/publish/reset_xgen_attributes.py b/openpype/hosts/maya/plugins/publish/reset_xgen_attributes.py index d3408f2c76..08f367f2d5 100644 --- a/openpype/hosts/maya/plugins/publish/reset_xgen_attributes.py +++ b/openpype/hosts/maya/plugins/publish/reset_xgen_attributes.py @@ -12,16 +12,18 @@ class ResetXgenAttributes(pyblish.api.InstancePlugin): families = ["workfile"] def process(self, instance): - for palette, data in instance.data.get("xgenAttributes", {}).items(): + xgen_attributes = instance.data.get("xgenAttributes", {}) + if not xgen_attributes : + return + + for palette, data in xgen_attributes.items(): for attr, value in data.items(): node_attr = "{}.{}".format(palette, attr) self.log.info( "Setting \"{}\" on \"{}\"".format(value, node_attr) ) cmds.setAttr(node_attr, value, type="string") - - cmds.setAttr(palette + "." + "xgExportAsDelta", True) - - if instance.data.get("xgenAttributes", {}): - self.log.info("Saving changes.") - cmds.file(save=True) + cmds.setAttr(palette + ".xgExportAsDelta", True) + + self.log.info("Saving changes.") + cmds.file(save=True) From 768b3d8ac9d4ab72a8d776b115232df079802dda Mon Sep 17 00:00:00 2001 From: Toke Stuart Jepsen Date: Tue, 31 Jan 2023 11:38:43 +0000 Subject: [PATCH 57/67] Improve docs --- .../maya/plugins/publish/reset_xgen_attributes.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/openpype/hosts/maya/plugins/publish/reset_xgen_attributes.py b/openpype/hosts/maya/plugins/publish/reset_xgen_attributes.py index 08f367f2d5..b90885663c 100644 --- a/openpype/hosts/maya/plugins/publish/reset_xgen_attributes.py +++ b/openpype/hosts/maya/plugins/publish/reset_xgen_attributes.py @@ -4,7 +4,11 @@ import pyblish.api class ResetXgenAttributes(pyblish.api.InstancePlugin): - """Reset Xgen attributes.""" + """Reset Xgen attributes. + + When the incremental save of the workfile triggers, the Xgen attributes + changes so this plugin will change it back to the values before publishing. + """ label = "Reset Xgen Attributes." # Offset to run after workfile increment plugin. @@ -13,7 +17,7 @@ class ResetXgenAttributes(pyblish.api.InstancePlugin): def process(self, instance): xgen_attributes = instance.data.get("xgenAttributes", {}) - if not xgen_attributes : + if not xgen_attributes: return for palette, data in xgen_attributes.items(): @@ -24,6 +28,9 @@ class ResetXgenAttributes(pyblish.api.InstancePlugin): ) cmds.setAttr(node_attr, value, type="string") cmds.setAttr(palette + ".xgExportAsDelta", True) - + + # Need to save the scene, cause the attribute changes above does not + # mark the scene as modified so user can exit without commiting the + # changes. self.log.info("Saving changes.") cmds.file(save=True) From 658ad2a0c2f34bdc14b4ac586ca88b08db4f8b57 Mon Sep 17 00:00:00 2001 From: Toke Jepsen Date: Tue, 31 Jan 2023 11:40:55 +0000 Subject: [PATCH 58/67] Update openpype/hosts/maya/plugins/load/load_xgen.py Co-authored-by: Roy Nieterau --- openpype/hosts/maya/plugins/load/load_xgen.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/openpype/hosts/maya/plugins/load/load_xgen.py b/openpype/hosts/maya/plugins/load/load_xgen.py index d0ed3e05b3..81a525fe61 100644 --- a/openpype/hosts/maya/plugins/load/load_xgen.py +++ b/openpype/hosts/maya/plugins/load/load_xgen.py @@ -149,8 +149,9 @@ class XgenLoader(openpype.hosts.maya.api.plugin.ReferenceLoader): new_xgen_file = maya_file.replace(extension, ".xgen") data_path = "" with open(new_xgen_file, "r") as f: - for line in [line.rstrip() for line in f]: + for line in f: if line.startswith("\txgDataPath"): + line = line.rstrip() data_path = line.split("\t")[-1] break From 67e438dcb90e9050c1d03a7de52489e678eebdf0 Mon Sep 17 00:00:00 2001 From: Toke Jepsen Date: Tue, 31 Jan 2023 11:56:55 +0000 Subject: [PATCH 59/67] Update openpype/hosts/maya/plugins/inventory/connect_geometry.py Co-authored-by: Roy Nieterau --- openpype/hosts/maya/plugins/inventory/connect_geometry.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/openpype/hosts/maya/plugins/inventory/connect_geometry.py b/openpype/hosts/maya/plugins/inventory/connect_geometry.py index 9fe4f9e195..bcfc577104 100644 --- a/openpype/hosts/maya/plugins/inventory/connect_geometry.py +++ b/openpype/hosts/maya/plugins/inventory/connect_geometry.py @@ -89,9 +89,7 @@ class ConnectGeometry(InventoryAction): return # Setup live worldspace blendshape connection. - for match in matches: - source = match[0] - target = match[1] + for source, target in matches: blendshape = cmds.blendShape(source, target)[0] cmds.setAttr(blendshape + ".origin", 0) cmds.setAttr(blendshape + "." + target.split(":")[-1], 1) From ab0e3fab01f150cc963579e921c5f9547b276060 Mon Sep 17 00:00:00 2001 From: Toke Stuart Jepsen Date: Tue, 31 Jan 2023 17:47:12 +0000 Subject: [PATCH 60/67] BigRoy feedback --- openpype/hosts/maya/api/lib.py | 28 +++- openpype/hosts/maya/api/plugin.py | 30 ---- .../plugins/inventory/connect_geometry.py | 32 +++- openpype/hosts/maya/plugins/load/load_xgen.py | 31 ++++ .../maya/plugins/publish/collect_xgen.py | 32 ++++ .../plugins/publish/extract_workfile_xgen.py | 97 +++++++---- .../maya/plugins/publish/extract_xgen.py | 158 ++++++++---------- .../maya/plugins/publish/validate_xgen.py | 18 +- 8 files changed, 255 insertions(+), 171 deletions(-) diff --git a/openpype/hosts/maya/api/lib.py b/openpype/hosts/maya/api/lib.py index 65f39270f5..e9956762f2 100644 --- a/openpype/hosts/maya/api/lib.py +++ b/openpype/hosts/maya/api/lib.py @@ -5,6 +5,7 @@ import sys import platform import uuid import math +import re import json import logging @@ -3449,14 +3450,31 @@ def get_attribute_input(attr): def write_xgen_file(data, filepath): + """Overwrites data in .xgen files. + + Quite naive approach to mainly overwrite "xgDataPath" and "xgProjectPath". + + Args: + data (dict): Dictionary of key, value. Key matches with xgen file. + For example: + {"xgDataPath": "some/path"} + filepath (string): Absolute path of .xgen file. + """ + # Generate regex lookup for line to key basically + # match any of the keys in `\t{key}\t\t` + keys = "|".join(re.escape(key) for key in data.keys()) + re_keys = re.compile("^\t({})\t\t".format(keys)) + lines = [] with open(filepath, "r") as f: - for line in [line.rstrip() for line in f]: - for key, value in data.items(): - if line.startswith("\t{}".format(key)): - line = "\t{}\t\t{}".format(key, value) + for line in f: + match = re_keys.match(line) + if match: + key = match.group(1) + value = data[key] + line = "\t{}\t\t{}\n".format(key, value) lines.append(line) with open(filepath, "w") as f: - f.write("\n".join(lines)) + f.writelines(lines) diff --git a/openpype/hosts/maya/api/plugin.py b/openpype/hosts/maya/api/plugin.py index b7adf6edfc..82df85a8be 100644 --- a/openpype/hosts/maya/api/plugin.py +++ b/openpype/hosts/maya/api/plugin.py @@ -300,36 +300,6 @@ class ReferenceLoader(Loader): str(representation["_id"]), type="string") - # Update any xgen containers. - compound_name = "xgenContainers" - if cmds.objExists("{}.{}".format(node, compound_name)): - import xgenm - container_amount = cmds.getAttr( - "{}.{}".format(node, compound_name), size=True - ) - # loop through all compound children - for i in range(container_amount): - attr = "{}.{}[{}].container".format(node, compound_name, i) - objectset = cmds.listConnections(attr)[0] - reference_node = cmds.sets(objectset, query=True)[0] - palettes = cmds.ls( - cmds.referenceQuery(reference_node, nodes=True), - type="xgmPalette" - ) - for palette in palettes: - for description in xgenm.descriptions(palette): - xgenm.setAttr( - "cacheFileName", - path.replace("\\", "/"), - palette, - description, - "SplinePrimitive" - ) - - # Refresh UI and viewport. - de = xgenm.xgGlobal.DescriptionEditor - de.refresh("Full") - def remove(self, container): """Remove an existing `container` from Maya scene diff --git a/openpype/hosts/maya/plugins/inventory/connect_geometry.py b/openpype/hosts/maya/plugins/inventory/connect_geometry.py index bcfc577104..a12487cf7e 100644 --- a/openpype/hosts/maya/plugins/inventory/connect_geometry.py +++ b/openpype/hosts/maya/plugins/inventory/connect_geometry.py @@ -1,6 +1,7 @@ from maya import cmds from openpype.pipeline import InventoryAction, get_representation_context +from openpype.hosts.maya.api.lib import get_id class ConnectGeometry(InventoryAction): @@ -64,12 +65,12 @@ class ConnectGeometry(InventoryAction): source_data = self.get_container_data(source_object) matches = [] - node_types = [] + node_types = set() for target_container in target_containers: target_data = self.get_container_data( target_container["objectName"] ) - node_types.extend(target_data["node_types"]) + node_types.update(target_data["node_types"]) for id, transform in target_data["ids"].items(): source_match = source_data["ids"].get(id) if source_match: @@ -99,15 +100,30 @@ class ConnectGeometry(InventoryAction): cmds.xgmPreview() def get_container_data(self, container): - data = {"node_types": [], "ids": {}} + """Collects data about the container nodes. + + Args: + container (dict): Container instance. + + Returns: + data (dict): + "node_types": All node types in container nodes. + "ids": If the node is a mesh, we collect its parent transform + id. + """ + data = {"node_types": set(), "ids": {}} ref_node = cmds.sets(container, query=True, nodesOnly=True)[0] for node in cmds.referenceQuery(ref_node, nodes=True): node_type = cmds.nodeType(node) - data["node_types"].append(node_type) - if node_type == "mesh": - transform = cmds.listRelatives(node, parent=True)[0] - id = cmds.getAttr(transform + ".cbId") - data["ids"][id] = transform + data["node_types"].add(node_type) + + # Only interested in mesh transforms for connecting geometry with + # blendshape. + if node_type != "mesh": + continue + + transform = cmds.listRelatives(node, parent=True)[0] + data["ids"][get_id(transform)] = transform return data diff --git a/openpype/hosts/maya/plugins/load/load_xgen.py b/openpype/hosts/maya/plugins/load/load_xgen.py index 81a525fe61..5110d4ca05 100644 --- a/openpype/hosts/maya/plugins/load/load_xgen.py +++ b/openpype/hosts/maya/plugins/load/load_xgen.py @@ -173,3 +173,34 @@ class XgenLoader(openpype.hosts.maya.api.plugin.ReferenceLoader): super().update(container, representation) xgenm.applyDelta(xgen_palette.replace("|", ""), xgd_file) + + # Update any xgen containers. + compound_name = "xgenContainers" + import xgenm + container_amount = cmds.getAttr( + "{}.{}".format(container_node, compound_name), size=True + ) + # loop through all compound children + for i in range(container_amount): + attr = "{}.{}[{}].container".format( + container_node, compound_name, i + ) + objectset = cmds.listConnections(attr)[0] + reference_node = cmds.sets(objectset, query=True)[0] + palettes = cmds.ls( + cmds.referenceQuery(reference_node, nodes=True), + type="xgmPalette" + ) + for palette in palettes: + for description in xgenm.descriptions(palette): + xgenm.setAttr( + "cacheFileName", + maya_file.replace("\\", "/"), + palette, + description, + "SplinePrimitive" + ) + + # Refresh UI and viewport. + de = xgenm.xgGlobal.DescriptionEditor + de.refresh("Full") diff --git a/openpype/hosts/maya/plugins/publish/collect_xgen.py b/openpype/hosts/maya/plugins/publish/collect_xgen.py index 5a48b1d221..da0549b2d8 100644 --- a/openpype/hosts/maya/plugins/publish/collect_xgen.py +++ b/openpype/hosts/maya/plugins/publish/collect_xgen.py @@ -1,3 +1,5 @@ +import os + from maya import cmds import pyblish.api @@ -35,5 +37,35 @@ class CollectXgen(pyblish.api.InstancePlugin): input = get_attribute_input("{}.{}".format(node, attr)) data["xgenConnections"][node][attr] = input + # Collect all files under palette root as resources. + import xgenm + + data_path = xgenm.getAttr( + "xgDataPath", data["xgmPalette"].replace("|", "") + ).split(os.pathsep)[0] + data_path = data_path.replace( + "${PROJECT}", + xgenm.getAttr("xgProjectPath", data["xgmPalette"].replace("|", "")) + ) + transfers = [] + + # Since we are duplicating this palette when extracting we predict that + # the name will be the basename without namespaces. + predicted_palette_name = data["xgmPalette"].split(":")[-1] + predicted_palette_name = predicted_palette_name.replace("|", "") + + for root, _, files in os.walk(data_path): + for file in files: + source = os.path.join(root, file).replace("\\", "/") + destination = os.path.join( + instance.data["resourcesDir"], + "collections", + predicted_palette_name, + source.replace(data_path, "")[1:] + ) + transfers.append((source, destination.replace("\\", "/"))) + + data["transfers"] = transfers + self.log.info(data) instance.data.update(data) diff --git a/openpype/hosts/maya/plugins/publish/extract_workfile_xgen.py b/openpype/hosts/maya/plugins/publish/extract_workfile_xgen.py index 5847563e5b..fe28427ae7 100644 --- a/openpype/hosts/maya/plugins/publish/extract_workfile_xgen.py +++ b/openpype/hosts/maya/plugins/publish/extract_workfile_xgen.py @@ -5,14 +5,16 @@ import copy from maya import cmds import pyblish.api -from openpype.hosts.maya.api import current_file from openpype.hosts.maya.api.lib import extract_alembic from openpype.pipeline import publish from openpype.lib import StringTemplate class ExtractWorkfileXgen(publish.Extractor): - """Extract Workfile Xgen.""" + """Extract Workfile Xgen. + + When submitting a render, we need to prep Xgen side car files. + """ # Offset to run before workfile scene save. order = pyblish.api.ExtractorOrder - 0.499 @@ -20,13 +22,65 @@ class ExtractWorkfileXgen(publish.Extractor): families = ["workfile"] hosts = ["maya"] + def get_render_max_frame_range(self, context): + """Return start to end frame range including all renderlayers in + context. + + This will return the full frame range which includes all frames of the + renderlayer instances to be published/submitted. + + Args: + context (pyblish.api.Context): Current publishing context. + + Returns: + tuple or None: Start frame, end frame tuple if any renderlayers + found. Otherwise None is returned. + + """ + + def _is_active_renderlayer(i): + """Return whether instance is active renderlayer""" + if not i.data.get("publish", True): + return False + + is_renderlayer = ( + "renderlayer" in i.data.get("families", []) or + i.data["family"] == "renderlayer" + ) + return is_renderlayer + + start_frame = None + end_frame = None + for instance in context: + if not _is_active_renderlayer(instance): + # Only consider renderlyare instances + continue + + render_start_frame = instance.data["frameStart"] + render_end_frame = instance.data["frameStart"] + + if start_frame is None: + start_frame = render_start_frame + else: + start_frame = min(start_frame, render_start_frame) + + if end_frame is None: + end_frame = render_end_frame + else: + end_frame = max(end_frame, render_end_frame) + + if start_frame is None or end_frame is None: + return + + return start_frame, end_frame + def process(self, instance): transfers = [] # Validate there is any palettes in the scene. if not cmds.ls(type="xgmPalette"): self.log.debug( - "No collections found in the scene. Abort Xgen extraction." + "No collections found in the scene. Skipping Xgen extraction." ) return else: @@ -34,44 +88,24 @@ class ExtractWorkfileXgen(publish.Extractor): # Validate to extract only when we are publishing a renderlayer as # well. - renderlayer = False - start_frame = None - end_frame = None - for i in instance.context: - is_renderlayer = ( - "renderlayer" in i.data.get("families", []) or - i.data["family"] == "renderlayer" - ) - if is_renderlayer and i.data["publish"]: - renderlayer = True - - if start_frame is None: - start_frame = i.data["frameStart"] - if end_frame is None: - end_frame = i.data["frameEnd"] - - if i.data["frameStart"] < start_frame: - start_frame = i.data["frameStart"] - if i.data["frameEnd"] > end_frame: - end_frame = i.data["frameEnd"] - - break - - if not renderlayer: + render_range = self.get_render_max_frame_range(instance.context) + if not render_range: self.log.debug( - "No publishable renderlayers found in context. Abort Xgen" + "No publishable renderlayers found in context. Skipping Xgen" " extraction." ) return + start_frame, end_frame = render_range + # We decrement start frame and increment end frame so motion blur will # render correctly. start_frame -= 1 end_frame += 1 # Extract patches alembic. - basename, _ = os.path.splitext(current_file()) - dirname = os.path.dirname(current_file()) + basename, _ = os.path.splitext(instance.context.data["currentFile"]) + dirname = os.path.dirname(instance.context.data["currentFile"]) kwargs = {"attrPrefix": ["xgen"], "stripNamespaces": True} alembic_files = [] for palette in cmds.ls(type="xgmPalette"): @@ -121,8 +155,7 @@ class ExtractWorkfileXgen(publish.Extractor): # Collect Xgen and Delta files. xgen_files = [] sources = [] - file_path = current_file() - current_dir = os.path.dirname(file_path) + current_dir = os.path.dirname(instance.context.data["currentFile"]) attrs = ["xgFileName", "xgBaseFile"] for palette in cmds.ls(type="xgmPalette"): for attr in attrs: diff --git a/openpype/hosts/maya/plugins/publish/extract_xgen.py b/openpype/hosts/maya/plugins/publish/extract_xgen.py index 9549aba76d..7428fab53f 100644 --- a/openpype/hosts/maya/plugins/publish/extract_xgen.py +++ b/openpype/hosts/maya/plugins/publish/extract_xgen.py @@ -7,7 +7,7 @@ import xgenm from openpype.pipeline import publish from openpype.hosts.maya.api.lib import ( - maintained_selection, attribute_values, write_xgen_file + maintained_selection, attribute_values, write_xgen_file, delete_after ) from openpype.lib import StringTemplate @@ -36,48 +36,81 @@ class ExtractXgenCache(publish.Extractor): maya_filename = "{}.{}".format(instance.data["name"], self.scene_type) maya_filepath = os.path.join(staging_dir, maya_filename) - # Collect nodes to export. - duplicate_nodes = [] - for node, connections in instance.data["xgenConnections"].items(): - transform_name = connections["transform"].split(".")[0] - - # Duplicate_transform subd patch geometry. - duplicate_transform = cmds.duplicate(transform_name)[0] - - # Discard the children. - shapes = cmds.listRelatives(duplicate_transform, shapes=True) - children = cmds.listRelatives(duplicate_transform, children=True) - cmds.delete(set(children) - set(shapes)) - - duplicate_transform = cmds.parent( - duplicate_transform, world=True - )[0] - - duplicate_nodes.append(duplicate_transform) - - # Export temp xgen palette files. - temp_xgen_path = os.path.join( - tempfile.gettempdir(), "temp.xgen" - ).replace("\\", "/") - xgenm.exportPalette( - instance.data["xgmPalette"].replace("|", ""), temp_xgen_path - ) - self.log.info("Extracted to {}".format(temp_xgen_path)) - - # Import xgen onto the duplicate. - with maintained_selection(): - cmds.select(duplicate_nodes) - palette = xgenm.importPalette(temp_xgen_path, []) - # Get published xgen file name. template_data = copy.deepcopy(instance.data["anatomyData"]) template_data.update({"ext": "xgen"}) templates = instance.context.data["anatomy"].templates["publish"] xgen_filename = StringTemplate(templates["file"]).format(template_data) - # Export duplicated palette. - xgen_path = os.path.join(staging_dir, xgen_filename).replace("\\", "/") - xgenm.exportPalette(palette, xgen_path) + xgen_path = os.path.join( + self.staging_dir(instance), xgen_filename + ).replace("\\", "/") + type = "mayaAscii" if self.scene_type == "ma" else "mayaBinary" + + # Duplicate xgen setup. + with delete_after() as delete_bin: + duplicate_nodes = [] + # Collect nodes to export. + for node, connections in instance.data["xgenConnections"].items(): + transform_name = connections["transform"].split(".")[0] + + # Duplicate_transform subd patch geometry. + duplicate_transform = cmds.duplicate(transform_name)[0] + delete_bin.append(duplicate_transform) + + # Discard the children. + shapes = cmds.listRelatives(duplicate_transform, shapes=True) + children = cmds.listRelatives( + duplicate_transform, children=True + ) + cmds.delete(set(children) - set(shapes)) + + duplicate_transform = cmds.parent( + duplicate_transform, world=True + )[0] + + duplicate_nodes.append(duplicate_transform) + + # Export temp xgen palette files. + temp_xgen_path = os.path.join( + tempfile.gettempdir(), "temp.xgen" + ).replace("\\", "/") + xgenm.exportPalette( + instance.data["xgmPalette"].replace("|", ""), temp_xgen_path + ) + self.log.info("Extracted to {}".format(temp_xgen_path)) + + # Import xgen onto the duplicate. + with maintained_selection(): + cmds.select(duplicate_nodes) + palette = xgenm.importPalette(temp_xgen_path, []) + + delete_bin.append(palette) + + # Export duplicated palettes. + xgenm.exportPalette(palette, xgen_path) + + # Export Maya file. + attribute_data = {"{}.xgFileName".format(palette): xgen_filename} + with attribute_values(attribute_data): + with maintained_selection(): + cmds.select(duplicate_nodes + [palette]) + cmds.file( + maya_filepath, + force=True, + type=type, + exportSelected=True, + preserveReferences=False, + constructionHistory=True, + shader=True, + constraints=True, + expressions=True + ) + + self.log.info("Extracted to {}".format(maya_filepath)) + + if os.path.exists(temp_xgen_path): + os.remove(temp_xgen_path) data = { "xgDataPath": os.path.join( @@ -91,6 +124,7 @@ class ExtractXgenCache(publish.Extractor): } write_xgen_file(data, xgen_path) + # Adding representations. representation = { "name": "xgen", "ext": "xgen", @@ -99,28 +133,6 @@ class ExtractXgenCache(publish.Extractor): } instance.data["representations"].append(representation) - # Export Maya file. - type = "mayaAscii" if self.scene_type == "ma" else "mayaBinary" - attribute_data = { - "{}.xgFileName".format(palette): xgen_filename - } - with attribute_values(attribute_data): - with maintained_selection(): - cmds.select(duplicate_nodes + [palette]) - cmds.file( - maya_filepath, - force=True, - type=type, - exportSelected=True, - preserveReferences=False, - constructionHistory=True, - shader=True, - constraints=True, - expressions=True - ) - - self.log.info("Extracted to {}".format(maya_filepath)) - representation = { "name": self.scene_type, "ext": self.scene_type, @@ -128,31 +140,3 @@ class ExtractXgenCache(publish.Extractor): "stagingDir": staging_dir } instance.data["representations"].append(representation) - - # Clean up. - cmds.delete(duplicate_nodes + [palette]) - os.remove(temp_xgen_path) - - # Collect all files under palette root as resources. - data_path = xgenm.getAttr( - "xgDataPath", instance.data["xgmPalette"].replace("|", "") - ).split(os.pathsep)[0] - data_path = data_path.replace( - "${PROJECT}", - xgenm.getAttr( - "xgProjectPath", instance.data["xgmPalette"].replace("|", "") - ) - ) - transfers = [] - for root, _, files in os.walk(data_path): - for file in files: - source = os.path.join(root, file).replace("\\", "/") - destination = os.path.join( - instance.data["resourcesDir"], - "collections", - palette, - source.replace(data_path, "")[1:] - ) - transfers.append((source, destination.replace("\\", "/"))) - - instance.data["transfers"] = transfers diff --git a/openpype/hosts/maya/plugins/publish/validate_xgen.py b/openpype/hosts/maya/plugins/publish/validate_xgen.py index 19cf612848..2870909974 100644 --- a/openpype/hosts/maya/plugins/publish/validate_xgen.py +++ b/openpype/hosts/maya/plugins/publish/validate_xgen.py @@ -4,7 +4,7 @@ import maya.cmds as cmds import xgenm import pyblish.api -from openpype.pipeline.publish import KnownPublishError +from openpype.pipeline.publish import PublishValidationError class ValidateXgen(pyblish.api.InstancePlugin): @@ -20,7 +20,7 @@ class ValidateXgen(pyblish.api.InstancePlugin): # Only 1 collection/node per instance. if len(set_members) != 1: - raise KnownPublishError( + raise PublishValidationError( "Only one collection per instance is allowed." " Found:\n{}".format(set_members) ) @@ -28,7 +28,7 @@ class ValidateXgen(pyblish.api.InstancePlugin): # Only xgen palette node is allowed. node_type = cmds.nodeType(set_members[0]) if node_type != "xgmPalette": - raise KnownPublishError( + raise PublishValidationError( "Only node of type \"xgmPalette\" are allowed. Referred to as" " \"collection\" in the Maya UI." " Node type found: {}".format(node_type) @@ -50,10 +50,10 @@ class ValidateXgen(pyblish.api.InstancePlugin): except KeyError: inactive_modifiers[description] = [name] - msg = ( - "There are inactive modifiers on the collection. " - "Please delete these:\n{}".format( - json.dumps(inactive_modifiers, indent=4, sort_keys=True) + if inactive_modifiers: + raise PublishValidationError( + "There are inactive modifiers on the collection. " + "Please delete these:\n{}".format( + json.dumps(inactive_modifiers, indent=4, sort_keys=True) + ) ) - ) - assert not inactive_modifiers, msg From caf26b8ab671ee90977a78fdf9485381b1ae1f68 Mon Sep 17 00:00:00 2001 From: Toke Stuart Jepsen Date: Tue, 31 Jan 2023 17:49:10 +0000 Subject: [PATCH 61/67] Hound --- openpype/hosts/maya/plugins/publish/extract_xgen.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openpype/hosts/maya/plugins/publish/extract_xgen.py b/openpype/hosts/maya/plugins/publish/extract_xgen.py index 7428fab53f..0719be3a1e 100644 --- a/openpype/hosts/maya/plugins/publish/extract_xgen.py +++ b/openpype/hosts/maya/plugins/publish/extract_xgen.py @@ -51,7 +51,7 @@ class ExtractXgenCache(publish.Extractor): with delete_after() as delete_bin: duplicate_nodes = [] # Collect nodes to export. - for node, connections in instance.data["xgenConnections"].items(): + for _, connections in instance.data["xgenConnections"].items(): transform_name = connections["transform"].split(".")[0] # Duplicate_transform subd patch geometry. From 51ad73c7b3f8b3376693fd946cefb38d716ad180 Mon Sep 17 00:00:00 2001 From: Toke Stuart Jepsen Date: Wed, 1 Feb 2023 09:12:47 +0000 Subject: [PATCH 62/67] BigRoy feedback --- openpype/hosts/maya/plugins/publish/extract_workfile_xgen.py | 4 ++-- website/docs/artist_hosts_maya_xgen.md | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/openpype/hosts/maya/plugins/publish/extract_workfile_xgen.py b/openpype/hosts/maya/plugins/publish/extract_workfile_xgen.py index fe28427ae7..49d724960f 100644 --- a/openpype/hosts/maya/plugins/publish/extract_workfile_xgen.py +++ b/openpype/hosts/maya/plugins/publish/extract_workfile_xgen.py @@ -83,8 +83,8 @@ class ExtractWorkfileXgen(publish.Extractor): "No collections found in the scene. Skipping Xgen extraction." ) return - else: - import xgenm + + import xgenm # Validate to extract only when we are publishing a renderlayer as # well. diff --git a/website/docs/artist_hosts_maya_xgen.md b/website/docs/artist_hosts_maya_xgen.md index 191826f49c..ec5f2ed921 100644 --- a/website/docs/artist_hosts_maya_xgen.md +++ b/website/docs/artist_hosts_maya_xgen.md @@ -4,6 +4,8 @@ title: Xgen for Maya sidebar_label: Xgen --- +OpenPype supports Xgen classic with the follow workflow. It eases the otherwise cumbersome issues around Xgen's side car files and hidden behaviour inside Maya. The workflow supports publishing, loading and updating of Xgen collections, along with connecting animation from geometry and (guide) curves. + ## Setup ### Settings From 981d454802dded37955a2c8c573c24173a5d5957 Mon Sep 17 00:00:00 2001 From: Toke Stuart Jepsen Date: Wed, 1 Feb 2023 11:57:12 +0000 Subject: [PATCH 63/67] Remove old xgen cache plugin --- .../maya/plugins/publish/extract_xgen.py | 2 +- .../plugins/publish/extract_xgen_cache.py | 64 ------------------- 2 files changed, 1 insertion(+), 65 deletions(-) delete mode 100644 openpype/hosts/maya/plugins/publish/extract_xgen_cache.py diff --git a/openpype/hosts/maya/plugins/publish/extract_xgen.py b/openpype/hosts/maya/plugins/publish/extract_xgen.py index 0719be3a1e..0cc842b4ec 100644 --- a/openpype/hosts/maya/plugins/publish/extract_xgen.py +++ b/openpype/hosts/maya/plugins/publish/extract_xgen.py @@ -12,7 +12,7 @@ from openpype.hosts.maya.api.lib import ( from openpype.lib import StringTemplate -class ExtractXgenCache(publish.Extractor): +class ExtractXgen(publish.Extractor): """Extract Xgen Workflow: diff --git a/openpype/hosts/maya/plugins/publish/extract_xgen_cache.py b/openpype/hosts/maya/plugins/publish/extract_xgen_cache.py deleted file mode 100644 index 77350f343e..0000000000 --- a/openpype/hosts/maya/plugins/publish/extract_xgen_cache.py +++ /dev/null @@ -1,64 +0,0 @@ -import os - -from maya import cmds - -from openpype.pipeline import publish -from openpype.hosts.maya.api.lib import ( - suspended_refresh, - maintained_selection -) - - -class ExtractXgenCache(publish.Extractor): - """Produce an alembic of just xgen interactive groom - - """ - - label = "Extract Xgen ABC Cache" - hosts = ["maya"] - families = ["xgen"] - optional = True - - def process(self, instance): - - # Collect the out set nodes - out_descriptions = [node for node in instance - if cmds.nodeType(node) == "xgmSplineDescription"] - - start = 1 - end = 1 - - self.log.info("Extracting Xgen Cache..") - dirname = self.staging_dir(instance) - - parent_dir = self.staging_dir(instance) - filename = "{name}.abc".format(**instance.data) - path = os.path.join(parent_dir, filename) - - with suspended_refresh(): - with maintained_selection(): - command = ( - '-file ' - + path - + ' -df "ogawa" -fr ' - + str(start) - + ' ' - + str(end) - + ' -step 1 -mxf -wfw' - ) - for desc in out_descriptions: - command += (" -obj " + desc) - cmds.xgmSplineCache(export=True, j=command) - - if "representations" not in instance.data: - instance.data["representations"] = [] - - representation = { - 'name': 'abc', - 'ext': 'abc', - 'files': filename, - "stagingDir": dirname, - } - instance.data["representations"].append(representation) - - self.log.info("Extracted {} to {}".format(instance, dirname)) From c71fc217da17da5e93b7129240ffc6e418d7cd12 Mon Sep 17 00:00:00 2001 From: Toke Stuart Jepsen Date: Wed, 1 Feb 2023 15:05:15 +0000 Subject: [PATCH 64/67] BigRoy feedback --- .../maya/plugins/publish/extract_workfile_xgen.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/openpype/hosts/maya/plugins/publish/extract_workfile_xgen.py b/openpype/hosts/maya/plugins/publish/extract_workfile_xgen.py index 49d724960f..b95add3306 100644 --- a/openpype/hosts/maya/plugins/publish/extract_workfile_xgen.py +++ b/openpype/hosts/maya/plugins/publish/extract_workfile_xgen.py @@ -104,7 +104,7 @@ class ExtractWorkfileXgen(publish.Extractor): end_frame += 1 # Extract patches alembic. - basename, _ = os.path.splitext(instance.context.data["currentFile"]) + path_no_ext, _ = os.path.splitext(instance.context.data["currentFile"]) dirname = os.path.dirname(instance.context.data["currentFile"]) kwargs = {"attrPrefix": ["xgen"], "stripNamespaces": True} alembic_files = [] @@ -116,7 +116,9 @@ class ExtractWorkfileXgen(publish.Extractor): alembic_file = os.path.join( dirname, - "{}__{}.abc".format(basename, palette.replace(":", "__ns__")) + "{}__{}.abc".format( + path_no_ext, palette.replace(":", "__ns__") + ) ) extract_alembic( alembic_file, @@ -138,7 +140,9 @@ class ExtractWorkfileXgen(publish.Extractor): for source in alembic_files: destination = os.path.join( os.path.dirname(instance.data["resourcesDir"]), - os.path.basename(source.replace(basename, published_basename)) + os.path.basename( + source.replace(path_no_ext, published_basename) + ) ) transfers.append((source, destination)) From 7a7102337bfe73ab7d7e8e805e681a7ec743fd40 Mon Sep 17 00:00:00 2001 From: Toke Stuart Jepsen Date: Wed, 1 Feb 2023 15:19:38 +0000 Subject: [PATCH 65/67] BigRoy feedback --- .../hosts/maya/plugins/publish/extract_workfile_xgen.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/openpype/hosts/maya/plugins/publish/extract_workfile_xgen.py b/openpype/hosts/maya/plugins/publish/extract_workfile_xgen.py index b95add3306..c8d0d63344 100644 --- a/openpype/hosts/maya/plugins/publish/extract_workfile_xgen.py +++ b/openpype/hosts/maya/plugins/publish/extract_workfile_xgen.py @@ -105,7 +105,6 @@ class ExtractWorkfileXgen(publish.Extractor): # Extract patches alembic. path_no_ext, _ = os.path.splitext(instance.context.data["currentFile"]) - dirname = os.path.dirname(instance.context.data["currentFile"]) kwargs = {"attrPrefix": ["xgen"], "stripNamespaces": True} alembic_files = [] for palette in cmds.ls(type="xgmPalette"): @@ -114,11 +113,8 @@ class ExtractWorkfileXgen(publish.Extractor): for name in xgenm.boundGeometry(palette, description): patch_names.append(name) - alembic_file = os.path.join( - dirname, - "{}__{}.abc".format( - path_no_ext, palette.replace(":", "__ns__") - ) + alembic_file = "{}__{}.abc".format( + path_no_ext, palette.replace(":", "__ns__") ) extract_alembic( alembic_file, From e0f18363cd2017fecab4dae2eceb5f1029c15f21 Mon Sep 17 00:00:00 2001 From: Toke Stuart Jepsen Date: Wed, 1 Feb 2023 20:57:47 +0000 Subject: [PATCH 66/67] Fix file paths. --- openpype/hosts/maya/plugins/load/load_xgen.py | 6 ++---- .../hosts/maya/plugins/publish/extract_workfile_xgen.py | 2 +- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/openpype/hosts/maya/plugins/load/load_xgen.py b/openpype/hosts/maya/plugins/load/load_xgen.py index 5110d4ca05..fc86596208 100644 --- a/openpype/hosts/maya/plugins/load/load_xgen.py +++ b/openpype/hosts/maya/plugins/load/load_xgen.py @@ -73,9 +73,8 @@ class XgenLoader(openpype.hosts.maya.api.plugin.ReferenceLoader): # to ensure paths are setup correctly. project_path = os.path.dirname(current_file()).replace("\\", "/") xgenm.setAttr("xgProjectPath", project_path, xgen_palette) - data_path = "${{PROJECT}}xgen/collections/{}{}{}".format( + data_path = "${{PROJECT}}xgen/collections/{};{}".format( xgen_palette.replace(":", "__ns__"), - os.pathsep, xgenm.getAttr("xgDataPath", xgen_palette) ) xgenm.setAttr("xgDataPath", data_path, xgen_palette) @@ -156,9 +155,8 @@ class XgenLoader(openpype.hosts.maya.api.plugin.ReferenceLoader): break project_path = os.path.dirname(current_file()).replace("\\", "/") - data_path = "${{PROJECT}}xgen/collections/{}{}{}".format( + data_path = "${{PROJECT}}xgen/collections/{};{}".format( xgen_palette.replace(":", "__ns__"), - os.pathsep, data_path ) data = {"xgProjectPath": project_path, "xgDataPath": data_path} diff --git a/openpype/hosts/maya/plugins/publish/extract_workfile_xgen.py b/openpype/hosts/maya/plugins/publish/extract_workfile_xgen.py index c8d0d63344..20e1bd37d8 100644 --- a/openpype/hosts/maya/plugins/publish/extract_workfile_xgen.py +++ b/openpype/hosts/maya/plugins/publish/extract_workfile_xgen.py @@ -208,7 +208,7 @@ class ExtractWorkfileXgen(publish.Extractor): project_path = xgenm.getAttr("xgProjectPath", palette) data_path = xgenm.getAttr("xgDataPath", palette) data_path = data_path.replace("${PROJECT}", project_path) - for path in data_path.split(os.pathsep): + for path in data_path.split(";"): for root, _, files in os.walk(path): for f in files: source = os.path.join(root, f) From 926f23d39275ece8fbdd04b97541cf874e9b22cd Mon Sep 17 00:00:00 2001 From: Toke Stuart Jepsen Date: Wed, 1 Feb 2023 20:57:53 +0000 Subject: [PATCH 67/67] Fix updating --- openpype/hosts/maya/api/plugin.py | 33 +++++++++++++++++++ openpype/hosts/maya/plugins/load/load_xgen.py | 31 ----------------- 2 files changed, 33 insertions(+), 31 deletions(-) diff --git a/openpype/hosts/maya/api/plugin.py b/openpype/hosts/maya/api/plugin.py index 82df85a8be..916fddd923 100644 --- a/openpype/hosts/maya/api/plugin.py +++ b/openpype/hosts/maya/api/plugin.py @@ -300,6 +300,39 @@ class ReferenceLoader(Loader): str(representation["_id"]), type="string") + # When an animation or pointcache gets connected to an Xgen container, + # the compound attribute "xgenContainers" gets created. When animation + # containers gets updated we also need to update the cacheFileName on + # the Xgen collection. + compound_name = "xgenContainers" + if cmds.objExists("{}.{}".format(node, compound_name)): + import xgenm + container_amount = cmds.getAttr( + "{}.{}".format(node, compound_name), size=True + ) + # loop through all compound children + for i in range(container_amount): + attr = "{}.{}[{}].container".format(node, compound_name, i) + objectset = cmds.listConnections(attr)[0] + reference_node = cmds.sets(objectset, query=True)[0] + palettes = cmds.ls( + cmds.referenceQuery(reference_node, nodes=True), + type="xgmPalette" + ) + for palette in palettes: + for description in xgenm.descriptions(palette): + xgenm.setAttr( + "cacheFileName", + path.replace("\\", "/"), + palette, + description, + "SplinePrimitive" + ) + + # Refresh UI and viewport. + de = xgenm.xgGlobal.DescriptionEditor + de.refresh("Full") + def remove(self, container): """Remove an existing `container` from Maya scene diff --git a/openpype/hosts/maya/plugins/load/load_xgen.py b/openpype/hosts/maya/plugins/load/load_xgen.py index fc86596208..1600cd49bd 100644 --- a/openpype/hosts/maya/plugins/load/load_xgen.py +++ b/openpype/hosts/maya/plugins/load/load_xgen.py @@ -171,34 +171,3 @@ class XgenLoader(openpype.hosts.maya.api.plugin.ReferenceLoader): super().update(container, representation) xgenm.applyDelta(xgen_palette.replace("|", ""), xgd_file) - - # Update any xgen containers. - compound_name = "xgenContainers" - import xgenm - container_amount = cmds.getAttr( - "{}.{}".format(container_node, compound_name), size=True - ) - # loop through all compound children - for i in range(container_amount): - attr = "{}.{}[{}].container".format( - container_node, compound_name, i - ) - objectset = cmds.listConnections(attr)[0] - reference_node = cmds.sets(objectset, query=True)[0] - palettes = cmds.ls( - cmds.referenceQuery(reference_node, nodes=True), - type="xgmPalette" - ) - for palette in palettes: - for description in xgenm.descriptions(palette): - xgenm.setAttr( - "cacheFileName", - maya_file.replace("\\", "/"), - palette, - description, - "SplinePrimitive" - ) - - # Refresh UI and viewport. - de = xgenm.xgGlobal.DescriptionEditor - de.refresh("Full")