Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save BigRoy/3873421057d9b8bf717b470dd28cdd4c to your computer and use it in GitHub Desktop.
Save BigRoy/3873421057d9b8bf717b470dd28cdd4c to your computer and use it in GitHub Desktop.
OpenPype v3 query the representations from a published file path
from openpype.pipeline import legacy_io, Anatomy
path = r"/path/to/representation/file.abc"
anatomy = Anatomy()
success, rootless_path = anatomy.find_root_template_from_path(path)
assert success
for result in legacy_io.find({"files.path": rootless_path, "type": "representation"}):
print(result)
@BigRoy
Copy link
Author

BigRoy commented May 23, 2023

Example for Maya to "recontainerise" references as if they were using the ReferenceLoader loader.

import json
from maya import cmds
from openpype.hosts.maya.api import lib
from openpype.hosts.maya.api import pipeline

from openpype.pipeline import (
    registered_host,
    legacy_io,
    Anatomy
)


def get_representation_from_path(path, anatomy=None):
    """Return representation from any of its absolute filepaths"""
    if anatomy is None:
        anatomy = Anatomy()
        
    _success, rootless_path = anatomy.find_root_template_from_path(path)
    return legacy_io.find_one({"files.path": rootless_path, "type": "representation"})


def is_in_a_container(node):
    """Return whether node is member of a container"""
    sets = cmds.listSets(o=node) or []
    if any(
        s for s in sets 
        if cmds.attributeQuery("id", node=s, exists=True)
        and cmds.getAttr(f"{s}.id") == "pyblish.avalon.container"
    ):
        return True
    else:
        return False
        
        

anatomy = Anatomy()
for reference in cmds.ls(references=True):
    try:
        filepath = cmds.referenceQuery(reference, filename=True, withoutCopyNumber=True)
    except Exception:
        # Ignore invalid reference nodes
        continue
    
    if is_in_a_container(reference):
        # Ignore references that are already in a container
        continue
        
    filepath = os.path.normpath(filepath)
    representation = get_representation_from_path(filepath, anatomy=anatomy)
    if representation is None:
        # No representation found
        continue
    
    namespace = cmds.referenceQuery(reference, namespace=True).strip(":")
    subset_name = representation["context"]["subset"]
    
    # Build fake context since containerise only requires the representation
    context = {"representation": representation}
    
    pipeline.containerise(
        name=subset_name,
        namespace=namespace,
        nodes=[reference],
        context=context,
        loader="ReferenceLoader"
    )

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