سؤال

folks! My question deals with a Python exercise that I'm currently trying to work out (well, to be more specific, the program is Autodesk Maya, but I'm using Python coding). The exercise involves taking a number of objects (spheres) contained in an array/list, and then using an increment variable to have them move in an offset animation. In other words, I want the first sphere to move, then the next spheres to move in a delayed time, then the next sphere with a more delayed time, etc.

The code that I have is as follows:

    spheres = mc.ls(selection=True)
    count=0

    for i in range(len(spheres)):
        count+=2
        mc.selectKey(spheres)
        mc.keyframe(edit=True, relative=True, timeChange=count)
        print spheres(i)

The spheres are my objects, and as I said, I want the first to move normally in the timeline, then the next sphere to move with a delayed time of two, then the next to move with a delayed time of four, so on and so forth.

Any help with this would be much appreciated.

Thanks, E

هل كانت مفيدة؟

المحلول

You're not actually setting the keyframe on the individual sphere; it looks like you're setting it on all spheres

Your for loop is generally bad form, but also less useful. Try changing it to this:

spheres = mc.ls(selection=True)
count=0

for sphere in spheres:
    count += 2
    mc.selectKey(sphere) # only selecting the one sphere!
    mc.keyframe(edit=True, relative=True, timeChange=count)
    print sphere # no need to look up the element
                 # which by the way should have been [i] not (i)

Output:

enter image description here

The keyframes were all lined up originally, but now offset by two frames each from the previous.

نصائح أخرى

You haven't told us what the problem is, but I have a guess. (If I've guessed wrong, please elaborate your question, and I'll delete my answer.)

Are you getting an exception like this?

TypeError                                 Traceback (most recent call last)
----> 1 print spheres(i)

TypeError: 'list' object is not callable

You claim that you have an "array/list" of spheres. If spheres is a list (or array or almost any other kind of collection) you index it using the [] operator. The () operator is used for function calls, not indexing. You're trying to call that list as if it were a function, passing it i as an argument, instead of trying to access that list as a sequence, getting the ith element.

To fix it:

print spheres[i]
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top