Skip to content

Instantly share code, notes, and snippets.

@BigRoy
Created February 8, 2021 16:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BigRoy/c7bd3d76b0f21df21518e65dabe9afdc to your computer and use it in GitHub Desktop.
Save BigRoy/c7bd3d76b0f21df21518e65dabe9afdc to your computer and use it in GitHub Desktop.
Avalon example for colorbleed-config on how to load latest version of an asset's `modelDefault` solely from the `cbId` attribute.
# Example on how to load latest `modelDefault` of an asset by `cbId` value using `AbcLoader` (alembic)
import avalon.io as io
import avalon.api as api
from maya import cmds
node = "my_node"
cbid = cmds.getAttr("%s.cbId" % node)
# Get asset object id from cbId
object_id = cbid.split(":", 1)[0] # The part before the : is the asset id
object_id = io.ObjectId(object_id) # Make sure it's a pymongo.ObjectId to query the database
asset = io.find_one({"_id": object_id,
"type": "asset"})
subset = io.find_one({"name": "modelDefault",
"parent": asset["_id"],
"type": "subset"})
version = io.find_one({"parent": subset["_id"],
"type": "version"}, sort=[("name", -1)])
representation = io.find_one({"name": "abc",
"parent": version["_id"],
"type": "representation"})
# From all loader get the AbcLoader
loaders = api.discover(api.Loader)
loader = {loader.__name__: loader for loader in loaders}.get("AbcLoader")
api.load(loader, representation)
@BigRoy
Copy link
Author

BigRoy commented Feb 8, 2021

Similar example for Houdini data:

import hou
import avalon.io as io
import avalon.api as api


def load_by_cbid(cbid, subset_name, loader_name):

    # Get asset object id from cbId
    object_id = cbid.split(":", 1)[0]    # The part before the : is the asset id
    object_id = io.ObjectId(object_id)   # Make sure it's a pymongo.ObjectId to query the database
    
    asset = io.find_one({"_id": object_id, 
                         "type": "asset"})
    subset = io.find_one({"name": subset_name,
                          "parent": asset["_id"],    
                          "type": "subset"})
    version = io.find_one({"parent": subset["_id"],
                           "type": "version"}, sort=[("name", -1)])
    representation = io.find_one({"name": "abc",
                                  "parent": version["_id"],
                                  "type": "representation"})
                                  
    # From all loader get the AbcLoader
    loaders = api.discover(api.Loader)
    loader = {loader.__name__: loader for loader in loaders}.get(loader_name)
    return api.load(loader, representation)
    
    
node = hou.selectedNodes()[0]
geo = node.geometry()
attrib = geo.findPrimAttrib("cbId")
if not attrib:
    raise RuntimeError("Selected node has no `cbId` primitive attribute.")
    
values = geo.primStringAttribValues("cbId")
cbids = set(value.split(":", 1)[0] for value in values)
for cbid in cbids:
    load_by_cbid(cbid, subset_name="modelDefault", loader_name="AbcLoader")

@BigRoy
Copy link
Author

BigRoy commented Feb 8, 2021

To not use a Loader but just get the Representation path use avalon.api.get_representation_path()

import avalon.api as api
path = api.get_representation_path(representation)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment