Search code examples
pythonmayaautodeskpymel

Maya Python Assistance requested: connect attributes based on textfield contents


Background:

So thanks to the help of the user Theodox I was able to figure out how to create nodes in the node editor with name prefixs based on the joint you load into the selection field.

However: I want to take it a step further and make it so not only are the nodes created with joint name prefixes: but they will also connect the translates of the nodes created via connectAttr.

The problem?

I currently lack the knowledge to make this work and I cant find anything online, so any assistance would be most appreciated

Code:

I've tried lines of code like:

cmds.connectAttr( sel[0] + '.rotate', sel[1] + '.rotate' )

or

cmds.connectAttr( n=text_value +'_firstGuy', n=text_value +'_secondGuy' )

I know I can create seperate textfields and buttons to load the nodes and connect them thatway, but with the shortcut I'm coding, all the nodes I've created would be too many to load so it would be easier if I could just create the nodes with their connections, I'll post the code below for anyone willing to take a crack at it:

import maya.cmds as cmds
if cmds.window(window, exists =True):
    cmds.deleteUI(window)

window = cmds.window(title='DS Node Connector demo')
column = cmds.columnLayout(adj=True)

def set_textfield(_):
    sel = cmds.ls(selection=True)
    cmds.textField(sld_textFld, edit=True, text=sel[0])

def nodebuilder(_):
    text_value = cmds.textField(sld_textFld, q = True, text=True)
    if text_value:
        print "created:", cmds.createNode( 'transform', n=text_value +'_firstGuy' )
        print "created:", cmds.createNode( 'transform', n=text_value +'_secondGuy' )

         # Connect the translation of two nodes together
        print "connected:", cmds.connectAttr (sel[0] +'.t', sel[1] + '.t') 
        #print "connected:", cmds.connectAttr( '_firstGuy.t', '_secondGuy.translate' )

        # Connect the rotation of one node to the override colour
        # of a second node.
        #print "connected:", cmds.connectAttr( '_firstGuy.rotate', '_secondGuy.overrideColor' )

    else:
        cmds.warning("select an object and add it to the window first!")




sld_textFld = cmds.textField('sld_surfaceTextHJ', width =240)
load_button = cmds.button( label='Load Helper Joint', c = set_textfield)
node_button = cmds.button( label='Make Node', c = nodebuilder)

cmds.showWindow(window)  

My expected result:

Upon hitting "make node" after loading a joint after hitting "load helper joint" that once "_firstGuy" and "_secondGuy" are created with the name prefix of the joint, that their translates will be connected. It helps to have the node editor open to test this.


Solution

  • Okay, you want to connect the translate attributes of the two new created nodes. Usually connecting attributes works like this:

    connectAttr(<attributeA>, <attributeB>)
    

    Where attributeA is something like "NodeA.translate". So what you need is the name of your first node and the attribute name, in your case:

    nodeNameA = text_value + "_firstGuy"
    nodeNameB = text_value + "_secondGuy"
    

    The attribute is the well known "translate", so the full attribute name would be:

    attributeNameA = nodeNameA + ".translate"
    attriubteNameB = nodeNameB + ".translate"
    

    And the full command is now:

    connectAttr(attributeNameA, attributeNameB)
    

    The only problem here is that Maya automatically renames objects if there is already one with the same name. So a more save way it to use the created name this way:

    firstGuyNode = cmds.createNode( 'transform', n=text_value +'_firstGuy' )