Skip to content
This repository has been archived by the owner on Sep 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request #642 from pypeclub/feature/get_latest_version_in_lib
Browse files Browse the repository at this point in the history
Get latest version in lib
  • Loading branch information
mkolar authored Oct 16, 2020
2 parents 18502ce + a80f161 commit 7ffe932
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 36 deletions.
7 changes: 5 additions & 2 deletions pype/hosts/harmony/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,11 @@ def check_inventory():


def application_launch():
ensure_scene_settings()
check_inventory()
# FIXME: This is breaking server <-> client communication.
# It is now moved so it it manually called.
# ensure_scene_settings()
# check_inventory()
pass


def export_template(backdrops, nodes, filepath):
Expand Down
81 changes: 58 additions & 23 deletions pype/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1434,41 +1434,76 @@ def source_hash(filepath, *args):
return "|".join([file_name, time, size] + list(args)).replace(".", ",")


def get_latest_version(asset_name, subset_name):
def get_latest_version(asset_name, subset_name, dbcon=None, project_name=None):
"""Retrieve latest version from `asset_name`, and `subset_name`.
Do not use if you want to query more than 5 latest versions as this method
query 3 times to mongo for each call. For those cases is better to use
more efficient way, e.g. with help of aggregations.
Args:
asset_name (str): Name of asset.
subset_name (str): Name of subset.
dbcon (avalon.mongodb.AvalonMongoDB, optional): Avalon Mongo connection
with Session.
project_name (str, optional): Find latest version in specific project.
Returns:
None: If asset, subset or version were not found.
dict: Last version document for entered .
"""
# Get asset
asset_name = io.find_one(
{"type": "asset", "name": asset_name}, projection={"name": True}
)

subset = io.find_one(
{"type": "subset", "name": subset_name, "parent": asset_name["_id"]},
projection={"_id": True, "name": True},
if not dbcon:
log.debug("Using `avalon.io` for query.")
dbcon = io
# Make sure is installed
io.install()

if project_name and project_name != dbcon.Session.get("AVALON_PROJECT"):
# `avalon.io` has only `_database` attribute
# but `AvalonMongoDB` has `database`
database = getattr(dbcon, "database", dbcon._database)
collection = database[project_name]
else:
project_name = dbcon.Session.get("AVALON_PROJECT")
collection = dbcon

log.debug((
"Getting latest version for Project: \"{}\" Asset: \"{}\""
" and Subset: \"{}\""
).format(project_name, asset_name, subset_name))

# Query asset document id by asset name
asset_doc = collection.find_one(
{"type": "asset", "name": asset_name},
{"_id": True}
)
if not asset_doc:
log.info(
"Asset \"{}\" was not found in Database.".format(asset_name)
)
return None

# Check if subsets actually exists.
assert subset, "No subsets found."

# Get version
version_projection = {
"name": True,
"parent": True,
}
subset_doc = collection.find_one(
{"type": "subset", "name": subset_name, "parent": asset_doc["_id"]},
{"_id": True}
)
if not subset_doc:
log.info(
"Subset \"{}\" was not found in Database.".format(subset_name)
)
return None

version = io.find_one(
{"type": "version", "parent": subset["_id"]},
projection=version_projection,
version_doc = collection.find_one(
{"type": "version", "parent": subset_doc["_id"]},
sort=[("name", -1)],
)

assert version, "No version found, this is a bug"

return version
if not version_doc:
log.info(
"Subset \"{}\" does not have any version yet.".format(subset_name)
)
return None
return version_doc


class ApplicationLaunchFailed(Exception):
Expand Down
3 changes: 2 additions & 1 deletion pype/plugins/global/publish/submit_publish_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,8 @@ def process(self, instance):
"resolutionHeight": data.get("resolutionHeight", 1080),
"multipartExr": data.get("multipartExr", False),
"jobBatchName": data.get("jobBatchName", ""),
"review": data.get("review", True)
"review": data.get("review", True),
"audio": data.get("audio", [])
}

if "prerender" in instance.data["families"]:
Expand Down
24 changes: 14 additions & 10 deletions pype/plugins/nuke/publish/collect_review.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,24 @@ def process(self, instance):
if not node["review"].value():
return

# Add audio to instance if it exists.
try:
version = pype.api.get_latest_version(
instance.context.data["assetEntity"]["name"], "audioMain"
)
representation = io.find_one(
{"type": "representation", "parent": version["_id"]}
# * Add audio to instance if exists.
# Find latest versions document
version_doc = pype.api.get_latest_version(
instance.context.data["assetEntity"]["name"], "audioMain"
)
repre_doc = None
if version_doc:
# Try to find it's representation (Expected there is only one)
repre_doc = io.find_one(
{"type": "representation", "parent": version_doc["_id"]}
)

# Add audio to instance if representation was found
if repre_doc:
instance.data["audio"] = [{
"offset": 0,
"filename": api.get_representation_path(representation)
"filename": api.get_representation_path(repre_doc)
}]
except AssertionError:
pass

instance.data["families"].append("review")
instance.data['families'].append('ftrack')
Expand Down

0 comments on commit 7ffe932

Please sign in to comment.