Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save BigRoy/bce9569fc7e60090c23ea2693903db6b to your computer and use it in GitHub Desktop.

Select an option

Save BigRoy/bce9569fc7e60090c23ea2693903db6b to your computer and use it in GitHub Desktop.
Python example on how to manipulate USD_UserExportedAttributesJson attribute on nodes to define how Maya USD exports custom attributes to the USD attribute names
from typing import Any
from maya import cmds
import json
ATTR: str = "USD_UserExportedAttributesJson"
def add_explicit_attribute_export_name(
node: str,
maya_attribute_name: str,
usd_attribute_name: str
):
"""Set usd export attribute name for the maya attribute on the maya node."""
# Ensure attribute exists
if not cmds.attributeQuery(ATTR, node=node, exists=True):
cmds.addAttr(node, ln=ATTR, dt="string")
plug: str = f"{node}.{ATTR}"
json_str: str = cmds.getAttr(plug) or "{}"
data: dict[str, dict[str, Any]] = json.loads(json_str)
data[maya_attribute_name] = {"usdAttrName": usd_attribute_name}
json_str: str = json.dumps(data)
cmds.setAttr(plug, json_str, type="string")
def remove_explicit_attribute_mapping(node):
"""Remove USD_UserExportedAttributesJson attribute from node."""
if not cmds.attributeQuery(ATTR, node=node, exists=True):
cmds.deleteAttr(f"{node}.{ATTR}")
for node in cmds.ls(selection=True):
add_explicit_attribute_export_name(
node,
maya_attribute_name="myAttribute",
usd_attribute_name="userProperties:myAttribute"
)
add_explicit_attribute_export_name(
node,
maya_attribute_name="myOtherAttribute",
usd_attribute_name="userProperties:myOtherAttribute"
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment