diff --git a/openpype/pipeline/colorspace.py b/openpype/pipeline/colorspace.py index 77ade773c5..557e96bad9 100644 --- a/openpype/pipeline/colorspace.py +++ b/openpype/pipeline/colorspace.py @@ -1,6 +1,7 @@ from copy import deepcopy import re import os +import sys import json import platform import contextlib @@ -139,51 +140,67 @@ def validate_imageio_colorspace_in_config(config_path, colorspace_name): def get_ocio_config_colorspaces(config_path): - with _make_temp_json_file() as tmp_json_path: - # Prepare subprocess arguments - args = [ - "run", get_ocio_config_script_path(), - "config", "get_colorspace", - "--in_path", config_path, - "--out_path", tmp_json_path + if sys.version_info[0] == 2: + with _make_temp_json_file() as tmp_json_path: + return get_colorspace_data_subprocess( + config_path, tmp_json_path + ) + from ..scripts.ocio_config_op import get_colorspace_data + return get_colorspace_data(config_path) - ] - log.info("Executing: {}".format(" ".join(args))) - process_kwargs = { - "logger": log, - "env": {} - } +def get_colorspace_data_subprocess(config_path, tmp_json_path): + # Prepare subprocess arguments + args = [ + "run", get_ocio_config_script_path(), + "config", "get_colorspace", + "--in_path", config_path, + "--out_path", tmp_json_path - run_openpype_process(*args, **process_kwargs) + ] + log.info("Executing: {}".format(" ".join(args))) - # return all colorspaces - return_json_data = open(tmp_json_path).read() - return json.loads(return_json_data) + process_kwargs = { + "logger": log, + "env": {} + } + + run_openpype_process(*args, **process_kwargs) + + # return all colorspaces + return_json_data = open(tmp_json_path).read() + return json.loads(return_json_data) def get_ocio_config_views(config_path): - with _make_temp_json_file() as tmp_json_path: - # Prepare subprocess arguments - args = [ - "run", get_ocio_config_script_path(), - "config", "get_views", - "--in_path", config_path, - "--out_path", tmp_json_path + if sys.version_info[0] == 2: + with _make_temp_json_file() as tmp_json_path: + return get_views_data_subprocess(config_path, tmp_json_path) + from ..scripts.ocio_config_op import get_views_data + return get_views_data(config_path) - ] - log.info("Executing: {}".format(" ".join(args))) - process_kwargs = { - "logger": log, - "env": {} - } +def get_views_data_subprocess(config_path, tmp_json_path): + # Prepare subprocess arguments + args = [ + "run", get_ocio_config_script_path(), + "config", "get_views", + "--in_path", config_path, + "--out_path", tmp_json_path - run_openpype_process(*args, **process_kwargs) + ] + log.info("Executing: {}".format(" ".join(args))) - # return all colorspaces - return_json_data = open(tmp_json_path).read() - return json.loads(return_json_data) + process_kwargs = { + "logger": log, + "env": {} + } + + run_openpype_process(*args, **process_kwargs) + + # return all colorspaces + return_json_data = open(tmp_json_path).read() + return json.loads(return_json_data) def get_imageio_config( diff --git a/openpype/scripts/ocio_config_op.py b/openpype/scripts/ocio_config_op.py index 0f1c0661a0..3f1ddc13c0 100644 --- a/openpype/scripts/ocio_config_op.py +++ b/openpype/scripts/ocio_config_op.py @@ -28,17 +28,9 @@ def config(): help="path where to write output json file", type=click.Path()) def get_colorspace(in_path, out_path): - config_path = Path(in_path) json_path = Path(out_path) - if not config_path.is_file(): - raise IOError("Input path should be `config.ocio` file") - config = ocio.Config().CreateFromFile(str(config_path)) - - out_data = { - c.getName(): c.getFamily() - for c in config.getColorSpaces() - } + out_data = get_colorspace_data(in_path) with open(json_path, "w") as f: json.dump(out_data, f) @@ -46,6 +38,20 @@ def get_colorspace(in_path, out_path): print(f"Colorspace data are saved to '{json_path}'") +def get_colorspace_data(config_path): + config_path = Path(config_path) + + if not config_path.is_file(): + raise IOError("Input path should be `config.ocio` file") + + config = ocio.Config().CreateFromFile(str(config_path)) + + return { + c.getName(): c.getFamily() + for c in config.getColorSpaces() + } + + @config.command( name="get_views", help=( @@ -60,18 +66,9 @@ def get_colorspace(in_path, out_path): help="path where to write output json file", type=click.Path()) def get_views(in_path, out_path): - config_path = Path(in_path) json_path = Path(out_path) - if not config_path.is_file(): - raise IOError("Input path should be `config.ocio` file") - config = ocio.Config().CreateFromFile(str(config_path)) - - out_data = { - "{}/{}".format(d, v): {"display": d, "view": v} - for d in config.getDisplays() - for v in config.getViews(d) - } + out_data = get_views_data(in_path) with open(json_path, "w") as f: json.dump(out_data, f) @@ -79,5 +76,21 @@ def get_views(in_path, out_path): print(f"Viewer data are saved to '{json_path}'") +def get_views_data(config_path): + + config_path = Path(config_path) + + if not config_path.is_file(): + raise IOError("Input path should be `config.ocio` file") + + config = ocio.Config().CreateFromFile(str(config_path)) + + return { + "{}/{}".format(d, v): {"display": d, "view": v} + for d in config.getDisplays() + for v in config.getViews(d) + } + + if __name__ == '__main__': main()