in src/open_vp_cal/core/ocio_config.py [0:0]
def write_config(
colour_spaces: typing.Dict[str, LedWallColourSpaces],
filename: str,
ocio_reference_space_name: str,
base_config_path: str = None,
preview_export_filter: bool = True) -> str:
""" Writes the OpenVPCal ocio config to disk using the given map of led wall names and colour spaces
Args:
colour_spaces: The colour spaces we want to write out per led wall
filename: The filename to write the ocio config to
ocio_reference_space_name: The name of the ocio reference space
base_config_path: The base ocio config path to use
preview_export_filter: Whether to export the preview colour space or not
Returns: The file path to the ocio config we write out
"""
if not base_config_path:
base_config_path = ResourceLoader.ocio_config_path()
config = ocio.Config.CreateFromFile(base_config_path)
# Ensure that the config contains the aces_interchange role and colour space
interchange_colorspace = config.getColorSpace("aces_interchange")
if not interchange_colorspace:
raise ValueError("aces_interchange not found in OCIO config")
# Check that the ocio reference space name we are using is the same as the aces_interchange colour space
if ocio_reference_space_name != interchange_colorspace.getName():
raise ValueError(
f"OCIO Reference Space: {ocio_reference_space_name} "
f"Does Not Match The 'aces_interchange': {interchange_colorspace.getName()}"
)
added_colour_spaces = []
added_view_transforms = []
added_acess_cct_view_transforms = []
added_display_colour_spaces = []
added_acess_cct_display_colour_spaces = []
added_target_colour_spaces = []
pre_calibration_view_transform_added = []
for _, lw_cs in colour_spaces.items():
calibrated_output_name = OcioConfigWriter.get_calibrated_output_name(lw_cs)
if lw_cs.transfer_function_only_cs:
if lw_cs.transfer_function_only_cs.getName() not in added_colour_spaces:
config.addColorSpace(lw_cs.transfer_function_only_cs)
added_colour_spaces.append(lw_cs.transfer_function_only_cs.getName())
if lw_cs.pre_calibration_view_transform:
if lw_cs.pre_calibration_view_transform.getName() not in pre_calibration_view_transform_added:
config.addViewTransform(lw_cs.pre_calibration_view_transform)
pre_calibration_view_transform_added.append(lw_cs.pre_calibration_view_transform.getName())
if lw_cs.calibration_preview_cs and preview_export_filter:
if lw_cs.calibration_preview_cs.getName() not in added_colour_spaces:
config.addColorSpace(lw_cs.calibration_preview_cs)
added_colour_spaces.append(lw_cs.calibration_preview_cs.getName())
if lw_cs.target_with_inv_eotf_cs:
if lw_cs.target_with_inv_eotf_cs.getName() not in added_target_colour_spaces:
config.addColorSpace(lw_cs.target_with_inv_eotf_cs)
added_target_colour_spaces.append(lw_cs.target_with_inv_eotf_cs.getName())
if lw_cs.target_gamut_cs:
if lw_cs.target_gamut_cs.getName() not in added_target_colour_spaces:
config.addColorSpace(lw_cs.target_gamut_cs)
added_target_colour_spaces.append(lw_cs.target_gamut_cs.getName())
if lw_cs.view_transform:
if lw_cs.view_transform.getName() not in added_view_transforms:
config.addViewTransform(lw_cs.view_transform)
added_view_transforms.append(lw_cs.view_transform.getName())
if lw_cs.aces_cct_view_transform:
if lw_cs.aces_cct_view_transform.getName() not in added_view_transforms:
config.addViewTransform(lw_cs.aces_cct_view_transform)
if lw_cs.aces_cct_calibration_view_transform:
if lw_cs.aces_cct_calibration_view_transform.getName() not in added_acess_cct_view_transforms:
config.addViewTransform(lw_cs.aces_cct_calibration_view_transform)
added_acess_cct_view_transforms.append(lw_cs.aces_cct_calibration_view_transform.getName())
if lw_cs.calibration_cs:
if lw_cs.calibration_cs.getName() not in added_colour_spaces:
config.addColorSpace(lw_cs.calibration_cs)
added_colour_spaces.append(lw_cs.calibration_cs.getName())
if lw_cs.display_colour_space_cs:
if lw_cs.display_colour_space_cs.getName() not in added_display_colour_spaces:
config.addColorSpace(lw_cs.display_colour_space_cs)
added_display_colour_spaces.append(lw_cs.display_colour_space_cs.getName())
config.addDisplayView(lw_cs.display_colour_space_cs.getName(), "Raw", "Raw")
if lw_cs.aces_cct_display_colour_space_cs:
if lw_cs.aces_cct_display_colour_space_cs.getName() not in added_acess_cct_display_colour_spaces:
config.addColorSpace(lw_cs.aces_cct_display_colour_space_cs)
added_acess_cct_display_colour_spaces.append(lw_cs.aces_cct_display_colour_space_cs.getName())
config.addDisplayView(lw_cs.aces_cct_display_colour_space_cs.getName(), "Raw", "Raw")
# Adds to the displays: section of the config
if lw_cs.display_colour_space_cs and lw_cs.view_transform:
config.addDisplaySharedView(lw_cs.display_colour_space_cs.getName(),
calibrated_output_name)
if lw_cs.aces_cct_display_colour_space_cs and lw_cs.aces_cct_view_transform:
config.addDisplaySharedView(
lw_cs.aces_cct_display_colour_space_cs.getName(), lw_cs.aces_cct_view_transform.getName()
)
if lw_cs.view_transform:
config.addSharedView(
calibrated_output_name, lw_cs.view_transform.getName(), ocio.OCIO_VIEW_USE_DISPLAY_NAME)
# Update the active_views part of the config
active_views = config.getActiveViews()
comps = active_views.split(",")
if calibrated_output_name not in comps:
comps.insert(0, calibrated_output_name)
active_views = ",".join(comps)
config.setActiveViews(active_views)
if lw_cs.aces_cct_view_transform:
config.addSharedView(
lw_cs.aces_cct_view_transform.getName(), lw_cs.aces_cct_view_transform.getName(), ocio.OCIO_VIEW_USE_DISPLAY_NAME)
active_views = config.getActiveViews()
active_views += f", {lw_cs.aces_cct_view_transform.getName()}"
config.setActiveViews(active_views)
if lw_cs.aces_cct_calibration_view_transform:
config.addSharedView(
lw_cs.aces_cct_calibration_view_transform.getName(),
lw_cs.aces_cct_calibration_view_transform.getName(), ocio.OCIO_VIEW_USE_DISPLAY_NAME)
active_views = config.getActiveViews()
active_views += f", {lw_cs.aces_cct_calibration_view_transform.getName()}"
config.setActiveViews(active_views)
for added_display_colour_space in added_display_colour_spaces:
config.addDisplaySharedView(added_display_colour_space, OcioConfigWriter.pre_calibration_output)
active_displays = config.getActiveDisplays()
comps = active_displays.split(",")
if added_display_colour_space not in comps:
comps.insert(0, added_display_colour_space)
active_displays = ",".join(comps)
config.setActiveDisplays(active_displays)
for added_acess_cct_display_colour_space in added_acess_cct_display_colour_spaces:
config.addDisplaySharedView(added_acess_cct_display_colour_space, added_acess_cct_view_transforms[0])
if pre_calibration_view_transform_added:
config.addSharedView(
OcioConfigWriter.pre_calibration_output, pre_calibration_view_transform_added[0],
ocio.OCIO_VIEW_USE_DISPLAY_NAME
)
active_views = config.getActiveViews()
comps = active_views.split(",")
if OcioConfigWriter.pre_calibration_output not in comps:
comps.insert(1, OcioConfigWriter.pre_calibration_output)
active_views = ",".join(comps)
config.setActiveViews(active_views)
# Set the search path so its relative to the ocio config folder
config.setSearchPath("./")
filename = os.path.abspath(filename)
parent_dir = os.path.dirname(filename)
if parent_dir:
os.makedirs(parent_dir, exist_ok=True)
with open(filename, "w", encoding="utf-8") as file:
file.write(config.serialize())
return filename