MPxData casting in Python API 1.0
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
When creating custom attribute data types in a plugin, we subclass MPxData from OpenMaya.OpenMayaMPx and override the default methods, while adding class vars to store custom data and new methods for setting and retrieving the data. What I can't figure out, and what isn't demonstrated in the python code example blindDoubleData.py, is how you retrieve the data stored in an attribute that uses your custom data type later on.
The example code shows how to retrieve the data from a plug here:
# Now try to retrieve the value off the plug as an MObject.
#
try:
sData = plug.asMObject()
except:
error("Error getting value off plug")
continue
# Convert the data back to MPxData.
#
pdFn = OpenMaya.MFnPluginData(sData)
data = pdFn.constData()However, the class of the object returned by MFnPluginData() is always the base class MPxData, and not the custom subclass we defined for the data type. That being the case, how can we access the data when the base class doesn't define the variable the data is stored in, or the methods added to set and get that data?
The C++ documentation for MFnPluginData() suggests that the returned MPxData object just be recast to your custom subclass type. That may be trivial in C++, but in Python you can't simply cast objects to a child class type. The closest thing in Python is assigning a new value to the objects __class__ variable, but that doesn't seem to have the desired effect here.