通过UI获取变量和调用函数。玛雅人蟒蛇

时间:2019-01-05 16:33:13

标签: python function user-interface variables maya

我目前正在研究一个小脚本,该脚本可以在Autodesk Maya中自动创建类似起重机的装备,用户可以通过UI选择关节的数量。

我的问题是如何获取用户的整数输入并将其用作我的“ jointAmount”的变量值?

我还想知道如何调用我的函数(AutoCraneRig)从UI实际运行脚本。我有一个“应用”按钮,但不确定如何将其连接到我的功能。

我见过类似我的类似文章,但是我觉得所示的解决方案对我来说有点难以理解和/或我无法将显示的内容与自己的问题真正联系起来。

如果有任何不清楚的地方或需要我提供更多信息,请随时给我打电话。

Here is what my current UI look like

import maya.cmds as cmds
import pymel.core as pm


def jntctrl():
number = pm.intField(jnt, q=1, v=1)
print(number)

if pm.window("stuff", exists = True):
pm.deleteUI("stuff")

pm.window("stuff", t = "Crane Rig Generator", w=400, h=200)
pm.columnLayout(adj = True)

pm.text(label="Joint Amount:")
jnt = pm.intField(changeCommand = 'jntctrl()')

pm.button(label="Create Crane")

pm.showWindow()


#Defining how many joints the user want to have for their crane rig
jointAmmount = 5

#Defining how many controllers the user want to have to orient the crane. 
#May not exceed the joint amount
controllerAmount = 5

def autoCraneRig():
#Creating the joints
for i in range(jointAmmount):
  pm.joint()
  pm.move(0, i, 0)

#Creating the controllers
for i in range(controllerAmount):
    pm.circle()
    pm.rotate (0,90,0)
    pm.makeIdentity (apply= True)

#Creating the groups    
for i in range(controllerAmount):
    pm.group()

#Somehow one of the nurbs get parented to a group when running the script, here i select both the groups and then unparent them.
pm.select("group*", "nurbsCircle*")
pm.parent(world = True)

#Creating lists/dictionaries for the groups
#Since I wanted to parent my objects by their number I had to put all objects in lists/dictionries to get access.
groups = pm.ls('group*')
nbs = [int(n.split('group')[-1]) for n in groups]
groupDic = dict(zip(nbs, groups))

#Create a list/dictionary for the joints
joint = pm.ls('joint*', type='joint')
nbs = [int(n.split('joint')[-1]) for n in joint]
jointDic = dict(zip(nbs, joint))

common = list(set(groupDic.keys())&set(jointDic.keys()))

#Parenting the groups to the joints
for i in common:
    pm.parent(groupDic[i], jointDic[i])

#Reseting the transformations of the groups and then unparenting them to still have the transformation data of the joints    
pm.select("group*")
pm.makeIdentity()
pm.parent(world = True)

#Creating a list/dictionary for the nurbs aswell that will be parented to the groups in numeric order
nurbs_sh = pm.ls('nurbsCircle*', type='nurbsCurve')

#I had to get the transformation information from the nurbs before parenting them with anything would work(took a long time to get it right).
nurbs_tr = pm.listRelatives(nurbs_sh, p=1)
nbs = [int(n.split('nurbsCircle')[-1]) for n in nurbs_tr]
curveDic = dict(zip(nbs, nurbs_tr))

common = list(set(groupDic.keys())&set(curveDic.keys()))

#Parent the nurbs to the groups
for i in common:
    pm.parent(curveDic[i], groupDic[i])

#Select the nurbs and reset transformations and then freeze transform
pm.select("nurbsCircle*")
pm.makeIdentity()

#Orient constrain the controllers/nurbs to the joints
for i in common:
    pm.orientConstraint(curveDic[i], jointDic[i])

#Parent the 2nd group with the first controller. Do this for the whole hierarchy. 
for i in common:
    pm.parent(groupDic[i+1], curveDic[i])

#I'm getting keyError after I put the "+1" in my groupDic and I don't know why, although it still works, I guess.

autoCraneRig()

1 个答案:

答案 0 :(得分:1)

这里有一个示例,说明如何在单击按钮时调用特定的函数/命令,以及如何获取int字段的值。关键在于命名字段,因此您以后可以引用UI控件。

import pymel.core as pm


def ui():
    if (pm.window("myWindow", exists=True)):
        pm.deleteUI("myWindow")

    window = pm.window("myWindow", t="My Window", w=400, h=200)
    pm.columnLayout(adj=True)
    pm.intField("myIntField")
    pm.button("Button", aop=True, command="action()")

    pm.showWindow(window)


def action():
    print("Button clicked!")
    value = pm.intField("myIntField", q=True, v=True)
    print(value)


ui()

如果您想更多地制作UI,我建议您观看以下两个视频: