From 36cd349c6a5b2c38cbd58792d9758b1bcab00241 Mon Sep 17 00:00:00 2001 From: iLLiCiTiT Date: Tue, 10 Nov 2020 11:51:06 +0100 Subject: [PATCH] switch_item moved to fusion.lib as it's only place where is used --- pype/hosts/fusion/lib.py | 83 ++++++++++++++++++- .../fusion/scripts/fusion_switch_shot.py | 2 +- pype/lib/__init__.py | 2 - pype/lib/avalon_context.py | 81 ------------------ pype/scripts/fusion_switch_shot.py | 2 +- 5 files changed, 84 insertions(+), 86 deletions(-) diff --git a/pype/hosts/fusion/lib.py b/pype/hosts/fusion/lib.py index f2846c966a..77866fde9d 100644 --- a/pype/hosts/fusion/lib.py +++ b/pype/hosts/fusion/lib.py @@ -2,7 +2,7 @@ import sys from avalon.vendor.Qt import QtGui import avalon.fusion - +from avalon import io self = sys.modules[__name__] self._project = None @@ -59,3 +59,84 @@ def get_additional_data(container): return {"color": QtGui.QColor.fromRgbF(tile_color["R"], tile_color["G"], tile_color["B"])} + + +def switch_item(container, + asset_name=None, + subset_name=None, + representation_name=None): + """Switch container asset, subset or representation of a container by name. + + It'll always switch to the latest version - of course a different + approach could be implemented. + + Args: + container (dict): data of the item to switch with + asset_name (str): name of the asset + subset_name (str): name of the subset + representation_name (str): name of the representation + + Returns: + dict + + """ + + if all(not x for x in [asset_name, subset_name, representation_name]): + raise ValueError("Must have at least one change provided to switch.") + + # Collect any of current asset, subset and representation if not provided + # so we can use the original name from those. + if any(not x for x in [asset_name, subset_name, representation_name]): + _id = io.ObjectId(container["representation"]) + representation = io.find_one({"type": "representation", "_id": _id}) + version, subset, asset, project = io.parenthood(representation) + + if asset_name is None: + asset_name = asset["name"] + + if subset_name is None: + subset_name = subset["name"] + + if representation_name is None: + representation_name = representation["name"] + + # Find the new one + asset = io.find_one({ + "name": asset_name, + "type": "asset" + }) + assert asset, ("Could not find asset in the database with the name " + "'%s'" % asset_name) + + subset = io.find_one({ + "name": subset_name, + "type": "subset", + "parent": asset["_id"] + }) + assert subset, ("Could not find subset in the database with the name " + "'%s'" % subset_name) + + version = io.find_one( + { + "type": "version", + "parent": subset["_id"] + }, + sort=[('name', -1)] + ) + + assert version, "Could not find a version for {}.{}".format( + asset_name, subset_name + ) + + representation = io.find_one({ + "name": representation_name, + "type": "representation", + "parent": version["_id"]} + ) + + assert representation, ("Could not find representation in the database " + "with the name '%s'" % representation_name) + + avalon.api.switch(container, representation) + + return representation diff --git a/pype/hosts/fusion/scripts/fusion_switch_shot.py b/pype/hosts/fusion/scripts/fusion_switch_shot.py index a3f2116db8..ed657cb612 100644 --- a/pype/hosts/fusion/scripts/fusion_switch_shot.py +++ b/pype/hosts/fusion/scripts/fusion_switch_shot.py @@ -234,7 +234,7 @@ def switch(asset_name, filepath=None, new=True): representations = [] for container in containers: try: - representation = pype.switch_item( + representation = fusion_lib.switch_item( container, asset_name=asset_name) representations.append(representation) diff --git a/pype/lib/__init__.py b/pype/lib/__init__.py index 59411db9de..a93d371b2b 100644 --- a/pype/lib/__init__.py +++ b/pype/lib/__init__.py @@ -9,7 +9,6 @@ from .deprecated import ( from .avalon_context import ( is_latest, any_outdated, - switch_item, get_asset, get_hierarchy, get_linked_assets, @@ -50,7 +49,6 @@ __all__ = [ "is_latest", "any_outdated", - "switch_item", "get_asset", "get_hierarchy", "get_linked_assets", diff --git a/pype/lib/avalon_context.py b/pype/lib/avalon_context.py index 099b48967b..813d244d72 100644 --- a/pype/lib/avalon_context.py +++ b/pype/lib/avalon_context.py @@ -62,87 +62,6 @@ def any_outdated(): return False -def switch_item(container, - asset_name=None, - subset_name=None, - representation_name=None): - """Switch container asset, subset or representation of a container by name. - - It'll always switch to the latest version - of course a different - approach could be implemented. - - Args: - container (dict): data of the item to switch with - asset_name (str): name of the asset - subset_name (str): name of the subset - representation_name (str): name of the representation - - Returns: - dict - - """ - - if all(not x for x in [asset_name, subset_name, representation_name]): - raise ValueError("Must have at least one change provided to switch.") - - # Collect any of current asset, subset and representation if not provided - # so we can use the original name from those. - if any(not x for x in [asset_name, subset_name, representation_name]): - _id = io.ObjectId(container["representation"]) - representation = io.find_one({"type": "representation", "_id": _id}) - version, subset, asset, project = io.parenthood(representation) - - if asset_name is None: - asset_name = asset["name"] - - if subset_name is None: - subset_name = subset["name"] - - if representation_name is None: - representation_name = representation["name"] - - # Find the new one - asset = io.find_one({ - "name": asset_name, - "type": "asset" - }) - assert asset, ("Could not find asset in the database with the name " - "'%s'" % asset_name) - - subset = io.find_one({ - "name": subset_name, - "type": "subset", - "parent": asset["_id"] - }) - assert subset, ("Could not find subset in the database with the name " - "'%s'" % subset_name) - - version = io.find_one( - { - "type": "version", - "parent": subset["_id"] - }, - sort=[('name', -1)] - ) - - assert version, "Could not find a version for {}.{}".format( - asset_name, subset_name - ) - - representation = io.find_one({ - "name": representation_name, - "type": "representation", - "parent": version["_id"]} - ) - - assert representation, ("Could not find representation in the database " - "with the name '%s'" % representation_name) - - avalon.api.switch(container, representation) - - return representation - - def get_asset(asset_name=None): """ Returning asset document from database """ if not asset_name: diff --git a/pype/scripts/fusion_switch_shot.py b/pype/scripts/fusion_switch_shot.py index f936b7d8e0..5791220acd 100644 --- a/pype/scripts/fusion_switch_shot.py +++ b/pype/scripts/fusion_switch_shot.py @@ -191,7 +191,7 @@ def switch(asset_name, filepath=None, new=True): representations = [] for container in containers: try: - representation = pype.switch_item(container, + representation = fusion_lib.switch_item(container, asset_name=asset_name) representations.append(representation) except Exception as e: