0

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.

Dasteel
  • 101
  • 18
  • Hi and welcome to SO. I'm sorry but I did not completely understood what you want to do. You create two nodes and you want to connect translation and rotation attributes, is that correct? Unfortunately your code does not work. It has logical errors as well as syntax errors. If you copy and paste it, it fails. Please try to build a working example. – haggi krey Dec 21 '18 at 09:02
  • Hey, sorry @haggikrey I'm still trying to figure out posting code here, but I think I got it figured out. 1. create a joint and open the node editor: name the joint anything 2. run the script and hit "load helper joint" 3. hit "make node" this should create 2 nodes with the name of the joint you loaded prefixing the node names, so if you loaded a joint named "A_Joint" and hit "make node" it will create 2 nodes: "A_Joint_firstGuy" and "A_Joint_secondGuy" But I want the script to go further and connect the translate of the 2 created nodes like one would do in the node editor, – Dasteel Dec 21 '18 at 18:48

1 Answers1

0

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' )
haggi krey
  • 1,885
  • 1
  • 7
  • 9