Duplicate Node on maya c++ Api


#1

Hello guys !

I’m trying to duplicate a node on the maya api without using the ‘duplicate’ command, my attempt was to create the same node and call MPxNode::copyInternalData on it, but the data don’t seem to carry on the new node.

here is my attempt :

MObject result = MDagModifierHandler::GetDag()->createNode(node->typeId());
MFnDependencyNode newNode(result);
FRBaseNode *ghostNode((FRBaseNode *)newNode.userNode());
ghostNode->copyInternalData(node);



thanks for any advise !


#2

After more doc reading and testing, I think there is simply no any maya function that handle a node duplication, so we have to make our own.

if that interest anybody, here is the process done rougly :

	
	MFnDependencyNode originalNode(originalNodeMObject);

	MObject newNode = MDagModifierHandler::GetDag()->createNode(originalNode.type());

	for(int i = 0; i < originalNode.attributeCount(); i++)
	{
		MObject oldAttr = originalNode.attribute(i);
		MFnAttribute MfnOldAttr(oldAttr);

		MPlug oriPlug;
		MPlug newPlug;
                //These 2 function are personnalised helper that simply return MPlug based on the inputed node and plug name
		GetPlug(node->thisMObject(), MfnOldAttr.name(), &oriPlug);
		GetPlug(newNode, MfnOldAttr.name(), &newPlug);

		MDataHandle oriVal(oriPlug.asMDataHandle());
		MDataHandle newVal(newPlug.asMDataHandle());

		MStatus stat = newVal.copy(oriVal);
		newPlug.setMDataHandle(newVal);

		oriPlug.destructHandle(oriVal);
		newPlug.destructHandle(newVal);
	}

this code is ofc incomplete, do not handle dynamic attribute, and do not duplicate connection.
if someone have a better way of doing this, i’ll be happy to hear about it !