Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
制作一个脚本,在Maya Python中循环3种不同的状态_Python_Loops_State_Maya_Cycle - Fatal编程技术网

制作一个脚本,在Maya Python中循环3种不同的状态

制作一个脚本,在Maya Python中循环3种不同的状态,python,loops,state,maya,cycle,Python,Loops,State,Maya,Cycle,我想使用Python在Maya中创建一个脚本,并将其绑定到热键上。每次运行脚本时,我都想循环3种状态,立方体/圆柱体/平面。例如,我第一次运行脚本时,它将创建一个立方体,第二次删除立方体并创建一个圆柱体,第三次删除圆柱体并创建一个平面,第四次删除平面并创建一个立方体等等。。。我希望这种情况发生,直到用户决定他想要什么原语并结束循环。我尝试使用while循环,但失败得很惨 结果是: def createCube(): return "cube" def createC

我想使用Python在Maya中创建一个脚本,并将其绑定到热键上。每次运行脚本时,我都想循环3种状态,立方体/圆柱体/平面。例如,我第一次运行脚本时,它将创建一个立方体,第二次删除立方体并创建一个圆柱体,第三次删除圆柱体并创建一个平面,第四次删除平面并创建一个立方体等等。。。我希望这种情况发生,直到用户决定他想要什么原语并结束循环。我尝试使用while循环,但失败得很惨

结果是:

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)

这种方法似乎有效。但是我预见到了反复运行命令时会出现的问题,因为我正在创建越来越多的原语,而不是替换现有的原语。还需要创建一个切换按钮来循环执行这些操作?

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

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()
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
这样,状态变量通过值0-2循环。现在,您需要创建几何体,并在再次调用该函数时立即替换它。其工作原理非常相似:保存当前对象,并在再次调用函数时立即删除它,如下所示:

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()
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()函数,每次它都会删除旧对象并创建新对象