【问题标题】:How do I connect custom Attributes to node inputs?如何将自定义属性连接到节点输入?
【发布时间】:2019-05-26 02:48:01
【问题描述】:

我终于完成了我正在处理的 Helper Joint 脚本的最终游戏,只有最后一个问题阻碍了我。这是脚本应该如何工作的。创建一个关节,将其命名为“Parent_Joint”,然后在节点编辑器中创建一个“multDoubleLinear”节点,将其命名为“bob,在选择您创建的关节并点击添加属性后点击“Load Parent Joint”。在理想的世界中,我'在这个问题上我更聪明,添加到关节的自定义属性将插入 bob 的“input1”,而不是我收到一条错误消息“# Error: The source attribute 'Parent_Joint_HelperJntAttr' can be found.”

就我已经尝试过的而言,我将 connectAttr 放在 addAttr 之下,因为常识会规定必须首先创建属性,然后才能连接:但尽管如此,它只是拒绝连接。我知道故障不在“bob.input1”节点上,因为它只显示了 'Parent_Joint_HelperJntAttr: 的前缀属性名称:所以我猜这只是我在编写这个特定程序时缺乏知识。

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)

我知道如何在 maya 中对默认属性使用 connectAttr 命令,但我遇到的问题是自定义属性。我的希望是我知道如何编写代码来创建和连接关节的自定义属性。提前感谢您的帮助

【问题讨论】:

    标签: python maya autodesk


    【解决方案1】:

    您使用addAttr 的方式是在属性名称中包含关节的名称。属性用. 分隔,而不是_,因此您的connectAttr 也会因此而失败。

    您还需要将您的 window 变量初始化为某个默认值,否则它会在您检查它是否存在的那一行失败(但此时尚未定义 window)。

    这是添加属性并按预期连接它的脚本:

    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)
    

    【讨论】:

    • 所以我遇到的一个问题是在名称中使用下划线而不是句点?
    • 是的,并且该属性被创建为Parent_Joint_HelperJntAttr 而不是HelperJntAttr
    • Het @greenCell,如果你不介意我问:你能帮我解决另一个问题吗?更具体地说,这个:stackoverflow.com/questions/54032240/… 并且为了将来参考,是否链接到其他不受欢迎的问题?
    • 一般不赞成,因为它与这个问题无关。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    相关资源
    最近更新 更多