Maya Python Textfield 对象名称帮助



这是我的计划。首先创建关节并打开节点编辑器。当您创建关节时,将其命名为" a_joint"命中"负载接头",然后在命中测试时命中"测试",您应该获得一个名称为" a_joint_firstguy"的节点

此脚本的目的是根据您加载到文本字段的任何内容的名称创建一个节点。它将以所选对象的名称添加到节点名称的正面

的名称。

至少这应该发生,但实际上,我缺乏弄清楚这一点的知识,到目前为止,每个Google搜索都是徒劳的。脚本下面是愿意为此划分的任何人,谢谢您的时间,我希望收到您的回音:

https://drive.google.com/file/d/1nvl0mzcdjckanvu6vovnybaw0hurz6rh/view?usp = sharing

或以下格式:

import maya.cmds as cmds
if cmds.window(window, exists =True):
    cmds.deleteUI(window)
window = cmds.window(title = "DS Selection Loader Demo", widthHeight=(300, 200) )
cmds.columnLayout(adjustableColumn=True) 
def sld_loadHelperJoint():
    sel = cmds.ls(selection=True)
    selString = " ".join(sel)
    add = cmds.textField('sld_surfaceTextHJ', edit=True, text=selString)
#def sld_loadParentJoint():
 #   sel = cmds.ls(selection=True)
  #  selString = " ".join(sel)
   # add = cmds.textField('sld_surfaceTextPJ', edit=True, text=selString)

def createNode():
    testNode = cmds.createNode( 'transform', selString = "sld_surfaceTextHJ", name = '_firstGuy' )

cmds.columnLayout(adjustableColumn=True)
sld_textFld = cmds.textField('sld_surfaceTextHJ', width =240)
cmds.button( label='Load Helper Joint', command = "sld_loadHelperJoint()")
cmds.setParent('..')
#cmds.columnLayout(adjustableColumn=True)
#name = cmds.textField('sld_surfaceTextPJ', width =240)
#cmds.button( label="Load Parent Joint", command = "sld_loadParentJoint()")
#cmds.setParent('..')
testNode = cmds.createNode( 'transform', name = textField +'_firstGuy' )

# you must first create "def" group for the attributes you 
# want to be created via button, "testNode" is our example
# Connect the translation of two nodes together
#cmds.connectAttr( 'firstGuy.t', 'secondGuy.translate' )
# Connect the rotation of one node to the override colour
# of a second node.
#cmds.connectAttr( 'firstGuy.rotate', 'secondGuy.overrideColor' )
cmds.showWindow (window)

好吧,这里有一些事情要考虑。

首先,Maya Gui小部件看起来像字符串 - 就像您制作一个polycube一样,它作为字符串的" pcube1'"又回到您身边,一个小部件将以" mywindow"或" textfieldfield72"之类的字符串返回。就像使用场景对象一样,您始终需要捕获命令的结果,以便您知道它的真正叫什么 - 您不能保证您要获得所需的名称,因此请始终捕获结果。

所以您想做这样的事情,只是为了使图形进行:

window = cmds.window(title='selection loader demo')
column = cmds.columnLayout(adj=True)
sld_textFld = cmds.textField('sld_surfaceTextHJ', width =240)
load_button = cmds.button( label='Load Helper Joint')
cmds.show_window(window)

例如,如果您需要询问Textfield中的内容,您会做:

text_contents = cmds.textField(sld_textFld, q=True, text=True)

您注意到这是 actible 而不是字符串,所以我们确定我们有任何工作。

为了使按钮使用该信息,按钮脚本需要访问变量。有几种方法可以做到这一点 - 这是一个常见的堆栈溢出问题 - 但最简单的方法只是定义您已经具有该变量的按钮命令。因此以上样本变为:

window = cmds.window(title='selection loader demo')
column = cmds.columnLayout(adj=True)
sld_textFld = cmds.textField('sld_surfaceTextHJ', width =240)
def set_textfield(_):
    sel = cmds.ls(selection=True)
    add = cmds.textField(sld_textFld, edit=True, text=sel[0])
load_button = cmds.button( label='Load Helper Joint', c = set_textfield)
cmds.showWindow(window)

这里有两个位。

一个是函数定义中的_;玛雅按钮总是用一个参数调用其功能。下划线没有什么神奇的,它只是"忽略我"的python语 - 但是,如果您在函数def中没有参数,它将失败。

更重要的一点是,该按钮是直接给出函数定义的,无引号。您说单击时调用此功能。如果您使用字符串版本 - MEL的保留 - 稍后将遇到问题。原因是在此处详细说明,但是TLDR不使用字符串表单。永远。

现在结构件已经到位,您应该能够将节点创建到我称为 set_textfield()的函数或制作一个执行实际节点创建的第二个按钮函数组合,例如:

window = cmds.window(title='selection loader demo')
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 Helper Joint', c = set_textfield)
def make_node(_):
   text_value = cmds.textField(sld_textFld, q = True, text=True)
   if text_value:
       print "created:", cmds.createNode('transform', n=text_value +'_firstGuy')
   else:
       cmds.warning("select an object and add it to the window first!")
node_button = cmds.button( label='Make Node', c = make_node)
cmds.showWindow(window)

最新更新