MPxData casting in Python API 1.0

MPxData casting in Python API 1.0

Anonymous
Not applicable
619 Views
1 Reply
Message 1 of 2

MPxData casting in Python API 1.0

Anonymous
Not applicable

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.

0 Likes
620 Views
1 Reply
Reply (1)
Message 2 of 2

cheng_xi_li
Autodesk Support
Autodesk Support

Hi,

 

I think the sample uses a dictionary to store the data and use it to access data from other MPxData created by subclasses.

 

If you look at the copy function:

 

	def copy(self, other):
		# Cannot convert other to self.  Use a dictionary
		# to hold the information we need.
		self.__fValue = fValueDictionary[OpenMayaMPx.asHashable(other)]

It doesn't cast other class but access data from the global dictionary.

So I think you'll need to create method to get the data from the dictionary in the command.

 

BTW: If you could use Python API 2.0, it seems you could get the subclass directly.

 

Yours,

Li

0 Likes