请求Maya Python协助:基于文本字段内容连接属性



背景:

因此,在用户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)  

我的预期结果:

在加载关节后点击"make node"后点击"load helper joint",一旦用关节的名称前缀创建了"_firstGuy"one_answers"_secondGuy",它们的平移将被连接。打开节点编辑器来测试这一点会有所帮助。

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

connectAttr(<attributeA>, <attributeB>)

其中attributeA类似于"NodeA.translate"。因此,您需要的是第一个节点的名称和属性名称,在您的情况下:

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

该属性是众所周知的"translate",因此完整的属性名称为:

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

现在完整的命令是:

connectAttr(attributeNameA, attributeNameB)

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

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

最新更新