【问题标题】:Maya Python Assistance requested: connect attributes based on textfield contents请求 Maya Python 协助:根据文本字段内容连接属性
【发布时间】:2019-05-21 13:40:28
【问题描述】:

背景:

因此,在用户 Theodox 的帮助下,我能够弄清楚如何在节点编辑器中根据您加载到选择字段中的关节创建具有名称前缀的节点。

但是:我想更进一步,让节点不仅使用联合名称前缀创建:而且它们还将连接通过 connectAttr 创建的节点的翻译。

问题所在?

我目前缺乏完成这项工作的知识,也无法在网上找到任何东西,因此非常感谢任何帮助

代码:

我尝试过以下代码行:

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

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

我知道我可以创建单独的文本字段和按钮来加载节点并以这种方式连接它们,但是使用我正在编码的快捷方式,我创建的所有节点都会太多而无法加载,所以如果我这样做会更容易可以用它们的连接创建节点,我将在下面发布代码供任何愿意尝试的人使用:

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)  

我的预期结果:

在点击“加载辅助关节”后加载关节后点击“制作节点”时,一旦使用关节的名称前缀创建“_firstGuy”和“_secondGuy”,它们的翻译将被连接。打开节点编辑器进行测试会有所帮助。

【问题讨论】:

  • 您好,欢迎来到 SO。对不起,我没有完全理解你想要做什么。你创建了两个节点,你想连接平移和旋转属性,对吗?不幸的是,您的代码不起作用。它有逻辑错误和语法错误。如果您复制并粘贴它,它会失败。请尝试构建一个工作示例。
  • 嘿,对不起@haggikrey 我还在想办法在这里发布代码,但我想我已经弄明白了。 1. 创建一个关节并打开节点编辑器:将关节命名为任何名称 2. 运行脚本并点击“load helper joint” 3. 点击“make node” 这应该创建2个节点,其中包含您加载的关节的名称前缀节点名称,因此,如果您加载了一个名为“A_Joint”的关节并点击“制作节点”,它将创建 2 个节点:“A_Joint_firstGuy”和“A_Joint_secondGuy”但我希望脚本更进一步并连接 2 个创建节点的翻译,就像一个会在节点编辑器中做,

标签: python maya autodesk pymel


【解决方案1】:

好的,你要连接两个新创建的节点的翻译属性。 通常连接属性的工作方式如下:

connectAttr(<attributeA>, <attributeB>)

attributeA 类似于“NodeA.translate”。所以你需要的是你的第一个节点的名称和属性名称,在你的情况下:

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

该属性是众所周知的“翻译”,因此完整的属性名称为:

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

现在完整的命令是:

connectAttr(attributeNameA, attributeNameB)

这里唯一的问题是,如果已经存在同名对象,Maya 会自动重命名对象。因此,以这种方式使用创建的名称是一种更节省的方式:

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

【讨论】:

    猜你喜欢
    • 2019-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-02
    • 2016-04-20
    • 1970-01-01
    • 2015-04-08
    • 2012-08-29
    相关资源
    最近更新 更多