Working destructive updating

This commit is contained in:
Toke Stuart Jepsen 2022-12-20 07:25:00 +00:00
parent a1890ee04d
commit 2a7d05e090
2 changed files with 60 additions and 16 deletions

View file

@ -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)

View file

@ -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):