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(