I'm finally in the end game of the Helper Joint script I'm working on and only 1 last problem stands in my way. Here is how the script is supposed to work. Create a joint, name it "Parent_Joint" then create a "multDoubleLinear" node in the node editor, name it "bob, Hit "Load Parent Joint" after selecting the joint you created and hit add attribute. In an ideal world where I'm smarter on this subject, The custom attribute added to the joint would be plugged into bob's "input1" instead I get an error saying "# Error: The source attribute 'Parent_Joint_HelperJntAttr' cannot be found."
In terms of what I've already tried, I put connectAttr beneath addAttr as common sense would dictate that first the attribute must be created before it's connected: but despite this it's just refusing to connect. I know the fault doesn't fall on the "bob.input1" node, because it only brings up the prefixed attribute name for 'Parent_Joint_HelperJntAttr: so my guess is it's just simply my lack of knowledge in writing this particular procedure.
import maya.cmds as cmds
if cmds.window(window, exists =True):
cmds.deleteUI(window)
window = cmds.window(title='DS Attribute adder')
column = cmds.columnLayout(adj=True)
sld_textFld = cmds.textField('sld_surfaceTextHJ', width =240)
def set_textfield(_):
sel = cmds.ls(selection=True)
cmds.textField(sld_textFld, edit=True, text=sel[0])
load_button = cmds.button( label='Load Parent Joint', c = set_textfield)
def add_Attribute(_):
text_value = cmds.textField(sld_textFld, q = True, text=True)
if text_value:
print "attrAdded:"
cmds.addAttr(ln=text_value +'_HelperJntAttr', defaultValue=5.0, minValue=0, attributeType='float', keyable=True)
cmds.connectAttr( text_value +"_HelperJntAttr", 'bob.input1')
else:
cmds.warning("select an object and add it to the window first!")
node_button = cmds.button( label='add attribute', c = add_Attribute)
cmds.showWindow(window)
I know how to use the connectAttr command on default attributes in maya, but where I fall flat is custom attributes. My hope is I come out of this knowing how to write code that both creates and connects the custom attributes of the joint. Thank you in advance for your help
The way you were using addAttr
, it was including the joint's name in the attribute name. Attributes are separated with a .
, not an _
, so your connectAttr
also fails because of that.
You also need to initialize your window
variable to some default value or it fails on the line where you check if it exists (but window
isn't defined at that point).
Here is the script adding the attribute and connecting it as expected:
import maya.cmds as cmds
window = "" # Need to initialize this variable first or it crashes on next line.
if cmds.window(window, exists =True):
cmds.deleteUI(window)
window = cmds.window(title='DS Attribute adder')
column = cmds.columnLayout(adj=True)
sld_textFld = cmds.textField('sld_surfaceTextHJ', width =240)
def set_textfield(_):
sel = cmds.ls(selection=True)
cmds.textField(sld_textFld, edit=True, text=sel[0])
load_button = cmds.button( label='Load Parent Joint', c = set_textfield)
def add_Attribute(_):
text_value = cmds.textField(sld_textFld, q = True, text=True)
if text_value:
print "attrAdded:"
# Attribute must be created this way.
cmds.addAttr(text_value, ln='HelperJntAttr', defaultValue=5.0, minValue=0, attributeType='float', keyable=True)
# Attribute is separated with a dot.
cmds.connectAttr(text_value + ".HelperJntAttr", 'bob.input1')
else:
cmds.warning("select an object and add it to the window first!")
node_button = cmds.button( label='add attribute', c = add_Attribute)
cmds.showWindow(window)