Add support for Look Assigner to assign looks in aiStandin for USD files based on cbId attributes in the USD file.

- For this to currently work the transform and shape should *not* be merged into a single Prim inside USD because otherwise the unique `cbId` between Transform and Shape node will be lost.
This commit is contained in:
Roy Nieterau 2023-09-06 00:02:39 +02:00
parent 4a861a6bfc
commit a61f7ac799
2 changed files with 46 additions and 0 deletions

View file

@ -10,6 +10,7 @@ from openpype.client import get_last_version_by_subset_name
from openpype.hosts.maya import api
from . import lib
from .alembic import get_alembic_ids_cache
from .usd import is_usd_lib_supported, get_usd_ids_cache
log = logging.getLogger(__name__)
@ -74,6 +75,13 @@ def get_nodes_by_id(standin):
# Support alembic files directly
return get_alembic_ids_cache(path)
elif (
is_usd_lib_supported and
any(path.endswith(ext) for ext in [".usd", ".usda", ".usdc"])
):
# Support usd files directly
return get_usd_ids_cache(path)
json_path = None
for f in os.listdir(os.path.dirname(path)):
if f.endswith(".json"):

View file

@ -0,0 +1,38 @@
from collections import defaultdict
try:
from pxr import Usd
is_usd_lib_supported = True
except ImportError:
is_usd_lib_supported = False
def get_usd_ids_cache(path):
# type: (str) -> dict
"""Build a id to node mapping in a USD file.
Nodes without IDs are ignored.
Returns:
dict: Mapping of id to nodes in the USD file.
"""
if not is_usd_lib_supported:
raise RuntimeError("No pxr.Usd python library available.")
stage = Usd.Stage.Open(path)
ids = {}
for prim in stage.Traverse():
attr = prim.GetAttribute("userProperties:cbId")
if not attr.IsValid():
continue
path = str(prim.GetPath())
value = attr.Get()
if not value:
continue
ids[path] = value
cache = defaultdict(list)
for path, value in ids.items():
cache[value].append(path)
return dict(cache)