Hi,
Does anyone know the python code to create a rotating object.
The object should rotate a little bit evry frame, to create an animation.
Thanx a lot !
ishwar
Hi,
Does anyone know the python code to create a rotating object.
The object should rotate a little bit evry frame, to create an animation.
Thanx a lot !
ishwar
Some time ago I created a script which will create a looping rotation, taking all 3 axises (sp?) in account. It’s probably a bit of overkill for what you want to know, but it might be useful:
#!BPY
"""
Name: 'Autorotate'
Blender: 236
Group: 'Animation'
Tip: 'Create a looping animation'
"""
#Autorotate 0.2
#Written by Bartius Crouch
import Blender
from Blender import*
from math import ceil, pi
#declaring functions for finding the Least Common Multiple
#thanks to Brandano for finding these
def mcf(a, b):
while a != b:
if a > b:
a = a-b
else:
b = b-a
return a
def lcm(a, b):
return (a*b)/mcf(a,b)
#declaring some main variables
startframe2 = Draw.Create(Scene.GetCurrent().getRenderingContext().currentFrame())
rottimex2 = rottimey2 = rottimez2 = Draw.Create(0)
xcw = ycw = zcw = Draw.Create(1)
xccw = yccw = zccw = Draw.Create(0)
clockx = clocky = clockz = -1
def setipos():
global rottimex, rottimey, rottimez, startframe, clockx, clocky, clockz
#converting button types to integers
startframe = int(startframe2.val)
rottimex = int(rottimex2.val)
rottimey = int(rottimey2.val)
rottimez = int(rottimez2.val)
#find the loop number
if rottimex!=0 and rottimey!=0:
rotxy = lcm(rottimex, rottimey)
if rottimez!=0:
rottime = lcm(rotxy, rottimez)
else:
rottime = rotxy
elif rottimex!=0 and rottimez!=0:
rottime = lcm(rottimex, rottimez)
elif rottimey!=0 and rottimez!=0:
rottime = lcm(rottimey, rottimez)
elif rottimex!=0:
rottime = rottimex
elif rottimey!=0:
rottime = rottimey
elif rottimez!=0:
rottime = rottimez
print(rottime)
#this is the object which will be rotated
if len(Object.GetSelected())>0:
ob = Object.GetSelected()[0]
else:
name = 'Error%t|No object selected'
Draw.PupMenu(name)
return()
#unlink the current ipo-data of the object
ob.clearIpo()
#check if the ipo-data 'autorotation' already exists
try:
autorotation = Ipo.Get('autorotation')
#if not; create it
except:
autorotation = Ipo.New('Object','autorotation')
#check if all rotation curves already exist and create them if they don't
if autorotation.getCurve('RotX') == None:
autorotation.addCurve('RotX')
if autorotation.getCurve('RotY') == None:
autorotation.addCurve('RotY')
if autorotation.getCurve('RotZ') == None:
autorotation.addCurve('RotZ')
#just introducing some variables to keep things looking neat
curvex = autorotation.getCurve('RotX')
curvey = autorotation.getCurve('RotY')
curvez = autorotation.getCurve('RotZ')
#set interpolationmode for the curves
curvex.setInterpolation('Linear')
curvey.setInterpolation('Linear')
curvez.setInterpolation('Linear')
#set extrapolationmode for the curves
curvex.setExtrapolation('Cyclic')
curvey.setExtrapolation('Cyclic')
curvez.setExtrapolation('Cyclic')
#remove all existing points from the curves
for i in range(1,len(curvex.getPoints())+1):
curvex.delBezier(-1)
for i in range(1,len(curvey.getPoints())+1):
curvey.delBezier(-1)
for i in range(1,len(curvez.getPoints())+1):
curvez.delBezier(-1)
#create the ipo curves
rotx = ob.RotX
roty = ob.RotY
rotz = ob.RotZ
if rottimex!=0:
curvex.addBezier((float(startframe),float((rotx/pi)*18)))
curvex.addBezier((float(startframe+rottimex),float(((rotx+clockx*2*pi)/pi)*18)))
else:
curvex.addBezier((float(startframe),float((rotx/pi)*18)))
curvex.addBezier((float(startframe+rottimex),float((rotx/pi)*18)))
if rottimey!=0:
curvey.addBezier((float(startframe),float((roty/pi)*18)))
curvey.addBezier((float(startframe+rottimey),float(((roty+clocky*2*pi)/pi)*18)))
else:
curvey.addBezier((float(startframe),float((roty/pi)*18)))
curvey.addBezier((float(startframe+rottimey),float((roty/pi)*18)))
if rottimez!=0:
curvez.addBezier((float(startframe),float((rotz/pi)*18)))
curvez.addBezier((float(startframe+rottimez),float(((rotz+clockz*2*pi)/pi)*18)))
else:
curvez.addBezier((float(startframe),float((rotz/pi)*18)))
curvez.addBezier((float(startframe+rottimez),float((rotz/pi)*18)))
#link the ipo data to the object
ob.setIpo(autorotation)
#set the start and the end frame for the animation
context = Scene.GetCurrent().getRenderingContext()
context.startFrame(startframe)
#prevent the script from crashing if the endframe is too high
endframe = startframe+rottime-1
if endframe > 30000:
name = 'Error%t|The endframe of the loop is|larger than Blender allows. Blender|allows only a maximum of 30000 frames.'
print('Your loop ended at frame '+str(endframe)+'. This is '+str(endframe-30000)+' frames further than Blender is capable of.')
Draw.PupMenu(name)
else:
context.endFrame(startframe+rottime-1)
#set the current frame to the start frame of the animation
context.currentFrame(startframe)
#draw the Graphical User Interface
def draw():
global rottimex2, rottimey2, rottimez2, startframe2
global zcw, zccw, ycw, yccw, xcw, xccw
Draw.Button("Exit", 1, 10, 10, 40, 20, 'Exit the script')
Draw.Button("Set Ipo Curves", 2, 10, 40, 60, 20, 'Set the Ipo Curves')
rottimez2 = Draw.Number("Z-axis: ", 6, 10, 90, 120, 20, rottimez2.val, 0, 1000, "Rotation-time (in frames) for the z-axis. 0 = no rotation")
zcw = Draw.Toggle("cw", 61, 130, 90, 30, 20, zcw.val, 'Rotate ClockWise')
zccw = Draw.Toggle("ccw", 62, 160, 90, 30, 20, zccw.val, 'Rotate CounterClockWise')
rottimey2 = Draw.Number("Y-axis: ", 7, 10, 110, 120, 20, rottimey2.val, 0, 1000, "Rotation-time (in frames) for the y-axis. 0 = no rotation")
ycw = Draw.Toggle("cw", 71, 130, 110, 30, 20, ycw.val, 'Rotate ClockWise')
yccw = Draw.Toggle("ccw", 72, 160, 110, 30, 20, yccw.val, 'Rotate CounterClockWise')
rottimex2 = Draw.Number("X-axis: ", 8, 10, 130, 120, 20, rottimex2.val, 0, 1000, "Rotation-time (in frames) for the x-axis. 0 = no rotation")
xcw = Draw.Toggle("cw", 81, 130, 130, 30, 20, xcw.val, 'Rotate ClockWise')
xccw = Draw.Toggle("ccw", 82, 160, 130, 30, 20, xccw.val, 'Rotate CounterClockWise')
startframe2 = Draw.Number("Start frame: ", 9, 10, 160, 120, 20, startframe2.val, 1, 50000, "Set the starting frame for the loop")
#event handling
def event(evt, val):
if (evt== Draw.QKEY and not val):
Draw.Exit()
def bevent(evt):
global zcw, ycw, xcw, zccw, yccw, xccw, clockx, clocky, clockz
if evt == 1: #exit button
Draw.Exit()
elif evt == 2: #setting ipo curves
if len(Object.GetSelected())<=0:
name = 'Error%t|No object selected'
Draw.PupMenu(name)
return()
if rottimex2.val==0 and rottimey2.val==0 and rottimez2.val==0:
name = 'Err?%t|No rotation, pointless to run the script'
Draw.PupMenu(name)
return()
setipos()
Blender.Redraw()
elif evt == 61: #clockwise z button
if zcw.val == 0:
zccw = Draw.Create(1)
clockz = 1
else:
zccw = Draw.Create(0)
clockz = -1
Blender.Redraw()
elif evt == 62: #counterclockwise z button
if zccw.val == 0:
zcw = Draw.Create(1)
clockz = -1
else:
zcw = Draw.Create(0)
clockz = 1
Blender.Redraw()
elif evt == 71: #clockwise y button
if ycw.val == 0:
yccw = Draw.Create(1)
clocky = 1
else:
yccw = Draw.Create(0)
clocky = -1
Blender.Redraw()
elif evt == 72: #counterclockwise y button
if yccw.val == 0:
ycw = Draw.Create(1)
clocky = -1
else:
ycw = Draw.Create(0)
clocky = 1
Blender.Redraw()
elif evt == 81: #clockwise x button
if xcw.val == 0:
xccw = Draw.Create(1)
clockx = 1
else:
xccw = Draw.Create(0)
clockx = -1
Blender.Redraw()
elif evt == 82: #counterclockwise x button
if xccw.val == 0:
xcw = Draw.Create(1)
clockx = -1
else:
xcw = Draw.Create(0)
clockx = 1
Blender.Redraw()
Draw.Register(draw, event, bevent)
The original thread can be found here.