PDA

View Full Version : Simple bobbing / floating animation with Python


davidmattock
07-13-2012, 11:26 AM
Hi Can anyone help me animate a simple floating in space / bobbing up and down animation with Python, im trying to use math.sin which works but I dont know how to get it to change every frame of the timeline.

I then need to add this animation to multple objects which Ive duplicated with python and random ranges. I need the bobbing to be slightly different of each duplicted geometry so they are not bobbing in time. here is what I have so far....

import maya.cmds as mc
import random
import math
import time
for x in range(100):
jobs = mc.duplicate('JOB_group')
rangeTransX = random.randint(-100,100)
rangeTransZ = random.randint(-100,100)
rangeTransY = random.randint(0,100)
rangeRotX = random.randint(-30,30)
rangeRotZ = random.randint(-30,30)
rangeRotY = random.randint(-30,30)
rangeScale = random.randint(1,2)
mc.setAttr(jobs[0] +'.translateX', rangeTransX)
mc.setAttr(jobs[0] +'.translateY', rangeTransY)
mc.setAttr(jobs[0] +'.translateZ', rangeTransZ)
mc.setAttr(jobs[0] +'.rotateX', rangeRotX)
mc.setAttr(jobs[0] +'.rotateY', rangeRotY)
mc.setAttr(jobs[0] +'.rotateZ', rangeRotZ)
mc.setAttr(jobs[0] +'.scaleX', rangeScale)
mc.setAttr(jobs[0] +'.scaleY', rangeScale)
mc.setAttr(jobs[0] +'.scaleZ', rangeScale)

now here sthe bit where im trying to make them bob up and down slighly like they are floating in space

mc.setAttr(jobs[0] +'.translateY', math.sin(time.time()))

this only works on one not them all, and dosnt work along teh timeline yet. Sorry Im quite new to Python :blush: any help really appreciated, many thanks

so then I tried this
startFrame=mc.playbackOptions(query=True,minTime=True)
endFrame=mc.playbackOptions(query=True,maxTime=True)
currentFrame=startFrame
while(currentFrame<endFrame):
#print "The current frame is %d" % currentFrame
mc.currentTime(currentFrame)
currentFrame+=1
mc.setAttr(jobs[0] +'.translateY', math.sin(time.time()))

but this dosnt work when I scrub the timeline

rgkovach123
07-13-2012, 03:58 PM
You should look into using Expressions. You can use Python to create the expression node(s) and hook it up to your nodes, but the expressions language itself isn't python - its MEL-like.

zoharl
07-13-2012, 04:06 PM
Expressions might be helpful here, but there's no need for mel, you can execute python from mel using the python command (in mel).

You did a loop over x, but still you assigned everything to job 0.
Next, when you move the items in each frame there should be an inner loop (inside the frame loop) that goes over your objects.

davidmattock
07-16-2012, 03:01 PM
Hey thanks for your replies, I got it working in the end the same way you suggested.
I put this in my while loop
mc.expression( n='bobbing', o=jobs[0], s='ty += sin(time)/2 * ' + str(random.uniform(-1,1)) + ';' )

I did have this at first: mc.expression( o=jobs[0], s='ty += sin(time*2)/2 +rand(1,4);' )
but the boobing motion was jumping because i think it was generating a new random number every frame. so someone teol me to use a python string to generate the random number.

Im still not sure why sticking a python string in works over the mel random number, and also why I cant do it all in Python, should be simple, but ast least I got it working in time, thanks to the good help at rigging dojo. :)

CGTalk Moderation
07-16-2012, 03:01 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.