Hi, I am trying to do the following:
- Add an object (cube)
- then scale it from zero to some dimension and animate the scaling over 20 key frames
- then rotate it on the x-axis and animate the rotation over the next 20 key frames, i.e. from 21 to 40
At step 2, the cube is scaling with an x-rotation of 4.5 degrees, even though there is nothing in the code to suggest this. Step 3 works fine. In other words, the second ‘for loop’ is ok but something seems to be wrong in the first ‘for loop’.
Strangely, the problem doesn’t arise if I am only running the first ‘for loop’ i.e. when I am removing the second ‘for loop’ from the code.
Please can someone advise what’s wrong with the code:
bpy.ops.object.select_all(action=‘SELECT’)
bpy.ops.object.delete(use_global=False)
add a cube
bpy.ops.mesh.primitive_cube_add(size=0, location=(0, 0, 0), rotation=(0, 0, 0))
create a reference for it
ob = bpy.context.object
set the number of keyframes to animate over
kf1 = 21
animate the scale, starting from zero up to the desired scale
set the object’s scale to 0
ob.scale = 0,0,0
ob.rotation_euler = 0,0,0
each frame will increment the scale by (scale/keyframe) units
for i in range(kf1):
ob.scale[0] += 6/kf1
ob.scale[1] += 2/kf1
ob.scale[2] += 0.25/kf1
# set a location and scale keyframe at each keyframe i.e. step
ob.keyframe_insert(data_path = “location”, frame = i)
ob.keyframe_insert(data_path = “scale”, frame = i)
rotate 90 degrees or 1.570796 radians over the next 20 keyframes
kf2 = 41
for i in range(kf1, kf2):
ob.rotation_euler[0] += 1.570796/(kf2 - kf1)
ob.keyframe_insert(data_path = “location”, frame = i)
ob.keyframe_insert(data_path = “rotation_euler”, frame = i)
Hey. Two things here.
The amount of elements in range(kf1 = 21) is 21.
The amount of elements in range(kf1 = 21, kf2= 41) is 41 - 21 = 20.
So you are actually animating scale over 21 frames, and rotation over 20.
I also don’t see any reason to animate location.
Now, to your problem. The reason you are getting 4.5 degrees of rotation since the start of the animation is because you set the object to be rotated 4.5 degrees at frame 21, but you never set an initial keyframe (at frame=0), and blender (by default), sets rotation to be 4.5 degrees at every frame before.
Hope that helps,
Regards 
Hey Nacho,
Thanks for your reply and help!
I tried doing what I could gather from your message. However, it didn’t work 
So I changed the code to do the following:
- set an initial keyframe at frame = 0 for rotation and scale
- scaled over keyframes 1-20
- rotated over keyframes 21-40
however, it still didn’t change - during scaling, it now had an angle of 4.29 degrees (as opposed to 4.5 degrees earlier)
here’s the code again:
bpy.ops.object.select_all(action=‘SELECT’)
bpy.ops.object.delete(use_global=False)
add a cube
bpy.ops.mesh.primitive_cube_add(size=0, location=(0, 0, 0))
create a reference for it
ob = bpy.context.object
set the number of keyframes to animate over
kf1 = 20
set the object’s scale to 0
ob.scale = 0,0,0
ob.keyframe_insert(data_path = “scale”, frame = 0)
#ob.keyframe_insert(data_path = “rotation_euler”, frame = 0)
animate the scale, starting from zero up to our desired scale
each frame will increment the scale by (scale/keyframe) units
for i in range(1, kf1):
ob.scale[0] += 6/kf1
ob.scale[1] += 2/kf1
ob.scale[2] += 0.25/kf1
ob.rotation_euler[0] = 0
ob.keyframe_insert(data_path = “scale”, frame = i)
rotate 90 degrees or 1.570796 radians over the next 20 keyframes
kf2 = 41
for i in range(kf1+1, kf2):
ob.rotation_euler[0] += 1.57096/(kf2 - kf1)
ob.keyframe_insert(data_path = “rotation_euler”, frame = i)
appreciate all the help i can get!
Thanks and regards