From a3f519fd46089f0cb507a999ea85d78ab8a637d9 Mon Sep 17 00:00:00 2001 From: Jakub Jezek Date: Tue, 27 Jun 2023 18:03:16 +0200 Subject: [PATCH 1/5] fixing current project in otio export --- openpype/hosts/hiero/api/otio/hiero_export.py | 83 ++++++++----------- 1 file changed, 35 insertions(+), 48 deletions(-) diff --git a/openpype/hosts/hiero/api/otio/hiero_export.py b/openpype/hosts/hiero/api/otio/hiero_export.py index 81cb43fa12..e6d6196196 100644 --- a/openpype/hosts/hiero/api/otio/hiero_export.py +++ b/openpype/hosts/hiero/api/otio/hiero_export.py @@ -10,23 +10,23 @@ from . import utils import hiero.core import hiero.ui -self = sys.modules[__name__] -self.track_types = { - hiero.core.VideoTrack: otio.schema.TrackKind.Video, - hiero.core.AudioTrack: otio.schema.TrackKind.Audio -} -self.project_fps = None -self.marker_color_map = { - "magenta": otio.schema.MarkerColor.MAGENTA, - "red": otio.schema.MarkerColor.RED, - "yellow": otio.schema.MarkerColor.YELLOW, - "green": otio.schema.MarkerColor.GREEN, - "cyan": otio.schema.MarkerColor.CYAN, - "blue": otio.schema.MarkerColor.BLUE, -} -self.timeline = None -self.include_tags = True +class CTX: + track_types = { + hiero.core.VideoTrack: otio.schema.TrackKind.Video, + hiero.core.AudioTrack: otio.schema.TrackKind.Audio + } + project_fps = None + marker_color_map = { + "magenta": otio.schema.MarkerColor.MAGENTA, + "red": otio.schema.MarkerColor.RED, + "yellow": otio.schema.MarkerColor.YELLOW, + "green": otio.schema.MarkerColor.GREEN, + "cyan": otio.schema.MarkerColor.CYAN, + "blue": otio.schema.MarkerColor.BLUE, + } + timeline = None + include_tags = True def flatten(_list): for item in _list: @@ -37,19 +37,6 @@ def flatten(_list): yield item -def get_current_hiero_project(remove_untitled=False): - projects = flatten(hiero.core.projects()) - if not remove_untitled: - return next(iter(projects)) - - # if remove_untitled - for proj in projects: - if "Untitled" in proj.name(): - proj.close() - else: - return proj - - def create_otio_rational_time(frame, fps): return otio.opentime.RationalTime( float(frame), @@ -152,7 +139,7 @@ def create_otio_reference(clip): file_head = media_source.filenameHead() is_sequence = not media_source.singleFile() frame_duration = media_source.duration() - fps = utils.get_rate(clip) or self.project_fps + fps = utils.get_rate(clip) or CTX.project_fps extension = os.path.splitext(path)[-1] if is_sequence: @@ -217,8 +204,8 @@ def get_marker_color(tag): res = re.search(pat, icon) if res: color = res.groupdict().get('color') - if color.lower() in self.marker_color_map: - return self.marker_color_map[color.lower()] + if color.lower() in CTX.marker_color_map: + return CTX.marker_color_map[color.lower()] return otio.schema.MarkerColor.RED @@ -232,7 +219,7 @@ def create_otio_markers(otio_item, item): # Hiero adds this tag to a lot of clips continue - frame_rate = utils.get_rate(item) or self.project_fps + frame_rate = utils.get_rate(item) or CTX.project_fps marked_range = otio.opentime.TimeRange( start_time=otio.opentime.RationalTime( @@ -279,7 +266,7 @@ def create_otio_clip(track_item): duration = int(track_item.duration()) - fps = utils.get_rate(track_item) or self.project_fps + fps = utils.get_rate(track_item) or CTX.project_fps name = track_item.name() media_reference = create_otio_reference(clip) @@ -296,7 +283,7 @@ def create_otio_clip(track_item): ) # Add tags as markers - if self.include_tags: + if CTX.include_tags: create_otio_markers(otio_clip, track_item) create_otio_markers(otio_clip, track_item.source()) @@ -319,13 +306,13 @@ def create_otio_gap(gap_start, clip_start, tl_start_frame, fps): def _create_otio_timeline(): - project = get_current_hiero_project(remove_untitled=False) - metadata = _get_metadata(self.timeline) + project = CTX.timeline.project() + metadata = _get_metadata(CTX.timeline) metadata.update({ - "openpype.timeline.width": int(self.timeline.format().width()), - "openpype.timeline.height": int(self.timeline.format().height()), - "openpype.timeline.pixelAspect": int(self.timeline.format().pixelAspect()), # noqa + "openpype.timeline.width": int(CTX.timeline.format().width()), + "openpype.timeline.height": int(CTX.timeline.format().height()), + "openpype.timeline.pixelAspect": int(CTX.timeline.format().pixelAspect()), # noqa "openpype.project.useOCIOEnvironmentOverride": project.useOCIOEnvironmentOverride(), # noqa "openpype.project.lutSetting16Bit": project.lutSetting16Bit(), "openpype.project.lutSetting8Bit": project.lutSetting8Bit(), @@ -339,10 +326,10 @@ def _create_otio_timeline(): }) start_time = create_otio_rational_time( - self.timeline.timecodeStart(), self.project_fps) + CTX.timeline.timecodeStart(), CTX.project_fps) return otio.schema.Timeline( - name=self.timeline.name(), + name=CTX.timeline.name(), global_start_time=start_time, metadata=metadata ) @@ -351,7 +338,7 @@ def _create_otio_timeline(): def create_otio_track(track_type, track_name): return otio.schema.Track( name=track_name, - kind=self.track_types[track_type] + kind=CTX.track_types[track_type] ) @@ -363,7 +350,7 @@ def add_otio_gap(track_item, otio_track, prev_out): gap = otio.opentime.TimeRange( duration=otio.opentime.RationalTime( gap_length, - self.project_fps + CTX.project_fps ) ) otio_gap = otio.schema.Gap(source_range=gap) @@ -396,14 +383,14 @@ def create_otio_timeline(): return track_item.parent().items()[itemindex - 1] # get current timeline - self.timeline = hiero.ui.activeSequence() - self.project_fps = self.timeline.framerate().toFloat() + CTX.timeline = hiero.ui.activeSequence() + CTX.project_fps = CTX.timeline.framerate().toFloat() # convert timeline to otio otio_timeline = _create_otio_timeline() # loop all defined track types - for track in self.timeline.items(): + for track in CTX.timeline.items(): # skip if track is disabled if not track.isEnabled(): continue @@ -441,7 +428,7 @@ def create_otio_timeline(): otio_track.append(otio_clip) # Add tags as markers - if self.include_tags: + if CTX.include_tags: create_otio_markers(otio_track, track) # add track to otio timeline From 2e684f107a6d355a82df97b9ede4187733881bb9 Mon Sep 17 00:00:00 2001 From: Jakub Jezek Date: Tue, 27 Jun 2023 18:04:48 +0200 Subject: [PATCH 2/5] updating hiero api with nicer code - creating constants module for api wide constants - creating CTX class for passing module wide variables instead of using fake self --- openpype/hosts/hiero/api/__init__.py | 13 ++++- openpype/hosts/hiero/api/constants.py | 3 + openpype/hosts/hiero/api/lib.py | 56 +++++++++---------- .../plugins/publish/precollect_instances.py | 2 +- .../plugins/publish/precollect_workfile.py | 3 +- 5 files changed, 44 insertions(+), 33 deletions(-) create mode 100644 openpype/hosts/hiero/api/constants.py diff --git a/openpype/hosts/hiero/api/__init__.py b/openpype/hosts/hiero/api/__init__.py index b95c0fe1d7..099db14794 100644 --- a/openpype/hosts/hiero/api/__init__.py +++ b/openpype/hosts/hiero/api/__init__.py @@ -21,8 +21,13 @@ from .pipeline import ( reset_selection ) +from .constants import ( + OPENPYPE_TAG_NAME, + DEFAULT_SEQUENCE_NAME, + DEFAULT_BIN_NAME +) + from .lib import ( - pype_tag_name, flatten, get_track_items, get_current_project, @@ -82,8 +87,12 @@ __all__ = [ "file_extensions", "work_root", + # Constants + "OPENPYPE_TAG_NAME", + "DEFAULT_SEQUENCE_NAME", + "DEFAULT_BIN_NAME", + # Lib functions - "pype_tag_name", "flatten", "get_track_items", "get_current_project", diff --git a/openpype/hosts/hiero/api/constants.py b/openpype/hosts/hiero/api/constants.py new file mode 100644 index 0000000000..61a780af33 --- /dev/null +++ b/openpype/hosts/hiero/api/constants.py @@ -0,0 +1,3 @@ +OPENPYPE_TAG_NAME = "openpypeData" +DEFAULT_SEQUENCE_NAME = "openpypeSequence" +DEFAULT_BIN_NAME = "openpypeBin" diff --git a/openpype/hosts/hiero/api/lib.py b/openpype/hosts/hiero/api/lib.py index fa874f9e9d..def79d70f1 100644 --- a/openpype/hosts/hiero/api/lib.py +++ b/openpype/hosts/hiero/api/lib.py @@ -5,7 +5,6 @@ Host specific functions where host api is connected from copy import deepcopy import os import re -import sys import platform import functools import warnings @@ -29,12 +28,22 @@ from openpype.pipeline import ( from openpype.pipeline.load import filter_containers from openpype.lib import Logger from . import tags - +from .constants import ( + OPENPYPE_TAG_NAME, + DEFAULT_SEQUENCE_NAME, + DEFAULT_BIN_NAME +) from openpype.pipeline.colorspace import ( get_imageio_config ) +class CTX: + _has_been_setup = False + _has_menu = False + _parent_gui = None + + class DeprecatedWarning(DeprecationWarning): pass @@ -82,15 +91,6 @@ def deprecated(new_destination): log = Logger.get_logger(__name__) -self = sys.modules[__name__] -self._has_been_setup = False -self._has_menu = False -self._registered_gui = None -self._parent = None -self.pype_tag_name = "openpypeData" -self.default_sequence_name = "openpypeSequence" -self.default_bin_name = "openpypeBin" - def flatten(_list): for item in _list: @@ -131,7 +131,7 @@ def get_current_sequence(name=None, new=False): if new: # create new - name = name or self.default_sequence_name + name = name or DEFAULT_SEQUENCE_NAME sequence = hiero.core.Sequence(name) root_bin.addItem(hiero.core.BinItem(sequence)) elif name: @@ -345,7 +345,7 @@ def get_track_item_tags(track_item): # collect all tags which are not openpype tag returning_tag_data.extend( tag for tag in _tags - if tag.name() != self.pype_tag_name + if tag.name() != OPENPYPE_TAG_NAME ) return returning_tag_data @@ -385,7 +385,7 @@ def set_track_openpype_tag(track, data=None): # if pype tag available then update with input data tag = tags.create_tag( "{}_{}".format( - self.pype_tag_name, + OPENPYPE_TAG_NAME, _get_tag_unique_hash() ), tag_data @@ -412,7 +412,7 @@ def get_track_openpype_tag(track): return None for tag in _tags: # return only correct tag defined by global name - if self.pype_tag_name in tag.name(): + if OPENPYPE_TAG_NAME in tag.name(): return tag @@ -484,7 +484,7 @@ def get_trackitem_openpype_tag(track_item): return None for tag in _tags: # return only correct tag defined by global name - if self.pype_tag_name in tag.name(): + if OPENPYPE_TAG_NAME in tag.name(): return tag @@ -516,7 +516,7 @@ def set_trackitem_openpype_tag(track_item, data=None): # if pype tag available then update with input data tag = tags.create_tag( "{}_{}".format( - self.pype_tag_name, + OPENPYPE_TAG_NAME, _get_tag_unique_hash() ), tag_data @@ -698,29 +698,29 @@ def setup(console=False, port=None, menu=True): menu (bool, optional): Display file menu in Hiero. """ - if self._has_been_setup: + if CTX._has_been_setup: teardown() add_submission() if menu: add_to_filemenu() - self._has_menu = True + CTX._has_menu = True - self._has_been_setup = True + CTX._has_been_setup = True log.debug("pyblish: Loaded successfully.") def teardown(): """Remove integration""" - if not self._has_been_setup: + if not CTX._has_been_setup: return - if self._has_menu: + if CTX._has_menu: remove_from_filemenu() - self._has_menu = False + CTX._has_menu = False - self._has_been_setup = False + CTX._has_been_setup = False log.debug("pyblish: Integration torn down successfully") @@ -928,7 +928,7 @@ def create_bin(path=None, project=None): # get the first loaded project project = project or get_current_project() - path = path or self.default_bin_name + path = path or DEFAULT_BIN_NAME path = path.replace("\\", "/").split("/") @@ -1311,11 +1311,11 @@ def before_project_save(event): def get_main_window(): """Acquire Nuke's main window""" - if self._parent is None: + if CTX._parent_gui is None: top_widgets = QtWidgets.QApplication.topLevelWidgets() name = "Foundry::UI::DockMainWindow" main_window = next(widget for widget in top_widgets if widget.inherits("QMainWindow") and widget.metaObject().className() == name) - self._parent = main_window - return self._parent + CTX._parent_gui = main_window + return CTX._parent_gui diff --git a/openpype/hosts/hiero/plugins/publish/precollect_instances.py b/openpype/hosts/hiero/plugins/publish/precollect_instances.py index bb02919b35..3f9da2cf60 100644 --- a/openpype/hosts/hiero/plugins/publish/precollect_instances.py +++ b/openpype/hosts/hiero/plugins/publish/precollect_instances.py @@ -310,7 +310,7 @@ class PrecollectInstances(pyblish.api.ContextPlugin): # add pypedata marker to otio_clip metadata for marker in otio_clip.markers: - if phiero.pype_tag_name in marker.name: + if phiero.OPENPYPE_TAG_NAME in marker.name: otio_clip.metadata.update(marker.metadata) return {"otioClip": otio_clip} diff --git a/openpype/hosts/hiero/plugins/publish/precollect_workfile.py b/openpype/hosts/hiero/plugins/publish/precollect_workfile.py index 08963f98fd..1f477c1639 100644 --- a/openpype/hosts/hiero/plugins/publish/precollect_workfile.py +++ b/openpype/hosts/hiero/plugins/publish/precollect_workfile.py @@ -8,7 +8,6 @@ from qtpy.QtGui import QPixmap import hiero.ui from openpype.pipeline import legacy_io -from openpype.hosts.hiero import api as phiero from openpype.hosts.hiero.api.otio import hiero_export @@ -22,8 +21,8 @@ class PrecollectWorkfile(pyblish.api.ContextPlugin): asset = legacy_io.Session["AVALON_ASSET"] subset = "workfile" - project = phiero.get_current_project() active_timeline = hiero.ui.activeSequence() + project = active_timeline.project() fps = active_timeline.framerate().toFloat() # adding otio timeline to context From a6fe26e31788267561633e664573f5064e473b91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Je=C5=BEek?= Date: Wed, 28 Jun 2023 11:42:22 +0200 Subject: [PATCH 3/5] Update openpype/hosts/hiero/api/otio/hiero_export.py Co-authored-by: Jakub Trllo <43494761+iLLiCiTiT@users.noreply.github.com> --- openpype/hosts/hiero/api/otio/hiero_export.py | 25 ++++++++++--------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/openpype/hosts/hiero/api/otio/hiero_export.py b/openpype/hosts/hiero/api/otio/hiero_export.py index e6d6196196..35db477407 100644 --- a/openpype/hosts/hiero/api/otio/hiero_export.py +++ b/openpype/hosts/hiero/api/otio/hiero_export.py @@ -11,20 +11,21 @@ import hiero.core import hiero.ui +TRACK_TYPE_MAP = { + hiero.core.VideoTrack: otio.schema.TrackKind.Video, + hiero.core.AudioTrack: otio.schema.TrackKind.Audio +} +MARKER_COLOR_MAP = { + "magenta": otio.schema.MarkerColor.MAGENTA, + "red": otio.schema.MarkerColor.RED, + "yellow": otio.schema.MarkerColor.YELLOW, + "green": otio.schema.MarkerColor.GREEN, + "cyan": otio.schema.MarkerColor.CYAN, + "blue": otio.schema.MarkerColor.BLUE, +} + class CTX: - track_types = { - hiero.core.VideoTrack: otio.schema.TrackKind.Video, - hiero.core.AudioTrack: otio.schema.TrackKind.Audio - } project_fps = None - marker_color_map = { - "magenta": otio.schema.MarkerColor.MAGENTA, - "red": otio.schema.MarkerColor.RED, - "yellow": otio.schema.MarkerColor.YELLOW, - "green": otio.schema.MarkerColor.GREEN, - "cyan": otio.schema.MarkerColor.CYAN, - "blue": otio.schema.MarkerColor.BLUE, - } timeline = None include_tags = True From cf8c30abcfb4cf801742e4853222dc3cfb7df11d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Je=C5=BEek?= Date: Wed, 28 Jun 2023 11:42:29 +0200 Subject: [PATCH 4/5] Update openpype/hosts/hiero/api/lib.py Co-authored-by: Jakub Trllo <43494761+iLLiCiTiT@users.noreply.github.com> --- openpype/hosts/hiero/api/lib.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/openpype/hosts/hiero/api/lib.py b/openpype/hosts/hiero/api/lib.py index def79d70f1..057af49f64 100644 --- a/openpype/hosts/hiero/api/lib.py +++ b/openpype/hosts/hiero/api/lib.py @@ -38,10 +38,10 @@ from openpype.pipeline.colorspace import ( ) -class CTX: - _has_been_setup = False - _has_menu = False - _parent_gui = None +class _CTX: + has_been_setup = False + has_menu = False + parent_gui = None class DeprecatedWarning(DeprecationWarning): From 04f7c125b0ab4d4849164fdf4a40aa09f1f69f30 Mon Sep 17 00:00:00 2001 From: Jakub Jezek Date: Wed, 28 Jun 2023 11:49:36 +0200 Subject: [PATCH 5/5] fixes after suggestions --- openpype/hosts/hiero/api/lib.py | 30 +++++++++---------- openpype/hosts/hiero/api/otio/hiero_export.py | 21 ++++++------- 2 files changed, 26 insertions(+), 25 deletions(-) diff --git a/openpype/hosts/hiero/api/lib.py b/openpype/hosts/hiero/api/lib.py index 057af49f64..09d73f5cc2 100644 --- a/openpype/hosts/hiero/api/lib.py +++ b/openpype/hosts/hiero/api/lib.py @@ -92,13 +92,13 @@ def deprecated(new_destination): log = Logger.get_logger(__name__) -def flatten(_list): - for item in _list: - if isinstance(item, (list, tuple)): - for sub_item in flatten(item): +def flatten(list_): + for item_ in list_: + if isinstance(item_, (list, tuple)): + for sub_item in flatten(item_): yield sub_item else: - yield item + yield item_ def get_current_project(remove_untitled=False): @@ -698,29 +698,29 @@ def setup(console=False, port=None, menu=True): menu (bool, optional): Display file menu in Hiero. """ - if CTX._has_been_setup: + if _CTX.has_been_setup: teardown() add_submission() if menu: add_to_filemenu() - CTX._has_menu = True + _CTX.has_menu = True - CTX._has_been_setup = True + _CTX.has_been_setup = True log.debug("pyblish: Loaded successfully.") def teardown(): """Remove integration""" - if not CTX._has_been_setup: + if not _CTX.has_been_setup: return - if CTX._has_menu: + if _CTX.has_menu: remove_from_filemenu() - CTX._has_menu = False + _CTX.has_menu = False - CTX._has_been_setup = False + _CTX.has_been_setup = False log.debug("pyblish: Integration torn down successfully") @@ -1311,11 +1311,11 @@ def before_project_save(event): def get_main_window(): """Acquire Nuke's main window""" - if CTX._parent_gui is None: + if _CTX.parent_gui is None: top_widgets = QtWidgets.QApplication.topLevelWidgets() name = "Foundry::UI::DockMainWindow" main_window = next(widget for widget in top_widgets if widget.inherits("QMainWindow") and widget.metaObject().className() == name) - CTX._parent_gui = main_window - return CTX._parent_gui + _CTX.parent_gui = main_window + return _CTX.parent_gui diff --git a/openpype/hosts/hiero/api/otio/hiero_export.py b/openpype/hosts/hiero/api/otio/hiero_export.py index 35db477407..de547f3046 100644 --- a/openpype/hosts/hiero/api/otio/hiero_export.py +++ b/openpype/hosts/hiero/api/otio/hiero_export.py @@ -3,7 +3,6 @@ import os import re -import sys import ast import opentimelineio as otio from . import utils @@ -23,19 +22,21 @@ MARKER_COLOR_MAP = { "cyan": otio.schema.MarkerColor.CYAN, "blue": otio.schema.MarkerColor.BLUE, } - + + class CTX: project_fps = None timeline = None include_tags = True -def flatten(_list): - for item in _list: - if isinstance(item, (list, tuple)): - for sub_item in flatten(item): + +def flatten(list_): + for item_ in list_: + if isinstance(item_, (list, tuple)): + for sub_item in flatten(item_): yield sub_item else: - yield item + yield item_ def create_otio_rational_time(frame, fps): @@ -205,8 +206,8 @@ def get_marker_color(tag): res = re.search(pat, icon) if res: color = res.groupdict().get('color') - if color.lower() in CTX.marker_color_map: - return CTX.marker_color_map[color.lower()] + if color.lower() in MARKER_COLOR_MAP: + return MARKER_COLOR_MAP[color.lower()] return otio.schema.MarkerColor.RED @@ -339,7 +340,7 @@ def _create_otio_timeline(): def create_otio_track(track_type, track_name): return otio.schema.Track( name=track_name, - kind=CTX.track_types[track_type] + kind=TRACK_TYPE_MAP[track_type] )