update the guess colorspace code reference from arnold

This commit is contained in:
Kayla Man 2023-02-02 11:45:08 +08:00
parent aa2f845204
commit 90ef934f49
2 changed files with 52 additions and 4 deletions

View file

@ -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'

View file

@ -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)):