Skip to content

Code Snippets

Roy Nieterau edited this page Jul 26, 2019 · 12 revisions

Interacting with Avalon database

The avalon database uses MongoDB. In Avalon's python api it exposes a thin wrapper around pymongo through avalon.io. Here are some example code snippets of how you could interact with the database.

Search asset by name with regex

Pymongo supports searching by regex and even allows to send a re.compile returned object as the value for a query directly.

import avalon.io as io
import re

# Compile a regex pattern
pattern = re.compile("character_.*")

# Search in avalon database by pattern
for asset in io.find({"name": pattern, "type": "asset"}):
    print(asset)

List all distinct values for a specific key

Using MongoDB it's trivial to list the unique set of values for a specific key using distinct.

import avalon.io as io

# Get all families that are stored in all versions in version["data"]["families"]
families = io.distinct("data.families", {"type": "version"})

Get the latest version of a publish for subset modelDefault for asset hero_x

import avalon.io as io

def get_latest_version(asset_name, subset_name):
    # Get asset and its subset by name
    asset = io.find_one({"name": asset_name, 
                                    "type": "asset"})
    subset = io.find_one({"name": subset_name, 
                                     "type": "subset", 
                                     "parent": asset["_id"]})
    
    # Get last version by using io.find_one() and sorting in reverse by name
    version = io.find_one({"type": "version", 
                                      "parent": subset["_id"]},
                                    sort=[("name", -1)])
                                    
    return version

version = get_latest_version("hero_x", "modelDefault")

Load a representation with a specific loader through avalon.api

Avalon comes with a Loader tool that makes it easy for artists to load publishes. However, this snippet shows how to do it through code for when you need some automated loading or load assets in bulk, e.g. for a scene builder.

import avalon.api as api

# Get the loader plug-in by its name
name = "AlembicLoader"
loader = next(loader for loader in api.discover(api.Loader) 
                     if loader.__name__ = name)
assert loader, "Loader not found.."

# Assuming you already have the representation id you can load it directly
api.load(loader, representation_id)

List the available Loaders for a representation

You can easily filter the discoverable Loaders to only those that are compatible with a specific representation.

import avalon.api as api

loaders = list(api.discover(api.Loader))
compatible_loaders = api.loaders_from_representation(loaders, representation_id)
Checking compatibility of a single loader

However, if you have a single Loader and want to check whether it's compatible you can do so too. This function however is not exposed in avalon.api as such it is not enforced to keep its function signature exactly backwards compatible in the future. It could be stable for use, however the behavior of the functions might change in the future.

import avalon.pipeline

context = avalon.pipeline.get_representation_context(representation_id)
is_compatible = avalon.pipeline.is_compatible_loader(loader, context)

As such, to ensure a stable future, use the avalon.api instead:

import avalon.api as api

compatible_loaders = api.loaders_from_representation([loader], representation_id)
is_compatible = any(compatible_loaders)
Clone this wiki locally