Maya Python: connect translate attributes of nodes loaded into text fields

rigging
maya
python

#1

Hey guys, so I’m working on a simple demo code: nothing special, I’m kindof new to python coding. It’s pretty simple: create 2 nodes and name them anything, run the python script, select your first node and hit “Load A Node” then once you do that: select the second node and hit “Load B Node”

Now where your assistance would be most appreciated, I’m trying figure out how to make “Connect Node” connect the translate attributes of the 2 nodes loaded into the text field, or just any attributes, here is the code if anyone would like 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 selection connector demo’)
column = cmds.columnLayout(adj=True)

sld_textFldA = cmds.textField(‘sld_surfaceText1’, width =240)
load_button = cmds.button( label=‘Load A Node’, c = set_textfield1)

sld_textFldB = cmds.textField(‘sld_surfaceText2’, width =240)
load_button = cmds.button( label=‘Load B Node’, c = set_textfield2)

node_button = cmds.button( label=‘Connect Node’, c = make_node)

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

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

def connect_node(_):
text_value = cmds.textField(sld_textFldA, q = True, text=True)
text_value = cmds.textField(sld_textFldB, q = True, text=True)
if text_value:
print “connected:”, cmds.connectAttr(c = set_textfield1 + ‘.translate’)
print “connected:”, cmds.connectAttr(c = set_textfield2 + ‘.translate’)

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

cmds.showWindow( window )


#2

First, wrap your code inside triple backtick marks. Like this:

39%20am

python goes inside the 3 backticks:
    and indents properly

Then you won’t have a bunch of smart quotes in your code, and we can help you better.

Also you are missing some functions in this example. “make_node” doesn’t exist.

Also, you have to define your functions before the code that you run. Any time you change the function, and run this code, it is going to call the old versions of the function first, and THEN it is going to define the function. Anyone who is running this code for the first time will get an error saying the functions don’t exist yet. Put your functions at the top.


#3

Rough, code - and fixed to translate. At a basic level you pass the source and target attributes in the connectAttr method.


def connectAttr(*args, **kwargs):

   src_text_value = cmds.textField(src_text_field, q=True, text=True)
   trg_text_value = cmds.textField(trg_text_field, q=True, text=True)

   # Very rudimentary - id advise doing attractive type checking,
   # checking connected, lock state, etc..

   if src_text_value and try_trg_text_value:

       src_text_value = "{0}.translate".format(src_text_value)
       try_text_value = "{0}.translate".format(trg_text_value)

       cmds.connectAttr(src_text_value, trg_text_value)

Also, I think:

node_button = cmds.button( label=‘Connect Node’, c = make_node)

Should be:

node_button = cmds.button( label=‘Connect Node’, c = connect_node)

-c


#4

Hey chalk, thanks for your response. This might sound incredibly noobish: but I can never get a clear answer on what *args and **kwargs means. What are they and what do they do?


#5

*args = unpacked arguments
**kwargs = unpacked keyword arguments

In python the * and ** are unpacking operators which allow you parse variable number of arguments or keyword arguments.

e.g. myFunc(1,2,3,4, a=10, b=20, c=30, …)

One really neat thing about unpacking is you can use it on lists and dictionaries when supplied to functions. E.g.


def test(a=10, b=20, c=30):
  print a, b, c

test(*[1,2,3])

# or

test(**{"a":1, "b": 2, "c":3})

Which is handy for things like serialize and deserialization.