From 90ef934f495dd82606f7a7b702f6b3187622cfd7 Mon Sep 17 00:00:00 2001 From: Kayla Man Date: Thu, 2 Feb 2023 11:45:08 +0800 Subject: [PATCH] update the guess colorspace code reference from arnold --- openpype/hosts/maya/api/lib.py | 38 +++++++++++++++++++ .../maya/plugins/publish/extract_look.py | 18 +++++++-- 2 files changed, 52 insertions(+), 4 deletions(-) diff --git a/openpype/hosts/maya/api/lib.py b/openpype/hosts/maya/api/lib.py index 25842a4776..51da7a4b98 100644 --- a/openpype/hosts/maya/api/lib.py +++ b/openpype/hosts/maya/api/lib.py @@ -15,6 +15,7 @@ from six import string_types from maya import cmds, mel import maya.api.OpenMaya as om +from arnold import * from openpype.client import ( get_project, @@ -3379,3 +3380,40 @@ def iter_visible_nodes_in_range(nodes, start, end): def get_attribute_input(attr): connections = cmds.listConnections(attr, plugs=True, destination=False) return connections[0] if connections else None + + +# Reference from Arnold +# Get Image Information for colorspace +def imageInfo(filepath): + """Take reference from makeTx.py + ImageInfo(filename): Get Image Information + AiTextureGetFormat(filename): Get Texture Format + AiTextureGetBitDepth(filename): Get Texture Bit Depth + """ + # Get Texture Information + img_info = {} + img_info['filename'] = filepath + if os.path.isfile(filepath): + img_info['bit_depth'] = AiTextureGetBitDepth(filepath) + img_info['format'] = AiTextureGetFormat(filepath) + else: + img_info['bit_depth'] = 8 + img_info['format'] = "unknown" + return img_info + +def guess_colorspace(img_info): + ''' Take reference from makeTx.py + Guess the colorspace of the input image filename. + @return: a string suitable for the --colorconvert option of maketx (linear, sRGB, Rec709) + ''' + try: + if img_info['bit_depth'] <= 16 and img_info['format'] in (AI_TYPE_BYTE, AI_TYPE_INT, AI_TYPE_UINT): + return 'sRGB' + else: + return 'linear' + + # now discard the image file as AiTextureGetFormat has loaded it + AiTextureInvalidate(img_info['filename']) + except: + print('[maketx] Error: Could not guess colorspace for "%s"' % img_info['filename']) + return 'linear' diff --git a/openpype/hosts/maya/plugins/publish/extract_look.py b/openpype/hosts/maya/plugins/publish/extract_look.py index 54b093f7c3..5ac5d9c728 100644 --- a/openpype/hosts/maya/plugins/publish/extract_look.py +++ b/openpype/hosts/maya/plugins/publish/extract_look.py @@ -16,6 +16,7 @@ import pyblish.api from openpype.lib import source_hash, run_subprocess from openpype.pipeline import legacy_io, publish from openpype.hosts.maya.api import lib +from openpype.hosts.maya.api.lib import imageInfo, guess_colorspace # Modes for transfer COPY = 1 @@ -541,16 +542,25 @@ class ExtractLook(publish.Extractor): color_space = cmds.getAttr(color_space_attr) except ValueError: # node doesn't have color space attribute - color_space = "Raw" + img_info = imageInfo(filepath) + color_space = guess_colorspace(img_info) self.log.info("tx: converting {0} -> {1}".format(color_space, render_colorspace)) # noqa additional_args.extend(["--colorconvert", color_space, render_colorspace]) else: - self.log.info("tx: converting sRGB -> linear") - additional_args.extend(["--colorconvert", "sRGB", "Raw"]) + img_info = imageInfo(filepath) + color_space = guess_colorspace(img_info) + if color_space == "sRGB": + self.log.info("tx: converting sRGB -> linear") + additional_args.extend(["--colorconvert", "sRGB", "Raw"]) + else: + self.log.info("tx: texture's colorspace is already linear") - config_path = get_ocio_config_path("nuke-default") + + config_path = cmds.colorManagementPrefs(query=True, configFilePath=True) + if not os.path.exists(config_path): + raise RuntimeError("No OCIO config path found!") additional_args.extend(["--colorconfig", config_path]) # Ensure folder exists if not os.path.exists(os.path.dirname(converted)):