Autodesk / Autodesk/maya-usd

Expose Maya Attribute to USD attribute conversion to maya usd lib python API

Open
#3,313 2 comments 2 reactions 0 assignees View on GitHub
enhancement
Dominant language
Wolfram Language
Stars
905
Forks
223
Avg merge
2d 9h
Merged PRs (30d)
17

Description

**Is your feature request related to a problem? Please describe.**

It'd be great if we can expose the [`UsdMayaWriteUtil::GetOrCreateUsdAttr`](https://github.com/Autodesk/maya-usd/blob/9ae146caea140154ca536eea6e4b8ea4715fe889/lib/mayaUsd/fileio/utils/writeUtil.cpp#L151) logic to the `mayaUsd.lib` python API or one of the lower level methods like just the type conversion as [here](https://github.com/Autodesk/maya-usd/blob/9ae146caea140154ca536eea6e4b8ea4715fe889/lib/mayaUsd/utils/converter.cpp#L1160).

By doing so it might make it easier to write custom Python Export chasers to write out certain attributes.

Imagine a chaser (like the "alembic" chaser) that mimics the `-attr` and `-attrPrefix` flags Maya's Alembic exports had.
Here's a very quick and dirty boilerplate:
```python
import mayaUsd.lib as mayaUsdLib
from pxr import Usd
import maya.api.OpenMaya as om

def get_mplug(plug):
sel = om.MSelectionList()
sel.add(plug)
return sel.getPlug(0)

class CustomAttributesChaser(mayaUsdLib.ExportChaser):

def __init__(self, factoryContext, *args, **kwargs):
super(CustomAttributesChaser, self).__init__(factoryContext, *args, **kwargs)

# TODO: Actually parse the attribute names from the chaser args
self._attrs = ["my_attribute", "my_prefix*"]

self._stage = factoryContext.GetStage()
self._dag_to_usd = {str(item.key()): item.data() for item in factoryContext.GetDagToUsdMap()}

# def ExportDefault(self):
# return self.ExportFrame(Usd.TimeCode.Default())

# def ExportFrame(self, frame):
# return True

def PostExport(self):

stage = self._stage
for maya_node, usd_path in self._dag_to_usd.items():
attrs = cmds.listAttr(maya_node, st=self._attrs)
if not attrs:
continue

prim = stage.GetPrimAtPath(usd_path)
for attr in attrs:
plug = get_mplug(f"{maya_node}.{attr}")

# TODO: Expose this
#mayaUsdLib.GetOrCreateUsdAttr(
# attrPlug=plug,
# usdPrim=prim,
# attrName=attr,
# custom=False,
# translateMayaDoubleToUsdSinglePrecision=True
#)

# And then actually setting the values..

return True

# Unregister and register again for easy development
mayaUsdLib.ExportChaser.Unregister(CustomAttributesChaser, "attrib")
mayaUsdLib.ExportChaser.Register(CustomAttributesChaser, "attrib")

# Perform a test export with the chaser
from maya import cmds
cmds.loadPlugin("mayaUsdPlugin", quiet=True)
cmds.mayaUSDExport(file="E:/test.usd",
frameRange=(1, 5),
frameStride=1.0,
chaser=["attrib"],
selection=True)
```

**Describe the solution you'd like**

Would be great if there was a trivial way to write a matching attribute (and maybe even write samples for a given frame, or multiple frames?) from that Maya attribute to the USD attribute in an easy way.

**Describe alternatives you've considered**

Writing and maintaining the (quite complex?) code myself..

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by reading the referenced logic in lib/mayaUsd/fileio/utils/writeUtil.cpp and the type-conversion code in lib/mayaUsd/utils/converter.cpp. Check how the mayaUsd.lib Python API exposes related functionality and how ExportChaser is used. Done means a custom Python export chaser can convert and write Maya attributes to USD attributes, including values for export frames.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, python
Domain
api, tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.