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/"
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"}):
Copy link

BigRoy commented May 17, 2023

There's another neat thing about pymongo too. You can search by regex if you wanted.

from openpype.pipeline import legacy_io
import re

folder = "{root[work]}/project/asset/char_hero/publish/model/modelMain/v001/"
regex = re.compile(re.escape(folder) + ".*")

for result in legacy_io.find({"files.path": regex, "type": "representation"}):

So that would also allow you to do the lookup based on the folder like above.

Copy link

BigRoy commented May 23, 2023

Example from Felix (Normaal Animation) as shared on Ynput discord here:

That's code for Blender, but could be used in other cases I think. The idea is to retrieve the representation from the filename and resolve the path with the local root. That's for multi-site purpose, when broken.

from pathlib import Path
import re

import bpy

from openpype.pipeline.anatomy import Anatomy
from openpype.pipeline.legacy_io import Session, find_one

print("Conforming lib filepaths...")

paths_fixed = []

# Retrieve representations by file name
for lib in
    regex = re.compile(f"{re.escape(Path(lib.filepath).name)}$")
    repre_doc = find_one({"files.path": regex, "type": "representation"})

    anatomy = Anatomy()
    remapped_path = Path(anatomy.fill_root(repre_doc["files"][0]["path"])).resolve()
    if remapped_path != Path(lib.filepath).resolve():
        paths_fixed.append((lib.filepath, remapped_path))
        lib.filepath = remapped_path

Copy link

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 (

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
        return False

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

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