From 3021fea4762ec84e53091f1cb954883be6a53634 Mon Sep 17 00:00:00 2001 From: Toke Stuart Jepsen Date: Wed, 11 Jan 2023 10:04:17 +0000 Subject: [PATCH] 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')