[docs]classAYONOCIOLookFileProcessor(object):"""Class for processing an AYON OCIO Look file. Arguments: filepath (Path): Path to the OCIO Look file. Attributes: target_path (Path): Target path for the operator. log (logging.Logger): Logger instance. """filepath:Pathtarget_path:Path=Nonelog:logging.Logger=log_ocio_objects:List[OCIO.Transform]=[]def__init__(self,filepath:Path,target_path:Path=None,logger:logging.Logger=None)->None:self.filepath=filepathself.target_path=target_pathiflogger:self.log=loggerself.load()@propertydefocio_objects(self)->List:"""List of OCIO objects to be processed."""returnself._ocio_objects
[docs]defclear_ocio_objects(self)->None:"""Clears lists of all OCIO objects."""self._ocio_objects=[]
[docs]defload(self)->None:"""Load the OCIO Look file. Note: This globs all relative files recursively so we can make sure files in transforms are having correct path. Attention: This method clears the OCIO objects before loading the file. """# first clear all OCIO objectsself.clear_ocio_objects()ociolook_file_path=self.filepath.resolve().as_posix()# get all relative files recursively so we can make sure files in# transforms are having correct pathall_relative_files={f.name:fforfinPath(self.filepath.parent).rglob("*")}withopen(ociolook_file_path,"r")asf:ops_data=json.load(f)schema_data_version=ops_data.get("version",1)ifschema_data_version!=1:raiseValueError(f"Schema data version {schema_data_version} is not supported")# INFO: This is a temporary fix to handle the case where# the filepath is not found in the data# add all relative files to the dataforiteminops_data["data"]["ocioLookItems"]:self._sanitize_file_path(item,all_relative_files)self._process_look_file_to_ocio_objects(ops_data["data"])
def_process_look_file_to_ocio_objects(self,data:dict)->None:"""Process the OCIO Look file to OCIO objects. Args: data (dict): The OCIO Look data. """look_working_colorspace=data["ocioLookWorkingSpace"]["colorspace"]look_items=data["ocioLookItems"]forindex,iteminenumerate(look_items):filepath=item["file"]lut_in_colorspace=item["input_colorspace"]["colorspace"]lut_out_colorspace=item["output_colorspace"]["colorspace"]direction=item["direction"]interpolation=item["interpolation"]ifindex==0:# set the first colorspace as the current working colorspacecurrent_working_colorspace=look_working_colorspaceifcurrent_working_colorspace!=lut_in_colorspace:self._ocio_objects.append(OCIO.ColorSpaceTransform(src=current_working_colorspace,dst=lut_in_colorspace,))self._ocio_objects.append(OCIO.FileTransform(src=Path(filepath).as_posix(),interpolation=operators.get_interpolation(interpolation),direction=operators.get_direction(direction),))current_working_colorspace=lut_out_colorspace# making sure we are back in the working colorspaceifcurrent_working_colorspace!=look_working_colorspace:self._ocio_objects.append(OCIO.ColorSpaceTransform(src=current_working_colorspace,dst=look_working_colorspace,))
[docs]defget_oiiotool_cmd(self)->List[str]:"""Get arguments for the OIIO command."""args=[]forooinself.ocio_objects:ifisinstance(oo,OCIO.FileTransform):lut=Path(oo.getSrc()).resolve()args.extend(["--ociofiletransform",f"{lut.as_posix()}"])ifisinstance(oo,OCIO.ColorSpaceTransform):args.extend(["--colorconvert",oo.getSrc(),oo.getDst()])returnargs
def_sanitize_file_path(self,repre_data:dict,all_relative_files:dict)->None:extension=repre_data["ext"]ifrepre_data.get("file"):returnifself.target_path:# set file with extensionrepre_data["file"]=f"{self.target_path.stem}.{extension}"else:forfile,pathinall_relative_files.items():iffile.endswith(extension):repre_data["file"]=path.resolve().as_posix()breakifnotrepre_data.get("file"):log.warning(f"File not found: {repre_data['name']}.{repre_data['ext']}.")self.log.info(f"Added missing file path: {repre_data['file']}")