gpt4 book ai didi

python - 使用 UI 获取变量并调用函数。玛雅 python

转载 作者:行者123 更新时间:2023-12-01 08:28:34 25 4
gpt4 key购买 nike

我目前正在编写一个小脚本,该脚本可以在 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()

最佳答案

下面是一个示例,说明如何在单击按钮时调用特定函数/命令,以及如何获取 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 的制作,我建议您观看这两个视频:

关于python - 使用 UI 获取变量并调用函数。玛雅 python ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54053949/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com