How to us Custom MPxData from cpp in python.

169 views
Skip to first unread message

Cameron Fulton

unread,
Feb 22, 2017, 5:16:03 PM2/22/17
to Python Programming for Autodesk Maya
Hi all,

So I have a custom data type that I create in cpp that I use for an attribute on a custom node. My custom data type is pretty straight forward:
class BlindData : public MPxData
{


 
public:
   
BlindData();
   
virtual ~BlindData();


 
/**
   * Copy the local data from a similar data object
   *
   * @param MPxData
   *   Data object to copy from.
   */

 
virtual void copy(const MPxData&);


 
MStatus
  readBinary
(istream& in, unsigned int length);


 
MStatus
  writeBinary
(ostream& out);


 
MTypeId typeId() const;


 
MString name() const;

 
static MString typeName;
 
static MTypeId id;
 
static void* creator();


  std
::vector<char> m_data;
};

And then I just use that as a type for my attr on my node
  MFnTypedAttribute tAttr;
 
MStatus stat;

    blindDataAttr
= tAttr.create("blindData",
                                 
"blind",
                                 
BlindData::id,
                                 
MObject::kNullObj,
                                 
&stat);
   addAttribute
(mPoseDataList);


So now I want to set that in Python, but I'm not sure how too. Anyone run into this before?

Cameron Fulton

unread,
Feb 22, 2017, 7:30:17 PM2/22/17
to Python Programming for Autodesk Maya
Ok, so after asking I plugged away and figured out how to do it. Here is what I found for anyone else who is interested in doing this.

def getAttrPlug(nodeName, attrName):
    selectionList
= OpenMaya.MSelectionList()
   
try:
        selectionList
.add(nodeName)
   
except:
       
return None
    dependNode
= selectionList.getDependNode(0)
    dagNode
= OpenMaya.MFnDagNode(selectionList.getDagPath(0))
    attrNode
= dagNode.attribute(attrName)
   
return OpenMaya.MPlug(dependNode,attrNode)


def createPluginData(typeId, dataList):
    pluginDataCreator
= OpenMaya.MFnPluginData()
    bdtID
= OpenMaya.MTypeId(typeId)
   
pluginDataCreator.create(bdtID)
    values
= bytearray(dataList)
    pluginDataCreator
.data().readBinary(values, len(dataList))
   
return pluginDataCreator.data()

blindNode = getAttrPlug("blind", "blindData")
blindData = createPluginData(0x00001948, [84,111,111,32,69,97,115,121])
dataHandle = blindNode.asMDataHandle()
dataHandle.setMPxData(blindData)

Reply all
Reply to author
Forward
0 new messages