Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save BigRoy/eb196c9d4d55752687089a73d140a2bb to your computer and use it in GitHub Desktop.
Save BigRoy/eb196c9d4d55752687089a73d140a2bb to your computer and use it in GitHub Desktop.
In AYON pipeline print a message if e.g. loaded look product was generated using a different model version than you have in your current scene.
"""
Example on using AYON's generative links to query whether the loaded products
in current workfile happen to have input products that some of the products
were also generated with and see if those versions match.
For example: You have a loaded model v002 and loaded look v001, however
that look version was created using model v001. You may want to identify that.
This script will print out a message for each product that has a version in the
inputs where another version of that input product is loaded in the scene.
The printed output will be like:
> lookMain was made with input modelMain v001 but scene has loaded modelMain v002
"""
from collections import defaultdict
import ayon_api
from ayon_core.pipeline import registered_host
# Get all containers in scene
host = registered_host()
context = host.get_current_context()
project_name: str = context["project_name"]
representation_id_to_containers = defaultdict(list)
for container in host.get_containers():
representation_id: str = container["representation"]
representation_id_to_containers[representation_id].append(container)
# Get all generative input links for the loaded product versions
representation_entities = list(ayon_api.get_representations(
project_name,
representation_ids=set(representation_id_to_containers.keys()),
fields={"versionId"}
))
scene_version_ids = {repre["versionId"] for repre in representation_entities}
inputs_by_version_id = ayon_api.get_versions_links(project_name,
scene_version_ids,
link_direction="in")
all_version_ids = set(scene_version_ids)
version_input_ids_by_version_id = {}
for version_id, inputs in inputs_by_version_id.items():
# Find generative version inputs
version_input_ids = [
inp["entityId"] for inp in inputs
if inp.get("linkType") == "generative"
and inp.get("entityType") == "version"
]
version_input_ids_by_version_id[version_id] = version_input_ids
all_version_ids.update(version_input_ids)
# Get product for all found versions
all_versions = list(
ayon_api.get_versions(project_name, version_ids=all_version_ids,
fields={"productId", "id", "name"}))
version_by_id = {version["id"]: version for version in all_versions}
product_id_by_version_id = {version["id"]: version["productId"] for version in
all_versions}
product_by_id = {product["id"]: product for product in
ayon_api.get_products(project_name, product_ids=set(
product_id_by_version_id.values()))}
# For each input version used to generate the loaded scene version,
# check if the input product is also loaded into the current scene
# and whether it matches the version it was generated with.
loaded_product_ids = {product_id_by_version_id[version_id] for version_id in
scene_version_ids}
for version_id in scene_version_ids:
input_version_ids = version_input_ids_by_version_id[version_id]
for input_version_id in input_version_ids:
input_product_id = product_id_by_version_id[input_version_id]
if input_product_id in loaded_product_ids:
# Check if versions match
for scene_version_id in scene_version_ids:
# Ignore matching version
if scene_version_id == input_version_id:
continue
scene_product_id = product_id_by_version_id[scene_version_id]
if scene_product_id != input_product_id:
continue
# define human-friendly message of the mismatch
product = product_by_id[product_id_by_version_id[version_id]]
input_product = product_by_id[input_product_id]
input_version = version_by_id[input_version_id]
scene_version = version_by_id[scene_version_id]
product_name: str = product["name"]
print(
f"{product_name} was made with input "
f"{input_product['name']} {input_version['name']} "
"but scene has loaded "
f"{input_product['name']} {scene_version['name']}"
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment