Skip to content
This repository has been archived by the owner on Sep 20, 2024. It is now read-only.

Commit

Permalink
Fix settings colorspace from representation data.
Browse files Browse the repository at this point in the history
  • Loading branch information
tokejepsen committed Feb 15, 2024
1 parent e020ca7 commit fa404cb
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 42 deletions.
33 changes: 21 additions & 12 deletions openpype/hosts/nuke/api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,33 +103,42 @@ def colorspace_exists_on_node(node, colorspace_name):
except ValueError:
# knob is not available on input node
return False
all_clrs = get_colorspace_list(colorspace_knob)

return colorspace_name in all_clrs
return colorspace_name in get_colorspace_list(colorspace_knob)


def get_colorspace_list(colorspace_knob):
"""Get available colorspace profile names
Because the values returned from colorspace_knob.values() do not correspond
to the value returned from colorspace_knob.value(), and the extracted
colorspace comes from using colorspace_knob.value(), we need to iterate
through all values to get the correct value.
A code example of the above would be:
for count, value in enumerate(colorspace_knob.values()):
colorspace_knob.setValue(count)
print(colorspace_knob.value() in colorspace_knob.values())
Args:
colorspace_knob (nuke.Knob): nuke knob object
Returns:
list: list of strings names of profiles
"""
original_value = colorspace_knob.value()

all_clrs = list(colorspace_knob.values())
reduced_clrs = []
colorspaces = []

if not colorspace_knob.getFlag(nuke.STRIP_CASCADE_PREFIX):
return all_clrs

# strip colorspace with nested path
for clrs in all_clrs:
clrs = clrs.split('/')[-1]
reduced_clrs.append(clrs)
try:
for count, value in enumerate(colorspace_knob.values()):
colorspace_knob.setValue(count)
colorspaces.append(colorspace_knob.value())
finally:
colorspace_knob.setValue(original_value)

return reduced_clrs
return colorspaces


def is_headless():
Expand Down
112 changes: 82 additions & 30 deletions openpype/hosts/nuke/plugins/load/load_clip.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
get_current_project_name,
get_representation_path,
)
from openpype.pipeline.colorspace import (
get_imageio_file_rules_colorspace_from_filepath
)
from openpype.hosts.nuke.api.lib import (
get_imageio_input_colorspace,
maintained_selection
Expand Down Expand Up @@ -101,7 +104,6 @@ def load(self, context, name, namespace, options):

filepath = self.filepath_from_context(context)
filepath = filepath.replace("\\", "/")
self.log.debug("_ filepath: {}".format(filepath))

start_at_workfile = options.get(
"start_at_workfile", self.options_defaults["start_at_workfile"])
Expand Down Expand Up @@ -140,6 +142,7 @@ def load(self, context, name, namespace, options):
"Representation id `{}` is failing to load".format(repre_id))
return

print(representation["data"]["colorspaceData"])
read_name = self._get_node_name(representation)

# Create the Loader with the filename path set
Expand All @@ -154,8 +157,8 @@ def load(self, context, name, namespace, options):
with viewer_update_and_undo_stop():
read_node["file"].setValue(filepath)

used_colorspace = self._set_colorspace(
read_node, version_data, representation["data"], filepath)
self.set_colorspace_to_node(
read_node, filepath, version, representation)

self._set_range_to_node(read_node, first, last, start_at_workfile)

Expand All @@ -180,8 +183,6 @@ def load(self, context, name, namespace, options):
colorspace = representation["data"].get(key)
colorspace = colorspace or version_data.get(key)
data_imprint["db_colorspace"] = colorspace
if used_colorspace:
data_imprint["used_colorspace"] = used_colorspace
else:
value_ = context["version"]['data'].get(
key, str(None))
Expand Down Expand Up @@ -302,8 +303,8 @@ def update(self, container, representation):
# to avoid multiple undo steps for rest of process
# we will switch off undo-ing
with viewer_update_and_undo_stop():
used_colorspace = self._set_colorspace(
read_node, version_data, representation["data"], filepath)
self.set_colorspace_to_node(
read_node, filepath, version_doc, representation)

self._set_range_to_node(read_node, first, last, start_at_workfile)

Expand All @@ -320,10 +321,6 @@ def update(self, container, representation):
"author": version_data.get("author")
}

# add used colorspace if found any
if used_colorspace:
updated_dict["used_colorspace"] = used_colorspace

last_version_doc = get_last_version_by_subset_id(
project_name, version_doc["parent"], fields=["_id"]
)
Expand All @@ -350,6 +347,36 @@ def update(self, container, representation):

self.set_as_member(read_node)

def set_colorspace_to_node(
self,
read_node,
filepath,
version_doc,
representation_doc,
):
"""Set colorspace to read node.
Sets colorspace with available names validation.
Args:
read_node (nuke.Node): The nuke's read node
filepath (str): file path
version_doc (dict): version document
representation_doc (dict): representation document
"""
used_colorspace = self._get_colorspace_data(
version_doc, representation_doc, filepath)

if (
used_colorspace
and colorspace_exists_on_node(read_node, used_colorspace)
):
self.log.info(f"Used colorspace: {used_colorspace}")
read_node["colorspace"].setValue(used_colorspace)
else:
self.log.info("Colorspace not set...")

def remove(self, container):
read_node = container["node"]
assert read_node.Class() == "Read", "Must be Read"
Expand Down Expand Up @@ -450,25 +477,50 @@ def _get_node_name(self, representation):

return self.node_name_template.format(**name_data)

def _set_colorspace(self, node, version_data, repre_data, path):
output_color = None
path = path.replace("\\", "/")
# get colorspace
colorspace = repre_data.get("colorspace")
colorspace = colorspace or version_data.get("colorspace")
def _get_colorspace_data(self, version_doc, representation_doc, filepath):
"""Get colorspace data from version and representation documents
# colorspace from `project_settings/nuke/imageio/regexInputs`
iio_colorspace = get_imageio_input_colorspace(path)
Args:
version_doc (dict): version document
representation_doc (dict): representation document
filepath (str): file path
# Set colorspace defined in version data
if (
colorspace is not None
and colorspace_exists_on_node(node, str(colorspace))
):
node["colorspace"].setValue(str(colorspace))
output_color = str(colorspace)
elif iio_colorspace is not None:
node["colorspace"].setValue(iio_colorspace)
output_color = iio_colorspace
Returns:
Any[str,None]: colorspace name or None
"""
# Get backward compatible colorspace key.
representation_data = representation_doc["data"]
colorspace = representation_doc["data"].get("colorspace")
self.log.debug(
f"Colorspace from representation colorspace: {colorspace}"
)

# Get backward compatible version data key if colorspace is not found.
colorspace = colorspace or version_doc["data"].get("colorspace")
self.log.debug(f"Colorspace from version colorspace: {colorspace}")

# Get colorspace from representation colorspaceData if colorspace is
# not found.
colorspace_data = representation_doc["data"].get("colorspaceData", {})
colorspace = colorspace or colorspace_data.get("colorspace")
self.log.debug(
f"Colorspace from representation colorspaceData: {colorspace}"
)

print(f"Colorspace found: {colorspace}")

# check if any filerules are not applicable
new_parsed_colorspace = get_imageio_file_rules_colorspace_from_filepath( # noqa
filepath, "nuke", get_current_project_name()
)
self.log.debug(f"Colorspace new filerules: {new_parsed_colorspace}")

# colorspace from `project_settings/nuke/imageio/regexInputs`
old_parsed_colorspace = get_imageio_input_colorspace(filepath)
self.log.debug(f"Colorspace old filerules: {old_parsed_colorspace}")

return output_color
return (
new_parsed_colorspace
or old_parsed_colorspace
or colorspace
)

0 comments on commit fa404cb

Please sign in to comment.