maya python中的增量值

时间:2013-08-01 14:43:40

标签: python random increment maya

randoms = [rand.uniform(-1,1)for items in range(10)]
x= 0
for i in List:
    cm.setKeyframe(at = "%s.cv[x].xValue"%i , t=5, v=rand.choice(randoms))
    x+=1

我想使用的这部分代码,但maya不能接受这个[x]。 有人告诉我你应该这样做:

for i in List:
    x = 0
    cvAttrX = i+".cv["+str(x)+"].xValue"
    cm.setKeyframe(at = cvAttrX , t=5, v=rand.choice(randoms))
    x+=1

但我收到了这个错误:

# Error: line 1: can only concatenate list (not "str") to list
# Traceback (most recent call last):
#   File "<maya console>", line 1, in <module>
#   File "<maya console>", line 7, in randList
# TypeError: can only concatenate list (not "str") to list #

如果有人解决了这个问题,请向我解释。 我想学这个非常好 请修复这个例子:

randoms = [rand.uniform(cm.floatField(Ceil, q = True , v = True),0.5)for i in range(30)]
    for objects in myList:
        cm.xform('%s.cv[0]'%objects , r = True , t = [(rand.choice(randoms)),(rand.choice(randoms)),(rand.choice(randoms))])
        cm.xform('%s.cv[1]'%objects , r = True , t = [(rand.choice(randoms)),(rand.choice(randoms)),(rand.choice(randoms))])
        cm.xform('%s.cv[2]'%objects , r = True , t = [(rand.choice(randoms)),(rand.choice(randoms)),(rand.choice(randoms))])
        cm.xform('%s.cv[3]'%objects , r = True , t = [(rand.choice(randoms)),(rand.choice(randoms)),(rand.choice(randoms))])
        cm.xform('%s.cv[4]'%objects , r = True , t = [(rand.choice(randoms)),(rand.choice(randoms)),(rand.choice(randoms))])
        cm.xform('%s.cv[5]'%objects , r = True , t = [(rand.choice(randoms)),(rand.choice(randoms)),(rand.choice(randoms))])
        cm.xform('%s.cv[6]'%objects , r = True , t = [(rand.choice(randoms)),(rand.choice(randoms)),(rand.choice(randoms))])
        cm.xform('%s.cv[7]'%objects , r = True , t = [(rand.choice(randoms)),(rand.choice(randoms)),(rand.choice(randoms))])
        cm.xform('%s.cv[8]'%objects , r = True , t = [(rand.choice(randoms)),(rand.choice(randoms)),(rand.choice(randoms))])
        cm.xform('%s.cv[9]'%objects , r = True , t = [(rand.choice(randoms)),(rand.choice(randoms)),(rand.choice(randoms))])
        cm.xform('%s.cv[10]'%objects , r = True , t = [(rand.choice(randoms)),(rand.choice(randoms)),(rand.choice(randoms))])

这是我脚本的一部分如果我可以做增量我不想写这行100例,我可以为此做intField并确定我需要多少

我想做这样的事情,但我不知道如何

import maya.cmds as cm
import random as rand
myList = cm.ls(sl =True)
cvList = []
randomList = []
def Lister():
for i in myList:
    cvList.append(cm.ls('%s.cv[:]'%i , flatten = True))
    return cvList
def randomize():
    x = 0 
    randomList.append([rand.uniform(-1,1)for items in range(10)])
    for i in cvList:
        cm.setKeyframe(at = "%s.cv[x].xValue"%i , t=5, v=rand.choice(randoms))
        x+=1

2 个答案:

答案 0 :(得分:0)

myCurve = cm.ls(sl=1) # Will return [u'curve1']

# Since it returned the object in a list in the next command you have to subscript it.

myCVs = cm.ls(myCurve[0] + ".cv[:]", fl=1)


# Result: [u'curve1.cv[0]',
 u'curve1.cv[1]',
 u'curve1.cv[2]',
 u'curve1.cv[3]',
 u'curve1.cv[4]',
 u'curve1.cv[5]',
 u'curve1.cv[6]',
 u'curve1.cv[7]',
 u'curve1.cv[8]',
 u'curve1.cv[9]',
 u'curve1.cv[10]'] # 

# We do not need to increment through these, flatten takes care of that. We need only iterate through the list of cvs and concatenate the attribute to the name.

for i in myCVs:
    cm.setAttr(i + ".xValue", random.uniform(-1, 1))
    print cm.getAttr(i + ".xValue")

# This will move all the cvs on that curve by a random value between -1 and 1.

-0.233699187896
0.353531892619
0.736778238244
-0.767007983153
0.672321920729
0.0556060597496
-0.340945469966
0.252136119281
-0.396852920849
-0.683451384587
0.926232431375

答案 1 :(得分:0)

首先,你甚至没有设置动画。你需要一些东西来驱动它。现在,如果您设置关键帧,所有这些都将在一帧上发生。你可以这样做:

time = cm.currentTime(q=1)
for i in range(10):
    cm.setAttr(myCVs[0]+".xValue", random.choice(test))
    cm.setKeyframe(myCurve, shape=1, controlPoints=1)
    time = cm.currentTime(time + 1)

或者您可以将我插入time的{​​{1}}标记。通过这种方式,它可以对整个曲线,变换,形状和控制点进行关键帧设置。而且你不必担心迭代所有这些。首先设置值然后设置关键帧,就像你实际制作动画一样。