When writing a custom node using Python and trying to retrieve an attribute string with the compute method, the MDataHandle.asString() method does not return the attribute string content, but a string referencing the internal Maya MString:
def compute( self, plug, data ):
if plug != myTestNode.outputAttr:
return maya.OpenMaya.MStatus.kUnknownParameter
inputHandle = data.inputValue( myTestNode.myStringAttr )
# returns something like "_18691109_p_MString"
simpleAttrValue = inputHandle.asString()
To retrieve the real string content th MDataHandle::asString() returns a reference to an internal Maya MString object. Because it is a reference, if the user changes the contents of the string that is immediately reflected in the datablock without having to call set(). There is no way to provide equivalent functionality in Python because, among other things, Python strings are immutable: any changes create a new string and the MString class that is available in C++ is not available in Python because it has its own string implementation which makes our not needed.
However, there is a simple workaround. If you add this code you will get the string content rather than the reference to the internal string.
stringData =inputHandle.data()
stringFn = maya.OpenMaya.MFnStringData(stringData)
stringValue = stringFn.string()
print "real stringValue:", stringValue





Subscribe
Recent Comments