Maya export selected nodes as mel commands

Hello everyone!
I stuck with some problem, maybe somebody worked with something simular.
I need to export given nodes as mel commands, so later a could recreate them with same changed attribute values.
Exactly the same operation happens during maya saving scene in .ma,
for example part of the code, which stored in .ma file with parent constrain creation:

createNode parentConstraint -n “pSphere1_parentConstraint1” -p “sphere:pSphere1”;
rename -uid “6794455A-474A-7200-4BE9-1CB07F4EDCF0”;
addAttr -dcb 0 -ci true -k true -sn “w0” -ln “pCube1W0” -dv 1 -min 0 -at “double”;
setAttr -k on “.nds”;
setAttr -k off “.v”;
setAttr -k off “.tx”;

I think somewhere in PythonApi or maybe cmds there is lies some command which can generate for me the same code for given node.
It consists with create node, of course it’s type and set atr for all non-default (all changed) attributes.
I think somewhere this type of command exists, but i can’t find one)

I founded workaround, where i export selected nodes to .ma file and later parse it for data i need (and it is working) or otherwise
i can try to find node type, find all changed params and generate this type of code on my own,
but both it is sort of a workaround, which is not greatest solution for this task, in my opinion, i think some where it already lies inside maya.

Huge thanks in advance!

I think your solution of exporting to a temp file is pretty decent actually. MPlug objects have a method of getSetAttrCmds() Help , but I’ve never found a way to get a nice string for the node creation - although of course at that point it would be easy to format your own by querying the name, type, parent etc.

1 Like

Thank you for reply! I will check in docs about MPlug function, looks very intresting