制作一个可以在Maya Python中循环经过3个不同状态的脚本 [英] Making a script that will cycle through 3 different states in Maya Python

查看:118
本文介绍了制作一个可以在Maya Python中循环经过3个不同状态的脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Python在Maya中创建脚本并将其绑定到热键上。每次运行脚本时,我都想遍历3个状态,即立方体/圆柱体/平面。因此,例如,第一次运行脚本将创建一个多维数据集,第二次删除该多维数据集并创建一个圆柱体,第三次删除该圆柱体并创建一个平面。直到用户决定他想要什么原语并结束循环为止。我尝试使用while循环,但失败了。

I want to create a script in Maya using Python and bind it on a hotkey. Every time I run the script I want to loop through 3 states, cube/ cylinder / plane. So for example first time I run the script it will create a cube, second time delete the cube and create a cylinder third time delete the cylinder and create a plane., fourth time delete the plane and create a cube etc... I want this to happen until the user decides what primitive he wants and end the loop. I tried using while loop but I failed miserably.

最终结束了:

def createCube():
    return "cube"

def createCylinder():
    return "cylinder"

def createPlane():
    return "plane"

def numbers_to_primitives(argument):
    switcher = {
        1: createCube,
        2: createCylinder,
        3: createPlane,
    }
    # Get the function from switcher dictionary
    func = switcher.get(argument, lambda: "Invalid primitive")
    # Execute the function
    print func()

numbers_to_primitives(2)

这有点奏效。但是随着我创建越来越多的原语而不是替换现有的原语,我一遍又一遍地运行命令时会遇到问题。

This kinda seems to work. But I foresee issues when running the command over and over as I am creating more and more primitives instead of replacing the existing ones. Would also need to create a toggle button to cycle through these?

推荐答案

您还有几个问题需要解决。首先,您想在热键中使用脚本,这意味着每次您调用不带任何参数的numbers_to_primitive()时,脚本应产生不同的结果。因此,您首先需要保存并读取当前状态。您有几种方法可以做到这一点。如果仅在当前maya会话中需要状态,则可以使用如下全局变量:

You have several questions problems to solve. First, you want to use the script in a hotkey what means it should produce a different result every time you call numbers_to_primitive() without any argument. So you first need to save and read the current state. You have several ways to do it. If you only need the state in the current maya session, you can use a global variable like this:

state = 0

def box():
    print "Box"

def cyl():
    print "Cylinder"

def sph():
    print "Sphere"
    
def creator():
    global state
    print "current state", state
    state = state + 1
    if state > 2:
        state = 0
        
creator()

这样状态变量在值0-2之间循环。现在,您要创建几何并在再次调用该函数后立即替换它。它的工作方式非常相似:保存当前对象,并在再次调用该函数后将其删除,如下所示:

This way the state variable cycles through values 0-2. Now you want to create geometry and replace it as soon as the function is called again. It works very similiar: Save the current object and delete it as soon as the function is called again like this:

import maya.cmds as cmds
state = 0
transformName = "" #again a global variable

def box():
    print "Box"
    global transformName #to use the global variable you have do use "global" keyword
    transformName, shape = cmds.polyCube()

def cyl():
    print "Cylinder"
    global transformName
    transformName, shape = cmds.polyCylinder()

def sph():
    print "Sphere"
    global transformName
    transformName, shape = cmds.polySphere()
    
def creator():
    global state
    funcs = [box, cyl, sph]
    print "current state", state
    print "TransformName", transformName
    if cmds.objExists(transformName):
        cmds.delete(transformName)
    funcs[state]()
    state = state + 1
    if state > 2:
        state = 0

现在,您可以调用creator()函数,并且每次它将删除旧对象并创建一个新对象。

Now you can call the creator() function and every time it will delete the old object and create a new one.

这篇关于制作一个可以在Maya Python中循环经过3个不同状态的脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆