使用UI获取变量和调用函数。玛雅Python

2024-09-30 03:25:25 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我的问题是如何获取用户的整数输入并将其用作“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()

Tags: thetointrueforgroupcommonparent
1条回答
网友
1楼 · 发布于 2024-09-30 03:25:25

下面是一个示例,说明如何在单击按钮时调用特定的函数/命令,以及如何获取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()

如果你想更深入地制作用户界面,我建议你看以下两个视频:

相关问题 更多 >

    热门问题