ImageIO: Minor fixes (#5147)

* define variable 'resolved_path' in right scope

* fixed missing 'input' variable

* make checks for keys more explicit and safe proof

* fixed caching of remapped colorspaces

* trying to fix indentation issue

* use safe keys pop
This commit is contained in:
Jakub Trllo 2023-06-16 14:17:08 +02:00 committed by GitHub
parent f9a64192b3
commit 7ecb03bb75
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 56 additions and 59 deletions

View file

@ -2020,11 +2020,11 @@ class WorkfileSettings(object):
# TODO: backward compatibility for old projects - remove later
# perhaps old project overrides is having it set to older version
# with use of `customOCIOConfigPath`
resolved_path = None
if workfile_settings.get("customOCIOConfigPath"):
unresolved_path = workfile_settings["customOCIOConfigPath"]
ocio_paths = unresolved_path[platform.system().lower()]
resolved_path = None
for ocio_p in ocio_paths:
resolved_path = str(ocio_p).format(**os.environ)
if not os.path.exists(resolved_path):
@ -2054,9 +2054,9 @@ class WorkfileSettings(object):
self._root_node["colorManagement"].setValue("OCIO")
# we dont need the key anymore
workfile_settings.pop("customOCIOConfigPath")
workfile_settings.pop("colorManagement")
workfile_settings.pop("OCIO_config")
workfile_settings.pop("customOCIOConfigPath", None)
workfile_settings.pop("colorManagement", None)
workfile_settings.pop("OCIO_config", None)
# then set the rest
for knob, value_ in workfile_settings.items():

View file

@ -312,7 +312,8 @@ def get_views_data_subprocess(config_path):
def get_imageio_config(
project_name, host_name,
project_name,
host_name,
project_settings=None,
anatomy_data=None,
anatomy=None
@ -325,12 +326,9 @@ def get_imageio_config(
Args:
project_name (str): project name
host_name (str): host name
project_settings (dict, optional): project settings.
Defaults to None.
anatomy_data (dict, optional): anatomy formatting data.
Defaults to None.
anatomy (lib.Anatomy, optional): Anatomy object.
Defaults to None.
project_settings (Optional[dict]): Project settings.
anatomy_data (Optional[dict]): anatomy formatting data.
anatomy (Optional[Anatomy]): Anatomy object.
Returns:
dict: config path data or empty dict
@ -345,37 +343,36 @@ def get_imageio_config(
formatting_data = deepcopy(anatomy_data)
# add project roots to anatomy data
# Add project roots to anatomy data
formatting_data["root"] = anatomy.roots
formatting_data["platform"] = platform.system().lower()
# get colorspace settings
# check if global settings group is having activate_global_color_management
# key at all. If it does't then default it to False
# this is for backward compatibility only
# TODO: in future rewrite this to be more explicit
# Get colorspace settings
imageio_global, imageio_host = _get_imageio_settings(
project_settings, host_name)
activate_color_management = (
imageio_global.get("activate_global_color_management", False)
# for already saved overrides from previous version
# TODO: remove this in future - backward compatibility
or imageio_host.get("ocio_config").get("enabled")
)
# Host 'ocio_config' is optional
host_ocio_config = imageio_host.get("ocio_config") or {}
# Global color management must be enabled to be able to use host settings
activate_color_management = imageio_global.get(
"activate_global_color_management")
# TODO: remove this in future - backward compatibility
# For already saved overrides from previous version look for 'enabled'
# on host settings.
if activate_color_management is None:
activate_color_management = host_ocio_config.get("enabled", False)
if not activate_color_management:
# if global settings are disabled return empty dict because
# it is expected that no colorspace management is needed
log.info(
"Colorspace management is disabled globally."
)
log.info("Colorspace management is disabled globally.")
return {}
# check if host settings group is having activate_host_color_management
# if it does not have activation key then default it to True so it uses
# global settings
# this is for backward compatibility
# Check if host settings group is having 'activate_host_color_management'
# - if it does not have activation key then default it to True so it uses
# global settings
# This is for backward compatibility.
# TODO: in future rewrite this to be more explicit
activate_host_color_management = imageio_host.get(
"activate_host_color_management", True)
@ -389,21 +386,18 @@ def get_imageio_config(
)
return {}
config_host = imageio_host.get("ocio_config", {})
# get config path from either global or host_name
# get config path from either global or host settings
# depending on override flag
# TODO: in future rewrite this to be more explicit
config_data = None
override_global_config = (
config_host.get("override_global_config")
override_global_config = host_ocio_config.get("override_global_config")
if override_global_config is None:
# for already saved overrides from previous version
# TODO: remove this in future - backward compatibility
or config_host.get("enabled")
)
override_global_config = host_ocio_config.get("enabled")
if override_global_config:
config_data = _get_config_data(
config_host["filepath"], formatting_data
host_ocio_config["filepath"], formatting_data
)
else:
# get config path from global
@ -507,34 +501,35 @@ def get_imageio_file_rules(project_name, host_name, project_settings=None):
frules_host = imageio_host.get("file_rules", {})
# compile file rules dictionary
activate_host_rules = (
frules_host.get("activate_host_rules")
activate_host_rules = frules_host.get("activate_host_rules")
if activate_host_rules is None:
# TODO: remove this in future - backward compatibility
or frules_host.get("enabled")
)
activate_host_rules = frules_host.get("enabled", False)
# return host rules if activated or global rules
return frules_host["rules"] if activate_host_rules else global_rules
def get_remapped_colorspace_to_native(
ocio_colorspace_name, host_name, imageio_host_settings):
ocio_colorspace_name, host_name, imageio_host_settings
):
"""Return native colorspace name.
Args:
ocio_colorspace_name (str | None): ocio colorspace name
host_name (str): Host name.
imageio_host_settings (dict[str, Any]): ImageIO host settings.
Returns:
str: native colorspace name defined in remapping or None
Union[str, None]: native colorspace name defined in remapping or None
"""
if not CashedData.remapping.get(host_name, {}).get("to_native"):
CashedData.remapping.setdefault(host_name, {})
if CashedData.remapping[host_name].get("to_native") is None:
remapping_rules = imageio_host_settings["remapping"]["rules"]
CashedData.remapping[host_name] = {
"to_native": {
rule["ocio_name"]: input["host_native_name"]
for rule in remapping_rules
}
CashedData.remapping[host_name]["to_native"] = {
rule["ocio_name"]: rule["host_native_name"]
for rule in remapping_rules
}
return CashedData.remapping[host_name]["to_native"].get(
@ -542,23 +537,25 @@ def get_remapped_colorspace_to_native(
def get_remapped_colorspace_from_native(
host_native_colorspace_name, host_name, imageio_host_settings):
host_native_colorspace_name, host_name, imageio_host_settings
):
"""Return ocio colorspace name remapped from host native used name.
Args:
host_native_colorspace_name (str): host native colorspace name
host_name (str): Host name.
imageio_host_settings (dict[str, Any]): ImageIO host settings.
Returns:
str: ocio colorspace name defined in remapping or None
Union[str, None]: Ocio colorspace name defined in remapping or None.
"""
if not CashedData.remapping.get(host_name, {}).get("from_native"):
CashedData.remapping.setdefault(host_name, {})
if CashedData.remapping[host_name].get("from_native") is None:
remapping_rules = imageio_host_settings["remapping"]["rules"]
CashedData.remapping[host_name] = {
"from_native": {
input["host_native_name"]: rule["ocio_name"]
for rule in remapping_rules
}
CashedData.remapping[host_name]["from_native"] = {
rule["host_native_name"]: rule["ocio_name"]
for rule in remapping_rules
}
return CashedData.remapping[host_name]["from_native"].get(