From 1d0e55aa833d99180b99cbbd718954933c5103b2 Mon Sep 17 00:00:00 2001 From: Jakub Jezek Date: Wed, 4 Oct 2023 13:00:42 +0200 Subject: [PATCH] improving colorspace categorization also adding new abstract function for returning all settings options nicely labelled --- .../plugins/create/create_colorspace_look.py | 13 ++-- .../publish/collect_explicit_colorspace.py | 29 ++----- openpype/pipeline/colorspace.py | 77 ++++++++++++++++++- openpype/scripts/ocio_wrapper.py | 33 ++++---- 4 files changed, 101 insertions(+), 51 deletions(-) diff --git a/openpype/hosts/traypublisher/plugins/create/create_colorspace_look.py b/openpype/hosts/traypublisher/plugins/create/create_colorspace_look.py index 62ecc391f6..3f3fa5348a 100644 --- a/openpype/hosts/traypublisher/plugins/create/create_colorspace_look.py +++ b/openpype/hosts/traypublisher/plugins/create/create_colorspace_look.py @@ -148,13 +148,12 @@ This creator publishes color space look file (LUT). if config_data: filepath = config_data["path"] - config_items = colorspace.get_ocio_config_colorspaces(filepath) - - self.colorspace_items.extend(( - (name, f"{name} [{data_['type']}]") - for name, data_ in config_items.items() - if data_.get("type") == "colorspace" - )) + labeled_colorspaces = colorspace.get_labeled_colorspaces( + filepath, + include_aliases=True, + include_roles=True + ) + self.colorspace_items.extend(labeled_colorspaces) self.enabled = True def _get_subset(self, asset_doc, variant, project_name, task_name=None): diff --git a/openpype/hosts/traypublisher/plugins/publish/collect_explicit_colorspace.py b/openpype/hosts/traypublisher/plugins/publish/collect_explicit_colorspace.py index 08479b8363..06ceac5923 100644 --- a/openpype/hosts/traypublisher/plugins/publish/collect_explicit_colorspace.py +++ b/openpype/hosts/traypublisher/plugins/publish/collect_explicit_colorspace.py @@ -50,30 +50,13 @@ class CollectColorspace(pyblish.api.InstancePlugin, ) if config_data: - filepath = config_data["path"] - config_items = colorspace.get_ocio_config_colorspaces(filepath) - aliases = set() - for _, value_ in config_items.items(): - if value_.get("type") != "colorspace": - continue - if not value_.get("aliases"): - continue - for alias in value_.get("aliases"): - aliases.add(alias) - - colorspaces = { - name for name, data_ in config_items.items() - if data_.get("type") == "colorspace" - } - - cls.colorspace_items.extend(( - (name, f"{name} [colorspace]") for name in colorspaces - )) - if aliases: - cls.colorspace_items.extend(( - (name, f"{name} [alias]") for name in aliases - )) + labeled_colorspaces = colorspace.get_labeled_colorspaces( + filepath, + include_aliases=True, + include_roles=True + ) + cls.colorspace_items.extend(labeled_colorspaces) cls.enabled = True @classmethod diff --git a/openpype/pipeline/colorspace.py b/openpype/pipeline/colorspace.py index 2800050496..39fdef046b 100644 --- a/openpype/pipeline/colorspace.py +++ b/openpype/pipeline/colorspace.py @@ -356,7 +356,10 @@ def parse_colorspace_from_filepath( "Must provide `config_path` if `colorspaces` is not provided." ) - colorspaces = colorspaces or get_ocio_config_colorspaces(config_path) + colorspaces = ( + colorspaces + or get_ocio_config_colorspaces(config_path)["colorspace"] + ) underscored_colorspaces = { key.replace(" ", "_"): key for key in colorspaces if " " in key @@ -393,7 +396,7 @@ def validate_imageio_colorspace_in_config(config_path, colorspace_name): Returns: bool: True if exists """ - colorspaces = get_ocio_config_colorspaces(config_path) + colorspaces = get_ocio_config_colorspaces(config_path)["colorspace"] if colorspace_name not in colorspaces: raise KeyError( "Missing colorspace '{}' in config file '{}'".format( @@ -530,6 +533,76 @@ def get_ocio_config_colorspaces(config_path): return CachedData.ocio_config_colorspaces[config_path] +def get_labeled_colorspaces( + config_path, + include_aliases=False, + include_looks=False, + include_roles=False, + +): + """Get all colorspace data with labels + + Wrapper function for aggregating all names and its families. + Families can be used for building menu and submenus in gui. + + Args: + config_path (str): path leading to config.ocio file + include_aliases (bool): include aliases in result + include_looks (bool): include looks in result + include_roles (bool): include roles in result + + Returns: + list[tuple[str,str]]: colorspace and family in couple + """ + config_items = get_ocio_config_colorspaces(config_path) + labeled_colorspaces = [] + aliases = set() + colorspaces = set() + looks = set() + roles = set() + for items_type, colorspace_items in config_items.items(): + if items_type == "colorspace": + for color_name, color_data in colorspace_items.items(): + if color_data.get("aliases"): + aliases.update([ + "{} ({})".format(alias_name, color_name) + for alias_name in color_data["aliases"] + ]) + colorspaces.add(color_name) + elif items_type == "look": + looks.update([ + "{} ({})".format(name, role_data["process_space"]) + for name, role_data in colorspace_items.items() + ]) + elif items_type == "role": + roles.update([ + "{} ({})".format(name, role_data["colorspace"]) + for name, role_data in colorspace_items.items() + ]) + + if roles and include_roles: + labeled_colorspaces.extend(( + (name, f"[role] {name}") for name in roles + )) + + labeled_colorspaces.extend(( + (name, f"[colorspace] {name}") for name in colorspaces + )) + + if aliases and include_aliases: + labeled_colorspaces.extend(( + (name, f"[alias] {name}") for name in aliases + )) + + if looks and include_looks: + labeled_colorspaces.extend(( + (name, f"[look] {name}") for name in looks + )) + + + return labeled_colorspaces + + # TODO: remove this in future - backward compatibility @deprecated("_get_wrapped_with_subprocess") def get_colorspace_data_subprocess(config_path): diff --git a/openpype/scripts/ocio_wrapper.py b/openpype/scripts/ocio_wrapper.py index 092d94623f..be21f0984f 100644 --- a/openpype/scripts/ocio_wrapper.py +++ b/openpype/scripts/ocio_wrapper.py @@ -107,37 +107,32 @@ def _get_colorspace_data(config_path): config = ocio.Config().CreateFromFile(str(config_path)) colorspace_data = { - color.getName(): { - "type": "colorspace", - "family": color.getFamily(), - "categories": list(color.getCategories()), - "aliases": list(color.getAliases()), - "equalitygroup": color.getEqualityGroup(), + "colorspace": { + color.getName(): { + "family": color.getFamily(), + "categories": list(color.getCategories()), + "aliases": list(color.getAliases()), + "equalitygroup": color.getEqualityGroup(), + } + for color in config.getColorSpaces() } - for color in config.getColorSpaces() } # add looks looks = config.getLooks() if looks: - colorspace_data.update({ - look.getName(): { - "type": "look", - "process_space": look.getProcessSpace() - } + colorspace_data["look"] = { + look.getName(): {"process_space": look.getProcessSpace()} for look in looks - }) + } # add roles roles = config.getRoles() if roles: - colorspace_data.update({ - role: { - "type": "role", - "colorspace": colorspace - } + colorspace_data["role"] = { + role: {"colorspace": colorspace} for (role, colorspace) in roles - }) + } return colorspace_data