How to copy connections from Node editor and convert them into a script

hi everyone, here I am with another question related to Maya pymel.

Is it possible to automatically create a node editor structure like this through pymel?

Is there any way to copy the whole thing and turn it directly into a script?

Of course it’s possible, the question is how, I assume.
Not sure what level your scripting skills are,
but you are here so I’ll assume you know some.

For reading the node graphs, look up these commands

ls(sl=True) # list selected node(s)
listConnections() # list a nodes connections

I’d save the result into some kind of dictionary or list of dictionaries

nodes=[
    {
        "node_name":"mulitplyDivide_l_eye_scale",
        "node_type":"mulitplyDivide",
        "connections":{
            "offsetScaleXY.outFloatX":"input2X",
            "offsetScaleXY.outFloatY":"input2Y",
            "OutputX":"l_eye_final_UV_offset.input2D[0].input2Dx",
            "OutputY":"l_eye_final_UV_offset.input2D[0].input2Dy"
        }
    },
    {
        "node_name": "l_eye_offset_X",
        "node_type": "someNodeType",
        "connections": {
            "OutFloat": "l_eye_final_UV_offset.input2D[1].input2Dx",
            ...
        }
    }
    ...
]

Don’t trust the attr names in this example dictionary, it’s off the cuff. It needs to contain all the values needed to correctly rebuild the graph.

Then I’d use the dictionary to drive functions to build a node graph
For building the node graphs, look up these commands

createNode()
connectAttr()

thanks for the answer! Yes surely is possible remap the whole structure of a node into a dictionary,
the real question is:
Is there any way (command, library, etc…) to do it automatically.

There is no ‘out of the box’ solution, if that is what you are asking for.
Looks like @VVVSLAVA proved me wrong. I didn’t understand that exporting to .ma was an option

Yes, such a commands exists :slight_smile:
For example:

  1. Select Nodes in Hypershade
  2. FileExport Selection Network
  3. Set file name (for example: m_020), files of type: Maya ASCII(*.ma)
  4. FileImport...

If you need a script, then the m_020.ma file is essentially a MEL script :slight_smile:

gsh_01.png

Good luck!

1 Like

WOW Thanks for the extensive explanation!