Skip to main content
 首页 » 编程设计

python之如何在 Maya 中通过 Python 创建进度窗口

2025年02月15日8TianFang

我想知道是否有一种方法可以创建一个进度窗口,显示距离程序完成还有多长时间。这是我目前所拥有的,但它没有达到我的预期。

我希望它一直运行到所有曲线都创建完毕然后停止。

import maya.cmds as cmds 
 
cmds.setAttr( "nParticle1Shape.lifespanMode", 2 )  
cmds.setAttr( "nParticle1Shape.maxCount", 5 )  
cmds.setAttr( "nParticle1Shape.lifespanRandom", 3 ) 
 
allParticleDictionary = {} 
minFrames = cmds.playbackOptions( q=True, min=True ) 
maxFrames = cmds.playbackOptions( q=True, max=True ) 
 
running = 0 
amount = 0 
cmds.progressWindow( title='Doing Nothing',progress=amount,status='Sleeping: 0%',isInterruptable=True ) 
 
for currentFrame in range(0, int(maxFrames)): 
    cmds.currentTime( currentFrame, update=True, edit=True ) 
    cmds.select( 'nParticle1' ) 
 
    Particles = cmds.ls( sl=True, type='transform' ) 
    for part in Particles: 
        for particleCount in range( 0,cmds.particle(part, q=True,ct=True) ): 
            particleName = cmds.particle( part, q=True, order=particleCount, at='id' ) 
            particlesPosition = cmds.particle( part, q=True, order=particleCount, at='position' ) 
            particleDictionary = {} 
 
            if str( particleName[0] ) in allParticleDictionary.keys(): 
                particleDictionary = allParticleDictionary[str(particleName[0])] 
 
                particleDictionary[currentFrame] = particlesPosition 
                allParticleDictionary[str(particleName[0])] = particleDictionary 
 
 
    emptyFolder = cmds.group( em=True, n="Curves" ) 
    for curveParticleId in allParticleDictionary.keys(): 
        pointList = [] 
        sortedKeyFrameList = sorted( allParticleDictionary[curveParticleId].keys() ) 
 
        if len( sortedKeyFrameList ) > 1: 
            for keyFrame in sortedKeyFrameList: 
                pointList.append( allParticleDictionary[curveParticleId][keyFrame] ) 
 
                curveObj = cmds.curve( name = "pCurve" + str(curveParticleId ), p = pointList) 
                Locators = cmds.spaceLocator( name = "locator" + str(curveParticleId) ) 
                cmds.pathAnimation( Locators, stu=sortedKeyFrameList[0], etu=sortedKeyFrameList[-1], c=curveObj )        
 
                running = 1 
                # place all objects in Group called Curves  
                cmds.parent( curveObj, emptyFolder ) 
                cmds.select( Locators ) 
                cmds.delete() 
 
            while running == 0: 
 
                # Checks if the dialog has been cancelled 
                if cmds.progressWindow( query=True, isCancelled=True ) : 
                    break 
 
                # Checks if end condition has been reached 
                if cmds.progressWindow( query=True, progress=True ) >= 100 : 
                    break 
 
                amount += 20 
                cmds.progressWindow( edit=True, progress=amount, status=('Sleeping: ' + `amount` + '%' ) ) 
                cmds.pause( seconds=1 ) 
                cmds.progressWindow( endProgress=1 ) 

请您参考如下方法:

前言是我不确定我的逻辑是否正确:as @theodox said, your indents seem wrong即使在您最近更正之后。

无论如何,如果我对它的解释正确的话,这里有一个版本可以满足你的目标:

from maya import cmds 
 
def checkProgressEscape(): 
    # check if dialog has been cancelled 
    cancelled = cmds.progressWindow(query=True, isCancelled=True) 
    if cancelled: 
        cmds.progressWindow(endProgress=1) 
    return cancelled 
 
def getAllParticlesDictionary(shape): 
    cmds.progressWindow(title='Fetch Particle Data', progress=0, status='Fetched: 0%', isInterruptable=True) 
 
    ptcsDict = {} 
    minFrames = int(cmds.playbackOptions( q=True, min=True)) 
    maxFrames = int(cmds.playbackOptions( q=True, max=True)) 
    nFrames = float(maxFrames - minFrames) 
    # better use the actual range, since you queried the playback options 
    for currentFrame in range(minFrames, maxFrames): 
        if checkProgressEscape(): 
            return 
        amount = 100.0 / nFrames * (currentFrame - minFrames + 1) 
        cmds.progressWindow(edit=True, progress=amount, status='Fetched: %d%%' % amount) 
        # cmds.pause(seconds=.001) 
 
        cmds.currentTime(currentFrame, update=True, edit=True) 
        for particleCount in xrange(cmds.particle(shape, q=True, ct=True)): 
            particleId = int(cmds.particle(shape, q=True, order=particleCount, at='id')[0]) 
            # better check the key exists first, then add your logic 
            if particleId not in ptcsDict.keys(): 
                ptcsDict[particleId] = {}                     
            ptcsDict[particleId][currentFrame] = cmds.particle(shape, q=True, order=particleCount, at='position') 
 
    cmds.progressWindow(endProgress=1) 
    return ptcsDict 
 
def curvesFromParticle(ptcsDict): 
    cmds.progressWindow(title='Build Curves', progress=0, status='Building Curves: 0%', isInterruptable=True) 
    # this section had to be de-indented 
    emptyFolder = cmds.group(em=True, n="Curves") 
    ptcCount = len(ptcsDict) 
    for i, (curveParticleId, data) in enumerate(ptcsDict.iteritems()): 
        if checkProgressEscape(): 
            return 
 
        sortedKeyFrameList = sorted(data.keys()) 
        pointList = [] 
        for keyFrame in sortedKeyFrameList: 
            pointList.append(ptcsDict[curveParticleId][keyFrame]) 
 
        curveObj = cmds.curve(name="pCurve%d" % curveParticleId, p=pointList) 
        locators = cmds.spaceLocator( name = "locator%d" % curveParticleId) 
        cmds.pathAnimation( locators, stu=sortedKeyFrameList[0], etu=sortedKeyFrameList[-1], c=curveObj)        
        # place all objects in Group called Curves  
        cmds.parent(curveObj, emptyFolder) 
        cmds.delete(locators) 
 
        amount = 100 / ptcCount * float(i) 
        cmds.progressWindow(edit=True, progress=amount, status='Building Curves: %d%%' % amount) 
    cmds.progressWindow(endProgress=1) 
 
 
ptcs = cmds.ls(typ='nParticle') 
for shape in ptcs: 
    cmds.setAttr("%s.lifespanMode" % (shape, ), 2) 
    cmds.setAttr("%s.maxCount" % (shape, ), 100) 
    cmds.setAttr("%s.lifespanRandom" % (shape, ), 3) 
    ptcDict = getAllParticlesDictionary(shape) 
    if not ptcDict: 
        print 'data was not be fetched for %s; skipped' % (shape, ) 
        continue 
    curvesFromParticle(ptcDict) 

以上是您脚本的(略微)修改版本;如您所见:

  • 我将主要的逻辑 block 移到了函数中(只是为了便于阅读)
  • 我删除了您的 while 语句,因为进度条不需要它们:您已经在 for 语句中循环
  • 我还删除了你的进度检查,如果等于或大于 100,因为一旦你退出循环,你可以假设你不再需要显示进度,只需使用 endProgress 参数;不过,您仍然需要检查用户是否逃脱了该过程
  • 我添加了两个进度条“ session ”,一个用于循环获取粒子数据并填充您的字典,一个用于构建曲线

附带说明一下,您可能想知道(如果您还不知道)maya is mostly single threaded .使用 API 时,您可能会遇到一些限制,但通常情况下,在使用 Maya cmds 时,您可以非常确定您是在单个线程上工作。

这意味着进度条将在您的逻辑运行的同一线程上运行,因此将脚本的性能限制了几个数量级:我一直在使用粒子 + 进度条做一些广泛的工作,我发现绘制 UI 可能比仅运行逻辑要慢得多。 随着粒子数量的增加,您的脚本可能会变得非常慢,这只是因为您的进度条正在刷新,而不是因为逻辑本身。

一个好的做法是只在已知的时间间隔更新进度(阅读:每 n 个粒子)。假设您有 1000000 个粒子并且您只想以 5% 的步长更新侧边栏,您可以这样写:

import random 
 
cmds.progressWindow(title='Stepped Progress Bar', progress=0, status='Progress (stepped by 1): 0%%', isInterruptable=True) 
limit = 1000000 
step = 5 
for i in xrange(limit): 
    progress = 100.0 / limit * i 
    if progress % step: 
        continue 
    cmds.progressWindow(e=1, progress=progress, status='Progress (stepped by 1): %d%%' % progress) 
    # the following is to simulate some processing time 
    cmds.pause(seconds=random.random() * 2) 
cmds.progressWindow(endProgress=1)