From 042769c32581a4ef364a754dec52e29835e26f54 Mon Sep 17 00:00:00 2001 From: Jeremy Date: Tue, 26 Jul 2016 19:01:03 +0800 Subject: [PATCH] Fix can't get name MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix can't get the correct “name”, so it always use filename... --- hooks/secondary_publish_tk-maya.py | 67 +++++++++++++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-) diff --git a/hooks/secondary_publish_tk-maya.py b/hooks/secondary_publish_tk-maya.py index eae6915..ed83fa9 100644 --- a/hooks/secondary_publish_tk-maya.py +++ b/hooks/secondary_publish_tk-maya.py @@ -170,7 +170,7 @@ def __publish_alembic_cache(self, item, output, work_template, primary_publish_p self.parent.ensure_folder_exists(publish_folder) # determine the publish name: - publish_name = fields.get("name") + publish_name = self._get_publish_name(publish_path, publish_template, fields) if not publish_name: publish_name = os.path.basename(publish_path) @@ -223,6 +223,71 @@ def __publish_alembic_cache(self, item, output, work_template, primary_publish_p } tank.util.register_publish(**args) + def _get_publish_name(self, path, template, fields=None): + """ + Return the 'name' to be used for the file - if possible + this will return a 'versionless' name + """ + # first, extract the fields from the path using the template: + fields = fields.copy() if fields else template.get_fields(path) + if "name" in fields and fields["name"]: + # well, that was easy! + name = fields["name"] + else: + # find out if version is used in the file name: + template_name, _ = os.path.splitext(os.path.basename(template.definition)) + version_in_name = "{version}" in template_name + + # extract the file name from the path: + name, _ = os.path.splitext(os.path.basename(path)) + delims_str = "_-. " + if version_in_name: + # looks like version is part of the file name so we + # need to isolate it so that we can remove it safely. + # First, find a dummy version whose string representation + # doesn't exist in the name string + version_key = template.keys["version"] + dummy_version = 9876 + while True: + test_str = version_key.str_from_value(dummy_version) + if test_str not in name: + break + dummy_version += 1 + + # now use this dummy version and rebuild the path + fields["version"] = dummy_version + path = template.apply_fields(fields) + name, _ = os.path.splitext(os.path.basename(path)) + + # we can now locate the version in the name and remove it + dummy_version_str = version_key.str_from_value(dummy_version) + + v_pos = name.find(dummy_version_str) + # remove any preceeding 'v' + pre_v_str = name[:v_pos].rstrip("v") + post_v_str = name[v_pos + len(dummy_version_str):] + + if (pre_v_str and post_v_str + and pre_v_str[-1] in delims_str + and post_v_str[0] in delims_str): + # only want one delimiter - strip the second one: + post_v_str = post_v_str.lstrip(delims_str) + + versionless_name = pre_v_str + post_v_str + versionless_name = versionless_name.strip(delims_str) + + if versionless_name: + # great - lets use this! + name = versionless_name + else: + # likely that version is only thing in the name so + # instead, replace the dummy version with #'s: + zero_version_str = version_key.str_from_value(0) + new_version_str = "#" * len(zero_version_str) + name = name.replace(dummy_version_str, new_version_str) + + return name + def _find_scene_animation_range(self): """ Find the animation range from the current scene.