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
This commit is contained in:
Jakub Jezek 2023-06-27 18:04:48 +02:00
parent a3f519fd46
commit 2e684f107a
No known key found for this signature in database
GPG key ID: 730D7C02726179A7
5 changed files with 44 additions and 33 deletions

View file

@ -21,8 +21,13 @@ from .pipeline import (
reset_selection reset_selection
) )
from .constants import (
OPENPYPE_TAG_NAME,
DEFAULT_SEQUENCE_NAME,
DEFAULT_BIN_NAME
)
from .lib import ( from .lib import (
pype_tag_name,
flatten, flatten,
get_track_items, get_track_items,
get_current_project, get_current_project,
@ -82,8 +87,12 @@ __all__ = [
"file_extensions", "file_extensions",
"work_root", "work_root",
# Constants
"OPENPYPE_TAG_NAME",
"DEFAULT_SEQUENCE_NAME",
"DEFAULT_BIN_NAME",
# Lib functions # Lib functions
"pype_tag_name",
"flatten", "flatten",
"get_track_items", "get_track_items",
"get_current_project", "get_current_project",

View file

@ -0,0 +1,3 @@
OPENPYPE_TAG_NAME = "openpypeData"
DEFAULT_SEQUENCE_NAME = "openpypeSequence"
DEFAULT_BIN_NAME = "openpypeBin"

View file

@ -5,7 +5,6 @@ Host specific functions where host api is connected
from copy import deepcopy from copy import deepcopy
import os import os
import re import re
import sys
import platform import platform
import functools import functools
import warnings import warnings
@ -29,12 +28,22 @@ from openpype.pipeline import (
from openpype.pipeline.load import filter_containers from openpype.pipeline.load import filter_containers
from openpype.lib import Logger from openpype.lib import Logger
from . import tags from . import tags
from .constants import (
OPENPYPE_TAG_NAME,
DEFAULT_SEQUENCE_NAME,
DEFAULT_BIN_NAME
)
from openpype.pipeline.colorspace import ( from openpype.pipeline.colorspace import (
get_imageio_config get_imageio_config
) )
class CTX:
_has_been_setup = False
_has_menu = False
_parent_gui = None
class DeprecatedWarning(DeprecationWarning): class DeprecatedWarning(DeprecationWarning):
pass pass
@ -82,15 +91,6 @@ def deprecated(new_destination):
log = Logger.get_logger(__name__) 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): def flatten(_list):
for item in _list: for item in _list:
@ -131,7 +131,7 @@ def get_current_sequence(name=None, new=False):
if new: if new:
# create new # create new
name = name or self.default_sequence_name name = name or DEFAULT_SEQUENCE_NAME
sequence = hiero.core.Sequence(name) sequence = hiero.core.Sequence(name)
root_bin.addItem(hiero.core.BinItem(sequence)) root_bin.addItem(hiero.core.BinItem(sequence))
elif name: elif name:
@ -345,7 +345,7 @@ def get_track_item_tags(track_item):
# collect all tags which are not openpype tag # collect all tags which are not openpype tag
returning_tag_data.extend( returning_tag_data.extend(
tag for tag in _tags tag for tag in _tags
if tag.name() != self.pype_tag_name if tag.name() != OPENPYPE_TAG_NAME
) )
return returning_tag_data 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 # if pype tag available then update with input data
tag = tags.create_tag( tag = tags.create_tag(
"{}_{}".format( "{}_{}".format(
self.pype_tag_name, OPENPYPE_TAG_NAME,
_get_tag_unique_hash() _get_tag_unique_hash()
), ),
tag_data tag_data
@ -412,7 +412,7 @@ def get_track_openpype_tag(track):
return None return None
for tag in _tags: for tag in _tags:
# return only correct tag defined by global name # 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 return tag
@ -484,7 +484,7 @@ def get_trackitem_openpype_tag(track_item):
return None return None
for tag in _tags: for tag in _tags:
# return only correct tag defined by global name # 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 return tag
@ -516,7 +516,7 @@ def set_trackitem_openpype_tag(track_item, data=None):
# if pype tag available then update with input data # if pype tag available then update with input data
tag = tags.create_tag( tag = tags.create_tag(
"{}_{}".format( "{}_{}".format(
self.pype_tag_name, OPENPYPE_TAG_NAME,
_get_tag_unique_hash() _get_tag_unique_hash()
), ),
tag_data tag_data
@ -698,29 +698,29 @@ def setup(console=False, port=None, menu=True):
menu (bool, optional): Display file menu in Hiero. menu (bool, optional): Display file menu in Hiero.
""" """
if self._has_been_setup: if CTX._has_been_setup:
teardown() teardown()
add_submission() add_submission()
if menu: if menu:
add_to_filemenu() 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.") log.debug("pyblish: Loaded successfully.")
def teardown(): def teardown():
"""Remove integration""" """Remove integration"""
if not self._has_been_setup: if not CTX._has_been_setup:
return return
if self._has_menu: if CTX._has_menu:
remove_from_filemenu() 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") log.debug("pyblish: Integration torn down successfully")
@ -928,7 +928,7 @@ def create_bin(path=None, project=None):
# get the first loaded project # get the first loaded project
project = project or get_current_project() project = project or get_current_project()
path = path or self.default_bin_name path = path or DEFAULT_BIN_NAME
path = path.replace("\\", "/").split("/") path = path.replace("\\", "/").split("/")
@ -1311,11 +1311,11 @@ def before_project_save(event):
def get_main_window(): def get_main_window():
"""Acquire Nuke's main window""" """Acquire Nuke's main window"""
if self._parent is None: if CTX._parent_gui is None:
top_widgets = QtWidgets.QApplication.topLevelWidgets() top_widgets = QtWidgets.QApplication.topLevelWidgets()
name = "Foundry::UI::DockMainWindow" name = "Foundry::UI::DockMainWindow"
main_window = next(widget for widget in top_widgets if main_window = next(widget for widget in top_widgets if
widget.inherits("QMainWindow") and widget.inherits("QMainWindow") and
widget.metaObject().className() == name) widget.metaObject().className() == name)
self._parent = main_window CTX._parent_gui = main_window
return self._parent return CTX._parent_gui

View file

@ -310,7 +310,7 @@ class PrecollectInstances(pyblish.api.ContextPlugin):
# add pypedata marker to otio_clip metadata # add pypedata marker to otio_clip metadata
for marker in otio_clip.markers: 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) otio_clip.metadata.update(marker.metadata)
return {"otioClip": otio_clip} return {"otioClip": otio_clip}

View file

@ -8,7 +8,6 @@ from qtpy.QtGui import QPixmap
import hiero.ui import hiero.ui
from openpype.pipeline import legacy_io from openpype.pipeline import legacy_io
from openpype.hosts.hiero import api as phiero
from openpype.hosts.hiero.api.otio import hiero_export from openpype.hosts.hiero.api.otio import hiero_export
@ -22,8 +21,8 @@ class PrecollectWorkfile(pyblish.api.ContextPlugin):
asset = legacy_io.Session["AVALON_ASSET"] asset = legacy_io.Session["AVALON_ASSET"]
subset = "workfile" subset = "workfile"
project = phiero.get_current_project()
active_timeline = hiero.ui.activeSequence() active_timeline = hiero.ui.activeSequence()
project = active_timeline.project()
fps = active_timeline.framerate().toFloat() fps = active_timeline.framerate().toFloat()
# adding otio timeline to context # adding otio timeline to context