From ecda9a6099f6efe52d8ef3121d894c76277098a4 Mon Sep 17 00:00:00 2001 From: Jakub Trllo Date: Tue, 21 Jun 2022 18:18:36 +0200 Subject: [PATCH 1/7] remove unused functions --- openpype/hosts/nuke/api/__init__.py | 6 -- openpype/hosts/nuke/api/command.py | 114 ---------------------------- openpype/hosts/nuke/api/lib.py | 23 ------ 3 files changed, 143 deletions(-) diff --git a/openpype/hosts/nuke/api/__init__.py b/openpype/hosts/nuke/api/__init__.py index b571c4098c..9e3ef1a397 100644 --- a/openpype/hosts/nuke/api/__init__.py +++ b/openpype/hosts/nuke/api/__init__.py @@ -8,9 +8,6 @@ from .workio import ( ) from .command import ( - reset_frame_range, - get_handles, - reset_resolution, viewer_update_and_undo_stop ) @@ -42,9 +39,6 @@ __all__ = ( "current_file", "work_root", - "reset_frame_range", - "get_handles", - "reset_resolution", "viewer_update_and_undo_stop", "OpenPypeCreator", diff --git a/openpype/hosts/nuke/api/command.py b/openpype/hosts/nuke/api/command.py index c756c48a12..2f772469d8 100644 --- a/openpype/hosts/nuke/api/command.py +++ b/openpype/hosts/nuke/api/command.py @@ -1,124 +1,10 @@ import logging import contextlib import nuke -from bson.objectid import ObjectId - -from openpype.pipeline import legacy_io log = logging.getLogger(__name__) -def reset_frame_range(): - """ Set frame range to current asset - Also it will set a Viewer range with - displayed handles - """ - - fps = float(legacy_io.Session.get("AVALON_FPS", 25)) - - nuke.root()["fps"].setValue(fps) - name = legacy_io.Session["AVALON_ASSET"] - asset = legacy_io.find_one({"name": name, "type": "asset"}) - asset_data = asset["data"] - - handles = get_handles(asset) - - frame_start = int(asset_data.get( - "frameStart", - asset_data.get("edit_in"))) - - frame_end = int(asset_data.get( - "frameEnd", - asset_data.get("edit_out"))) - - if not all([frame_start, frame_end]): - missing = ", ".join(["frame_start", "frame_end"]) - msg = "'{}' are not set for asset '{}'!".format(missing, name) - log.warning(msg) - nuke.message(msg) - return - - frame_start -= handles - frame_end += handles - - nuke.root()["first_frame"].setValue(frame_start) - nuke.root()["last_frame"].setValue(frame_end) - - # setting active viewers - vv = nuke.activeViewer().node() - vv["frame_range_lock"].setValue(True) - vv["frame_range"].setValue("{0}-{1}".format( - int(asset_data["frameStart"]), - int(asset_data["frameEnd"])) - ) - - -def get_handles(asset): - """ Gets handles data - - Arguments: - asset (dict): avalon asset entity - - Returns: - handles (int) - """ - data = asset["data"] - if "handles" in data and data["handles"] is not None: - return int(data["handles"]) - - parent_asset = None - if "visualParent" in data: - vp = data["visualParent"] - if vp is not None: - parent_asset = legacy_io.find_one({"_id": ObjectId(vp)}) - - if parent_asset is None: - parent_asset = legacy_io.find_one({"_id": ObjectId(asset["parent"])}) - - if parent_asset is not None: - return get_handles(parent_asset) - else: - return 0 - - -def reset_resolution(): - """Set resolution to project resolution.""" - project = legacy_io.find_one({"type": "project"}) - p_data = project["data"] - - width = p_data.get("resolution_width", - p_data.get("resolutionWidth")) - height = p_data.get("resolution_height", - p_data.get("resolutionHeight")) - - if not all([width, height]): - missing = ", ".join(["width", "height"]) - msg = "No resolution information `{0}` found for '{1}'.".format( - missing, - project["name"]) - log.warning(msg) - nuke.message(msg) - return - - current_width = nuke.root()["format"].value().width() - current_height = nuke.root()["format"].value().height() - - if width != current_width or height != current_height: - - fmt = None - for f in nuke.formats(): - if f.width() == width and f.height() == height: - fmt = f.name() - - if not fmt: - nuke.addFormat( - "{0} {1} {2}".format(int(width), int(height), project["name"]) - ) - fmt = project["name"] - - nuke.root()["format"].setValue(fmt) - - @contextlib.contextmanager def viewer_update_and_undo_stop(): """Lock viewer from updating and stop recording undo steps""" diff --git a/openpype/hosts/nuke/api/lib.py b/openpype/hosts/nuke/api/lib.py index 505eb19419..7e44aaa7c5 100644 --- a/openpype/hosts/nuke/api/lib.py +++ b/openpype/hosts/nuke/api/lib.py @@ -2151,29 +2151,6 @@ class WorkfileSettings(object): set_context_favorites(favorite_items) -def get_hierarchical_attr(entity, attr, default=None): - attr_parts = attr.split('.') - value = entity - for part in attr_parts: - value = value.get(part) - if not value: - break - - if value or entity["type"].lower() == "project": - return value - - parent_id = entity["parent"] - if ( - entity["type"].lower() == "asset" - and entity.get("data", {}).get("visualParent") - ): - parent_id = entity["data"]["visualParent"] - - parent = legacy_io.find_one({"_id": parent_id}) - - return get_hierarchical_attr(parent, attr) - - def get_write_node_template_attr(node): ''' Gets all defined data from presets From 6d4f05e3da277f11fc7614c340f63024f1997036 Mon Sep 17 00:00:00 2001 From: Jakub Trllo Date: Wed, 22 Jun 2022 14:12:05 +0200 Subject: [PATCH 2/7] added ability to pass asset name to 'get_last_version_by_subset_name' --- openpype/client/entities.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/openpype/client/entities.py b/openpype/client/entities.py index 4b4a3729fe..9864fee469 100644 --- a/openpype/client/entities.py +++ b/openpype/client/entities.py @@ -789,14 +789,19 @@ def get_last_version_by_subset_id(project_name, subset_id, fields=None): def get_last_version_by_subset_name( - project_name, subset_name, asset_id, fields=None + project_name, subset_name, asset_id=None, asset_name=None, fields=None ): - """Last version for passed subset name under asset id. + """Last version for passed subset name under asset id/name. + + It is required to pass 'asset_id' or 'asset_name'. Asset id is recommended + if is available. Args: project_name (str): Name of project where to look for queried entities. subset_name (str): Name of subset. - asset_id (str|ObjectId): Asset id which is parnt of passed subset name. + asset_id (str|ObjectId): Asset id which is parent of passed + subset name. + asset_name (str): Asset name which is parent of passed subset name. fields (list[str]): Fields that should be returned. All fields are returned if 'None' is passed. @@ -805,6 +810,14 @@ def get_last_version_by_subset_name( Dict: Version document which can be reduced to specified 'fields'. """ + if not asset_id and not asset_name: + return None + + if not asset_id: + asset_doc = get_asset_by_name(project_name, asset_name, fields=["_id"]) + if not asset_doc: + return None + asset_id = asset_doc["_id"] subset_doc = get_subset_by_name( project_name, subset_name, asset_id, fields=["_id"] ) From 4e59ae973e055953cbf77f0f9c8932120e0d19b2 Mon Sep 17 00:00:00 2001 From: Jakub Trllo Date: Wed, 22 Jun 2022 14:19:15 +0200 Subject: [PATCH 3/7] use query functions in loaders --- .../hosts/nuke/plugins/load/load_backdrop.py | 35 ++++++++------- .../nuke/plugins/load/load_camera_abc.py | 38 ++++++++-------- openpype/hosts/nuke/plugins/load/load_clip.py | 37 +++++++-------- .../hosts/nuke/plugins/load/load_effects.py | 35 ++++++++------- .../nuke/plugins/load/load_effects_ip.py | 33 +++++++------- .../hosts/nuke/plugins/load/load_gizmo.py | 34 +++++++------- .../hosts/nuke/plugins/load/load_gizmo_ip.py | 34 +++++++------- .../hosts/nuke/plugins/load/load_image.py | 34 +++++++------- .../hosts/nuke/plugins/load/load_model.py | 38 ++++++++-------- .../nuke/plugins/load/load_script_precomp.py | 45 +++++++++---------- 10 files changed, 183 insertions(+), 180 deletions(-) diff --git a/openpype/hosts/nuke/plugins/load/load_backdrop.py b/openpype/hosts/nuke/plugins/load/load_backdrop.py index 143fdf1f30..164ab6f9f4 100644 --- a/openpype/hosts/nuke/plugins/load/load_backdrop.py +++ b/openpype/hosts/nuke/plugins/load/load_backdrop.py @@ -1,6 +1,10 @@ import nuke import nukescripts +from openpype.client import ( + get_version_by_id, + get_last_version_by_subset_id, +) from openpype.pipeline import ( legacy_io, load, @@ -188,18 +192,17 @@ class LoadBackdropNodes(load.LoaderPlugin): # get main variables # Get version from io - version = legacy_io.find_one({ - "type": "version", - "_id": representation["parent"] - }) + project_name = legacy_io.active_project() + version_doc = get_version_by_id(project_name, representation["parent"]) + # get corresponding node GN = nuke.toNode(container['objectName']) file = get_representation_path(representation).replace("\\", "/") - context = representation["context"] + name = container['name'] - version_data = version.get("data", {}) - vname = version.get("name", None) + version_data = version_doc.get("data", {}) + vname = version_doc.get("name", None) first = version_data.get("frameStart", None) last = version_data.get("frameEnd", None) namespace = container['namespace'] @@ -237,20 +240,18 @@ class LoadBackdropNodes(load.LoaderPlugin): GN["name"].setValue(object_name) # get all versions in list - versions = legacy_io.find({ - "type": "version", - "parent": version["parent"] - }).distinct('name') - - max_version = max(versions) + last_version_doc = get_last_version_by_subset_id( + project_name, version_doc["parent"], fields=["_id"] + ) # change color of node - if version.get("name") not in [max_version]: - GN["tile_color"].setValue(int("0xd88467ff", 16)) + if version_doc["_id"] == last_version_doc["_id"]: + color_value = self.node_color else: - GN["tile_color"].setValue(int(self.node_color, 16)) + color_value = "0xd88467ff" + GN["tile_color"].setValue(int(color_value, 16)) - self.log.info("updated to version: {}".format(version.get("name"))) + self.log.info("updated to version: {}".format(version_doc.get("name"))) return update_container(GN, data_imprint) diff --git a/openpype/hosts/nuke/plugins/load/load_camera_abc.py b/openpype/hosts/nuke/plugins/load/load_camera_abc.py index 964ca5ec90..f5dfc8c0ab 100644 --- a/openpype/hosts/nuke/plugins/load/load_camera_abc.py +++ b/openpype/hosts/nuke/plugins/load/load_camera_abc.py @@ -1,5 +1,9 @@ import nuke +from openpype.client import ( + get_version_by_id, + get_last_version_by_subset_id +) from openpype.pipeline import ( legacy_io, load, @@ -102,17 +106,16 @@ class AlembicCameraLoader(load.LoaderPlugin): None """ # Get version from io - version = legacy_io.find_one({ - "type": "version", - "_id": representation["parent"] - }) + project_name = legacy_io.active_project() + version_doc = get_version_by_id(project_name, representation["parent"]) + object_name = container['objectName'] # get corresponding node camera_node = nuke.toNode(object_name) # get main variables - version_data = version.get("data", {}) - vname = version.get("name", None) + version_data = version_doc.get("data", {}) + vname = version_doc.get("name", None) first = version_data.get("frameStart", None) last = version_data.get("frameEnd", None) fps = version_data.get("fps") or nuke.root()["fps"].getValue() @@ -165,28 +168,27 @@ class AlembicCameraLoader(load.LoaderPlugin): d.setInput(index, camera_node) # color node by correct color by actual version - self.node_version_color(version, camera_node) + self.node_version_color(version_doc, camera_node) - self.log.info("updated to version: {}".format(version.get("name"))) + self.log.info("updated to version: {}".format(version_doc.get("name"))) return update_container(camera_node, data_imprint) - def node_version_color(self, version, node): + def node_version_color(self, version_doc, node): """ Coloring a node by correct color by actual version """ # get all versions in list - versions = legacy_io.find({ - "type": "version", - "parent": version["parent"] - }).distinct('name') - - max_version = max(versions) + project_name = legacy_io.active_project() + last_version_doc = get_last_version_by_subset_id( + project_name, version_doc["parent"], fields=["_id"] + ) # change color of node - if version.get("name") not in [max_version]: - node["tile_color"].setValue(int("0xd88467ff", 16)) + if version_doc["_id"] == last_version_doc["_id"]: + color_value = self.node_color else: - node["tile_color"].setValue(int(self.node_color, 16)) + color_value = "0xd88467ff" + node["tile_color"].setValue(int(color_value, 16)) def switch(self, container, representation): self.update(container, representation) diff --git a/openpype/hosts/nuke/plugins/load/load_clip.py b/openpype/hosts/nuke/plugins/load/load_clip.py index 681561e303..d177e6ba76 100644 --- a/openpype/hosts/nuke/plugins/load/load_clip.py +++ b/openpype/hosts/nuke/plugins/load/load_clip.py @@ -1,6 +1,10 @@ import nuke import qargparse +from openpype.client import ( + get_version_by_id, + get_last_version_by_subset_id, +) from openpype.pipeline import ( legacy_io, get_representation_path, @@ -196,11 +200,10 @@ class LoadClip(plugin.NukeLoader): start_at_workfile = bool("start at" in read_node['frame_mode'].value()) - version = legacy_io.find_one({ - "type": "version", - "_id": representation["parent"] - }) - version_data = version.get("data", {}) + project_name = legacy_io.active_project() + version_doc = get_version_by_id(project_name, representation["parent"]) + + version_data = version_doc.get("data", {}) repre_id = representation["_id"] repre_cont = representation["context"] @@ -251,7 +254,7 @@ class LoadClip(plugin.NukeLoader): "representation": str(representation["_id"]), "frameStart": str(first), "frameEnd": str(last), - "version": str(version.get("name")), + "version": str(version_doc.get("name")), "db_colorspace": colorspace, "source": version_data.get("source"), "handleStart": str(self.handle_start), @@ -264,26 +267,24 @@ class LoadClip(plugin.NukeLoader): if used_colorspace: updated_dict["used_colorspace"] = used_colorspace + last_version_doc = get_last_version_by_subset_id( + project_name, version_doc["parent"], fields=["_id"] + ) # change color of read_node - # get all versions in list - versions = legacy_io.find({ - "type": "version", - "parent": version["parent"] - }).distinct('name') - - max_version = max(versions) - - if version.get("name") not in [max_version]: - read_node["tile_color"].setValue(int("0xd84f20ff", 16)) + if version_doc["_id"] == last_version_doc["_id"]: + color_value = "0x4ecd25ff" else: - read_node["tile_color"].setValue(int("0x4ecd25ff", 16)) + color_value = "0xd84f20ff" + read_node["tile_color"].setValue(int(color_value, 16)) # Update the imprinted representation update_container( read_node, updated_dict ) - self.log.info("updated to version: {}".format(version.get("name"))) + self.log.info( + "updated to version: {}".format(version_doc.get("name")) + ) if version_data.get("retime", None): self._make_retimes(read_node, version_data) diff --git a/openpype/hosts/nuke/plugins/load/load_effects.py b/openpype/hosts/nuke/plugins/load/load_effects.py index 6a30330ed0..d164e0604c 100644 --- a/openpype/hosts/nuke/plugins/load/load_effects.py +++ b/openpype/hosts/nuke/plugins/load/load_effects.py @@ -3,6 +3,10 @@ from collections import OrderedDict import nuke import six +from openpype.client import ( + get_version_by_id, + get_last_version_by_subset_id, +) from openpype.pipeline import ( legacy_io, load, @@ -148,17 +152,16 @@ class LoadEffects(load.LoaderPlugin): """ # get main variables # Get version from io - version = legacy_io.find_one({ - "type": "version", - "_id": representation["parent"] - }) + project_name = legacy_io.active_project() + version_doc = get_version_by_id(project_name, representation["parent"]) + # get corresponding node GN = nuke.toNode(container['objectName']) file = get_representation_path(representation).replace("\\", "/") name = container['name'] - version_data = version.get("data", {}) - vname = version.get("name", None) + version_data = version_doc.get("data", {}) + vname = version_doc.get("name", None) first = version_data.get("frameStart", None) last = version_data.get("frameEnd", None) workfile_first_frame = int(nuke.root()["first_frame"].getValue()) @@ -243,21 +246,19 @@ class LoadEffects(load.LoaderPlugin): # try to find parent read node self.connect_read_node(GN, namespace, json_f["assignTo"]) - # get all versions in list - versions = legacy_io.find({ - "type": "version", - "parent": version["parent"] - }).distinct('name') - - max_version = max(versions) + last_version_doc = get_last_version_by_subset_id( + project_name, version_doc["parent"], fields=["_id"] + ) # change color of node - if version.get("name") not in [max_version]: - GN["tile_color"].setValue(int("0xd84f20ff", 16)) + if version_doc["_id"] == last_version_doc["_id"]: + color_value = "0x3469ffff" else: - GN["tile_color"].setValue(int("0x3469ffff", 16)) + color_value = "0xd84f20ff" - self.log.info("updated to version: {}".format(version.get("name"))) + GN["tile_color"].setValue(int(color_value, 16)) + + self.log.info("updated to version: {}".format(version_doc.get("name"))) def connect_read_node(self, group_node, asset, subset): """ diff --git a/openpype/hosts/nuke/plugins/load/load_effects_ip.py b/openpype/hosts/nuke/plugins/load/load_effects_ip.py index eaf151b3b8..44565c139d 100644 --- a/openpype/hosts/nuke/plugins/load/load_effects_ip.py +++ b/openpype/hosts/nuke/plugins/load/load_effects_ip.py @@ -3,6 +3,10 @@ from collections import OrderedDict import six import nuke +from openpype.client import ( + get_version_by_id, + get_last_version_by_subset_id, +) from openpype.pipeline import ( legacy_io, load, @@ -153,17 +157,16 @@ class LoadEffectsInputProcess(load.LoaderPlugin): # get main variables # Get version from io - version = legacy_io.find_one({ - "type": "version", - "_id": representation["parent"] - }) + project_name = legacy_io.active_project() + version_doc = get_version_by_id(project_name, representation["parent"]) + # get corresponding node GN = nuke.toNode(container['objectName']) file = get_representation_path(representation).replace("\\", "/") name = container['name'] - version_data = version.get("data", {}) - vname = version.get("name", None) + version_data = version_doc.get("data", {}) + vname = version_doc.get("name", None) first = version_data.get("frameStart", None) last = version_data.get("frameEnd", None) workfile_first_frame = int(nuke.root()["first_frame"].getValue()) @@ -251,20 +254,18 @@ class LoadEffectsInputProcess(load.LoaderPlugin): # return # get all versions in list - versions = legacy_io.find({ - "type": "version", - "parent": version["parent"] - }).distinct('name') - - max_version = max(versions) + last_version_doc = get_last_version_by_subset_id( + project_name, version_doc["parent"], fields=["_id"] + ) # change color of node - if version.get("name") not in [max_version]: - GN["tile_color"].setValue(int("0xd84f20ff", 16)) + if version_doc["_id"] == last_version_doc["_id"]: + color_value = "0x3469ffff" else: - GN["tile_color"].setValue(int("0x3469ffff", 16)) + color_value = "0xd84f20ff" + GN["tile_color"].setValue(int(color_value, 16)) - self.log.info("updated to version: {}".format(version.get("name"))) + self.log.info("updated to version: {}".format(version_doc.get("name"))) def connect_active_viewer(self, group_node): """ diff --git a/openpype/hosts/nuke/plugins/load/load_gizmo.py b/openpype/hosts/nuke/plugins/load/load_gizmo.py index 4ea9d64d7d..9a18eeef5c 100644 --- a/openpype/hosts/nuke/plugins/load/load_gizmo.py +++ b/openpype/hosts/nuke/plugins/load/load_gizmo.py @@ -1,5 +1,9 @@ import nuke +from openpype.client import ( + get_version_by_id, + get_last_version_by_subset_id, +) from openpype.pipeline import ( legacy_io, load, @@ -101,17 +105,16 @@ class LoadGizmo(load.LoaderPlugin): # get main variables # Get version from io - version = legacy_io.find_one({ - "type": "version", - "_id": representation["parent"] - }) + project_name = legacy_io.active_project() + version_doc = get_version_by_id(project_name, representation["parent"]) + # get corresponding node GN = nuke.toNode(container['objectName']) file = get_representation_path(representation).replace("\\", "/") name = container['name'] - version_data = version.get("data", {}) - vname = version.get("name", None) + version_data = version_doc.get("data", {}) + vname = version_doc.get("name", None) first = version_data.get("frameStart", None) last = version_data.get("frameEnd", None) namespace = container['namespace'] @@ -148,21 +151,18 @@ class LoadGizmo(load.LoaderPlugin): GN.setXYpos(xpos, ypos) GN["name"].setValue(object_name) - # get all versions in list - versions = legacy_io.find({ - "type": "version", - "parent": version["parent"] - }).distinct('name') - - max_version = max(versions) + last_version_doc = get_last_version_by_subset_id( + project_name, version_doc["parent"], fields=["_id"] + ) # change color of node - if version.get("name") not in [max_version]: - GN["tile_color"].setValue(int("0xd88467ff", 16)) + if version_doc["_id"] == last_version_doc["_id"]: + color_value = self.node_color else: - GN["tile_color"].setValue(int(self.node_color, 16)) + color_value = "0xd88467ff" + GN["tile_color"].setValue(int(color_value, 16)) - self.log.info("updated to version: {}".format(version.get("name"))) + self.log.info("updated to version: {}".format(version_doc.get("name"))) return update_container(GN, data_imprint) diff --git a/openpype/hosts/nuke/plugins/load/load_gizmo_ip.py b/openpype/hosts/nuke/plugins/load/load_gizmo_ip.py index 38dd70935e..2890dbfd2c 100644 --- a/openpype/hosts/nuke/plugins/load/load_gizmo_ip.py +++ b/openpype/hosts/nuke/plugins/load/load_gizmo_ip.py @@ -1,6 +1,10 @@ import nuke import six +from openpype.client import ( + get_version_by_id, + get_last_version_by_subset_id, +) from openpype.pipeline import ( legacy_io, load, @@ -108,17 +112,16 @@ class LoadGizmoInputProcess(load.LoaderPlugin): # get main variables # Get version from io - version = legacy_io.find_one({ - "type": "version", - "_id": representation["parent"] - }) + project_name = legacy_io.active_project() + version_doc = get_version_by_id(project_name, representation["parent"]) + # get corresponding node GN = nuke.toNode(container['objectName']) file = get_representation_path(representation).replace("\\", "/") name = container['name'] - version_data = version.get("data", {}) - vname = version.get("name", None) + version_data = version_doc.get("data", {}) + vname = version_doc.get("name", None) first = version_data.get("frameStart", None) last = version_data.get("frameEnd", None) namespace = container['namespace'] @@ -155,21 +158,18 @@ class LoadGizmoInputProcess(load.LoaderPlugin): GN.setXYpos(xpos, ypos) GN["name"].setValue(object_name) - # get all versions in list - versions = legacy_io.find({ - "type": "version", - "parent": version["parent"] - }).distinct('name') - - max_version = max(versions) + last_version_doc = get_last_version_by_subset_id( + project_name, version_doc["parent"], fields=["_id"] + ) # change color of node - if version.get("name") not in [max_version]: - GN["tile_color"].setValue(int("0xd88467ff", 16)) + if version_doc["_id"] == last_version_doc["_id"]: + color_value = self.node_color else: - GN["tile_color"].setValue(int(self.node_color, 16)) + color_value = "0xd88467ff" + GN["tile_color"].setValue(int(color_value, 16)) - self.log.info("updated to version: {}".format(version.get("name"))) + self.log.info("updated to version: {}".format(version_doc.get("name"))) return update_container(GN, data_imprint) diff --git a/openpype/hosts/nuke/plugins/load/load_image.py b/openpype/hosts/nuke/plugins/load/load_image.py index 6df286a4f7..3e81ef999b 100644 --- a/openpype/hosts/nuke/plugins/load/load_image.py +++ b/openpype/hosts/nuke/plugins/load/load_image.py @@ -2,6 +2,10 @@ import nuke import qargparse +from openpype.client import ( + get_version_by_id, + get_last_version_by_subset_id, +) from openpype.pipeline import ( legacy_io, load, @@ -186,20 +190,13 @@ class LoadImage(load.LoaderPlugin): format(frame_number, "0{}".format(padding))) # Get start frame from version data - version = legacy_io.find_one({ - "type": "version", - "_id": representation["parent"] - }) + project_name = legacy_io.active_project() + version_doc = get_version_by_id(project_name, representation["parent"]) + last_version_doc = get_last_version_by_subset_id( + project_name, version_doc["parent"], fields=["_id"] + ) - # get all versions in list - versions = legacy_io.find({ - "type": "version", - "parent": version["parent"] - }).distinct('name') - - max_version = max(versions) - - version_data = version.get("data", {}) + version_data = version_doc.get("data", {}) last = first = int(frame_number) @@ -215,7 +212,7 @@ class LoadImage(load.LoaderPlugin): "representation": str(representation["_id"]), "frameStart": str(first), "frameEnd": str(last), - "version": str(version.get("name")), + "version": str(version_doc.get("name")), "colorspace": version_data.get("colorspace"), "source": version_data.get("source"), "fps": str(version_data.get("fps")), @@ -223,17 +220,18 @@ class LoadImage(load.LoaderPlugin): }) # change color of node - if version.get("name") not in [max_version]: - node["tile_color"].setValue(int("0xd84f20ff", 16)) + if version_doc["_id"] == last_version_doc["_id"]: + color_value = "0x4ecd25ff" else: - node["tile_color"].setValue(int("0x4ecd25ff", 16)) + color_value = "0xd84f20ff" + node["tile_color"].setValue(int(color_value, 16)) # Update the imprinted representation update_container( node, updated_dict ) - self.log.info("updated to version: {}".format(version.get("name"))) + self.log.info("updated to version: {}".format(version_doc.get("name"))) def remove(self, container): node = nuke.toNode(container['objectName']) diff --git a/openpype/hosts/nuke/plugins/load/load_model.py b/openpype/hosts/nuke/plugins/load/load_model.py index 2f54595cb0..c317b15450 100644 --- a/openpype/hosts/nuke/plugins/load/load_model.py +++ b/openpype/hosts/nuke/plugins/load/load_model.py @@ -1,5 +1,9 @@ import nuke +from openpype.client import ( + get_version_by_id, + get_last_version_by_subset_id, +) from openpype.pipeline import ( legacy_io, load, @@ -100,17 +104,15 @@ class AlembicModelLoader(load.LoaderPlugin): None """ # Get version from io - version = legacy_io.find_one({ - "type": "version", - "_id": representation["parent"] - }) + project_name = legacy_io.active_project() + version_doc = get_version_by_id(project_name, representation["parent"]) object_name = container['objectName'] # get corresponding node model_node = nuke.toNode(object_name) # get main variables - version_data = version.get("data", {}) - vname = version.get("name", None) + version_data = version_doc.get("data", {}) + vname = version_doc.get("name", None) first = version_data.get("frameStart", None) last = version_data.get("frameEnd", None) fps = version_data.get("fps") or nuke.root()["fps"].getValue() @@ -163,28 +165,26 @@ class AlembicModelLoader(load.LoaderPlugin): d.setInput(index, model_node) # color node by correct color by actual version - self.node_version_color(version, model_node) + self.node_version_color(version_doc, model_node) - self.log.info("updated to version: {}".format(version.get("name"))) + self.log.info("updated to version: {}".format(version_doc.get("name"))) return update_container(model_node, data_imprint) def node_version_color(self, version, node): - """ Coloring a node by correct color by actual version - """ - # get all versions in list - versions = legacy_io.find({ - "type": "version", - "parent": version["parent"] - }).distinct('name') + """ Coloring a node by correct color by actual version""" - max_version = max(versions) + project_name = legacy_io.active_project() + last_version_doc = get_last_version_by_subset_id( + project_name, version["parent"], fields=["_id"] + ) # change color of node - if version.get("name") not in [max_version]: - node["tile_color"].setValue(int("0xd88467ff", 16)) + if version["_id"] == last_version_doc["_id"]: + color_value = self.node_color else: - node["tile_color"].setValue(int(self.node_color, 16)) + color_value = "0xd88467ff" + node["tile_color"].setValue(int(color_value, 16)) def switch(self, container, representation): self.update(container, representation) diff --git a/openpype/hosts/nuke/plugins/load/load_script_precomp.py b/openpype/hosts/nuke/plugins/load/load_script_precomp.py index bd351ad785..21e384b538 100644 --- a/openpype/hosts/nuke/plugins/load/load_script_precomp.py +++ b/openpype/hosts/nuke/plugins/load/load_script_precomp.py @@ -1,5 +1,9 @@ import nuke +from openpype.client import ( + get_version_by_id, + get_last_version_by_subset_id, +) from openpype.pipeline import ( legacy_io, load, @@ -116,29 +120,23 @@ class LinkAsGroup(load.LoaderPlugin): root = get_representation_path(representation).replace("\\", "/") # Get start frame from version data - version = legacy_io.find_one({ - "type": "version", - "_id": representation["parent"] - }) - - # get all versions in list - versions = legacy_io.find({ - "type": "version", - "parent": version["parent"] - }).distinct('name') - - max_version = max(versions) + project_name = legacy_io.active_project() + version_doc = get_version_by_id(project_name, representation["parent"]) + last_version_doc = get_last_version_by_subset_id( + project_name, version_doc["parent"], fields=["_id"] + ) updated_dict = {} + version_data = version_doc["data"] updated_dict.update({ "representation": str(representation["_id"]), - "frameEnd": version["data"].get("frameEnd"), - "version": version.get("name"), - "colorspace": version["data"].get("colorspace"), - "source": version["data"].get("source"), - "handles": version["data"].get("handles"), - "fps": version["data"].get("fps"), - "author": version["data"].get("author") + "frameEnd": version_data.get("frameEnd"), + "version": version_doc.get("name"), + "colorspace": version_data.get("colorspace"), + "source": version_data.get("source"), + "handles": version_data.get("handles"), + "fps": version_data.get("fps"), + "author": version_data.get("author") }) # Update the imprinted representation @@ -150,12 +148,13 @@ class LinkAsGroup(load.LoaderPlugin): node["file"].setValue(root) # change color of node - if version.get("name") not in [max_version]: - node["tile_color"].setValue(int("0xd84f20ff", 16)) + if version_doc["_id"] == last_version_doc["_id"]: + color_value = "0xff0ff0ff" else: - node["tile_color"].setValue(int("0xff0ff0ff", 16)) + color_value = "0xd84f20ff" + node["tile_color"].setValue(int(color_value, 16)) - self.log.info("updated to version: {}".format(version.get("name"))) + self.log.info("updated to version: {}".format(version_doc.get("name"))) def remove(self, container): node = nuke.toNode(container['objectName']) From f919c24989739e40e46028b3dfdd82642e38507c Mon Sep 17 00:00:00 2001 From: Jakub Trllo Date: Wed, 22 Jun 2022 14:20:05 +0200 Subject: [PATCH 4/7] remove unnecessary query of asset document --- .../hosts/nuke/plugins/publish/precollect_instances.py | 7 ------- 1 file changed, 7 deletions(-) diff --git a/openpype/hosts/nuke/plugins/publish/precollect_instances.py b/openpype/hosts/nuke/plugins/publish/precollect_instances.py index 8bf7280cea..4b3b70fa12 100644 --- a/openpype/hosts/nuke/plugins/publish/precollect_instances.py +++ b/openpype/hosts/nuke/plugins/publish/precollect_instances.py @@ -1,7 +1,6 @@ import nuke import pyblish.api -from openpype.pipeline import legacy_io from openpype.hosts.nuke.api.lib import ( add_publish_knob, get_avalon_knob_data @@ -20,12 +19,6 @@ class PreCollectNukeInstances(pyblish.api.ContextPlugin): sync_workfile_version_on_families = [] def process(self, context): - asset_data = legacy_io.find_one({ - "type": "asset", - "name": legacy_io.Session["AVALON_ASSET"] - }) - - self.log.debug("asset_data: {}".format(asset_data["data"])) instances = [] root = nuke.root() From 2ce3200fa5579fc18963f0e25023156179c3996d Mon Sep 17 00:00:00 2001 From: Jakub Trllo Date: Wed, 22 Jun 2022 14:21:22 +0200 Subject: [PATCH 5/7] use query functions in validate script plugin --- .../nuke/plugins/publish/validate_script.py | 56 +++++++++++++++---- 1 file changed, 44 insertions(+), 12 deletions(-) diff --git a/openpype/hosts/nuke/plugins/publish/validate_script.py b/openpype/hosts/nuke/plugins/publish/validate_script.py index 10c9e93f8b..9bda0da85e 100644 --- a/openpype/hosts/nuke/plugins/publish/validate_script.py +++ b/openpype/hosts/nuke/plugins/publish/validate_script.py @@ -1,5 +1,6 @@ import pyblish.api +from openpype.client import get_project, get_asset_by_id from openpype import lib from openpype.pipeline import legacy_io @@ -19,6 +20,7 @@ class ValidateScript(pyblish.api.InstancePlugin): asset_name = ctx_data["asset"] asset = lib.get_asset(asset_name) asset_data = asset["data"] + project_name = legacy_io.active_project() # These attributes will be checked attributes = [ @@ -48,12 +50,19 @@ class ValidateScript(pyblish.api.InstancePlugin): asset_attributes[attr] = asset_data[attr] elif attr in hierarchical_attributes: - # Try to find fps on parent - parent = asset['parent'] - if asset_data['visualParent'] is not None: - parent = asset_data['visualParent'] + # TODO this should be probably removed + # Hierarchical attributes is not a thing since Pype 2? - value = self.check_parent_hierarchical(parent, attr) + # Try to find attribute on parent + parent_id = asset['parent'] + parent_type = "project" + if asset_data['visualParent'] is not None: + parent_type = "asset" + parent_id = asset_data['visualParent'] + + value = self.check_parent_hierarchical( + project_name, parent_type, parent_id, attr + ) if value is None: missing_attributes.append(attr) else: @@ -113,12 +122,35 @@ class ValidateScript(pyblish.api.InstancePlugin): message = msg.format(", ".join(not_matching)) raise ValueError(message) - def check_parent_hierarchical(self, entityId, attr): - if entityId is None: + def check_parent_hierarchical( + self, project_name, parent_type, parent_id, attr + ): + if parent_id is None: return None - entity = legacy_io.find_one({"_id": entityId}) - if attr in entity['data']: + + doc = None + if parent_type == "project": + doc = get_project(project_name) + elif parent_type == "asset": + doc = get_asset_by_id(project_name, parent_id) + + if not doc: + return None + + doc_data = doc["data"] + if attr in doc_data: self.log.info(attr) - return entity['data'][attr] - else: - return self.check_parent_hierarchical(entity['parent'], attr) + return doc_data[attr] + + if parent_type == "project": + return None + + parent_id = doc_data.get("visualParent") + new_parent_type = "asset" + if parent_id is None: + parent_id = doc["parent"] + new_parent_type = "project" + + return self.check_parent_hierarchical( + project_name, new_parent_type, parent_id, attr + ) From 796f32bccdfd171dc8f768227bcdff4d41c53e8d Mon Sep 17 00:00:00 2001 From: Jakub Trllo Date: Wed, 22 Jun 2022 14:23:35 +0200 Subject: [PATCH 6/7] use query functions in publish plugins --- .../nuke/plugins/publish/collect_reads.py | 12 ++++----- .../nuke/plugins/publish/precollect_writes.py | 26 ++++++++++++++----- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/openpype/hosts/nuke/plugins/publish/collect_reads.py b/openpype/hosts/nuke/plugins/publish/collect_reads.py index 4d6944f523..b79d9646d5 100644 --- a/openpype/hosts/nuke/plugins/publish/collect_reads.py +++ b/openpype/hosts/nuke/plugins/publish/collect_reads.py @@ -3,6 +3,7 @@ import re import nuke import pyblish.api +from openpype.client import get_asset_by_name from openpype.pipeline import legacy_io @@ -16,12 +17,11 @@ class CollectNukeReads(pyblish.api.InstancePlugin): families = ["source"] def process(self, instance): - asset_data = legacy_io.find_one({ - "type": "asset", - "name": legacy_io.Session["AVALON_ASSET"] - }) + project_name = legacy_io.active_project() + asset_name = legacy_io.Session["AVALON_ASSET"] + asset_doc = get_asset_by_name(project_name, asset_name) - self.log.debug("asset_data: {}".format(asset_data["data"])) + self.log.debug("asset_doc: {}".format(asset_doc["data"])) self.log.debug("checking instance: {}".format(instance)) @@ -127,7 +127,7 @@ class CollectNukeReads(pyblish.api.InstancePlugin): "frameStart": first_frame, "frameEnd": last_frame, "colorspace": colorspace, - "handles": int(asset_data["data"].get("handles", 0)), + "handles": int(asset_doc["data"].get("handles", 0)), "step": 1, "fps": int(nuke.root()['fps'].value()) }) diff --git a/openpype/hosts/nuke/plugins/publish/precollect_writes.py b/openpype/hosts/nuke/plugins/publish/precollect_writes.py index 7e50679ed5..a7c07975e2 100644 --- a/openpype/hosts/nuke/plugins/publish/precollect_writes.py +++ b/openpype/hosts/nuke/plugins/publish/precollect_writes.py @@ -4,7 +4,10 @@ from pprint import pformat import nuke import pyblish.api -import openpype.api as pype +from openpype.client import ( + get_last_version_by_subset_name, + get_representations, +) from openpype.pipeline import ( legacy_io, get_representation_path, @@ -180,17 +183,26 @@ class CollectNukeWrites(pyblish.api.InstancePlugin): if not instance.data["review"]: instance.data["useSequenceForReview"] = False + project_name = legacy_io.active_project() + asset_name = instance.data["asset"] # * Add audio to instance if exists. # Find latest versions document - version_doc = pype.get_latest_version( - instance.data["asset"], "audioMain" + last_version_doc = get_last_version_by_subset_name( + project_name, "audioMain", asset_name=asset_name, fields=["_id"] ) + repre_doc = None - if version_doc: + if last_version_doc: # Try to find it's representation (Expected there is only one) - repre_doc = legacy_io.find_one( - {"type": "representation", "parent": version_doc["_id"]} - ) + repre_docs = list(get_representations( + project_name, version_ids=[last_version_doc["_id"]] + )) + if not repre_docs: + self.log.warning( + "Version document does not contain any representations" + ) + else: + repre_doc = repre_docs[0] # Add audio to instance if representation was found if repre_doc: From 20bee203523e2979df32f010a305024d94a4310e Mon Sep 17 00:00:00 2001 From: Jakub Trllo Date: Wed, 22 Jun 2022 14:25:23 +0200 Subject: [PATCH 7/7] use query functions in nuke lib --- openpype/hosts/nuke/api/lib.py | 135 ++++++++++++++++++++++----------- 1 file changed, 89 insertions(+), 46 deletions(-) diff --git a/openpype/hosts/nuke/api/lib.py b/openpype/hosts/nuke/api/lib.py index 7e44aaa7c5..57a81f7909 100644 --- a/openpype/hosts/nuke/api/lib.py +++ b/openpype/hosts/nuke/api/lib.py @@ -7,10 +7,16 @@ import contextlib from collections import OrderedDict import clique -from bson.objectid import ObjectId import nuke +from openpype.client import ( + get_project, + get_asset_by_name, + get_versions, + get_last_versions, + get_representations, +) from openpype.api import ( Logger, Anatomy, @@ -734,47 +740,84 @@ def check_inventory_versions(): from .pipeline import parse_container # get all Loader nodes by avalon attribute metadata - for each in nuke.allNodes(): - container = parse_container(each) + node_with_repre_id = [] + repre_ids = set() + # Find all containers and collect it's node and representation ids + for node in nuke.allNodes(): + container = parse_container(node) if container: node = nuke.toNode(container["objectName"]) avalon_knob_data = read_avalon_data(node) + repre_id = avalon_knob_data["representation"] - # get representation from io - representation = legacy_io.find_one({ - "type": "representation", - "_id": ObjectId(avalon_knob_data["representation"]) - }) + repre_ids.add(repre_id) + node_with_repre_id.append((node, repre_id)) - # Failsafe for not finding the representation. - if not representation: - log.warning( - "Could not find the representation on " - "node \"{}\"".format(node.name()) - ) - continue + # Skip if nothing was found + if not repre_ids: + return - # Get start frame from version data - version = legacy_io.find_one({ - "type": "version", - "_id": representation["parent"] - }) + project_name = legacy_io.active_project() + # Find representations based on found containers + repre_docs = get_representations( + project_name, + repre_ids=repre_ids, + fields=["_id", "parent"] + ) + # Store representations by id and collect version ids + repre_docs_by_id = {} + version_ids = set() + for repre_doc in repre_docs: + # Use stringed representation id to match value in containers + repre_id = str(repre_doc["_id"]) + repre_docs_by_id[repre_id] = repre_doc + version_ids.add(repre_doc["parent"]) - # get all versions in list - versions = legacy_io.find({ - "type": "version", - "parent": version["parent"] - }).distinct("name") + version_docs = get_versions( + project_name, version_ids, fields=["_id", "name", "parent"] + ) + # Store versions by id and collect subset ids + version_docs_by_id = {} + subset_ids = set() + for version_doc in version_docs: + version_docs_by_id[version_doc["_id"]] = version_doc + subset_ids.add(version_doc["parent"]) - max_version = max(versions) + # Query last versions based on subset ids + last_versions_by_subset_id = get_last_versions( + project_name, subset_ids=subset_ids, fields=["_id", "parent"] + ) - # check the available version and do match - # change color of node if not max version - if version.get("name") not in [max_version]: - node["tile_color"].setValue(int("0xd84f20ff", 16)) - else: - node["tile_color"].setValue(int("0x4ecd25ff", 16)) + # Loop through collected container nodes and their representation ids + for item in node_with_repre_id: + # Some python versions of nuke can't unfold tuple in for loop + node, repre_id = item + repre_doc = repre_docs_by_id.get(repre_id) + # Failsafe for not finding the representation. + if not repre_doc: + log.warning(( + "Could not find the representation on node \"{}\"" + ).format(node.name())) + continue + + version_id = repre_doc["parent"] + version_doc = version_docs_by_id.get(version_id) + if not version_doc: + log.warning(( + "Could not find the version on node \"{}\"" + ).format(node.name())) + continue + + # Get last version based on subset id + subset_id = version_doc["parent"] + last_version = last_versions_by_subset_id[subset_id] + # Check if last version is same as current version + if last_version["_id"] == version_doc["_id"]: + color_value = "0x4ecd25ff" + else: + color_value = "0xd84f20ff" + node["tile_color"].setValue(int(color_value, 16)) def writes_version_sync(): @@ -899,11 +942,9 @@ def format_anatomy(data): file = script_name() data["version"] = get_version_from_path(file) - project_doc = legacy_io.find_one({"type": "project"}) - asset_doc = legacy_io.find_one({ - "type": "asset", - "name": data["avalon"]["asset"] - }) + project_name = anatomy.project_name + project_doc = get_project(project_name) + asset_doc = get_asset_by_name(project_name, data["avalon"]["asset"]) task_name = os.environ["AVALON_TASK"] host_name = os.environ["AVALON_APP"] context_data = get_workdir_data( @@ -1692,12 +1733,13 @@ class WorkfileSettings(object): """ - def __init__(self, - root_node=None, - nodes=None, - **kwargs): - Context._project_doc = kwargs.get( - "project") or legacy_io.find_one({"type": "project"}) + def __init__(self, root_node=None, nodes=None, **kwargs): + project_doc = kwargs.get("project") + if project_doc is None: + project_name = legacy_io.active_project() + project_doc = get_project(project_name) + + Context._project_doc = project_doc self._asset = ( kwargs.get("asset_name") or legacy_io.Session["AVALON_ASSET"] @@ -2047,9 +2089,10 @@ class WorkfileSettings(object): def reset_resolution(self): """Set resolution to project resolution.""" log.info("Resetting resolution") - project = legacy_io.find_one({"type": "project"}) - asset = legacy_io.Session["AVALON_ASSET"] - asset = legacy_io.find_one({"name": asset, "type": "asset"}) + project_name = legacy_io.active_project() + project = get_project(project_name) + asset_name = legacy_io.Session["AVALON_ASSET"] + asset = get_asset_by_name(project_name, asset_name) asset_data = asset.get('data', {}) data = {