# Simple rotation animation using command-line python script

I've been trying to generate a simple rotation animation entirely using a python script from command-line. The animation file gets generated with the configured frames and all, but the rotation applied to the cube object doesn't reflect in the animation. Could you please point out to me what I'm doing wrong?

My code involves sample.blend, which is a blender file with the default scene with cube, and renderAnim.py (code given below). I invoke this from the command-line as: blender -b sample.blend -P renderAnim.py

import bpy

scene = bpy.data.scenes["Scene"]
mycube = bpy.data.objects['Cube']
mycube.rotation_mode = 'XYZ'

scene.frame_start = 1
scene.frame_end = 100

scene.frame_current = 1
mycube.keyframe_insert('rotation_euler', index=0 ,frame=1)

scene.frame_current = 100
mycube.rotation_euler = (0,0,180)
mycube.keyframe_insert('rotation_euler', index=0 ,frame=100)

scene.render.use_stamp = 1
scene.render.stamp_background = (0,0,0,1)

scene.render.filepath = "render/anim"
scene.render.image_settings.file_format = "AVI_JPEG"
bpy.ops.render.render(animation=True)


In these two lines, you are rotating on the z-axis and then keying the x-axis, which means you aren't doing any animation.

mycube.rotation_euler = (0,0,180) # (x, y, z)
mycube.keyframe_insert('rotation_euler', index=0 ,frame=100) # index=0 is x-axis


Changing the axis you keyframe will most likely not provide the expected result. While by default blender's UI uses degrees for rotation, when setting rotations in python you need to specify in radians, which means it will be rotating over 10,000 degrees in 100 frames.

Also note that to properly change the current frame you should use frame_set(), but you don't have to change frames in your script as the keyframe_insert() specifies what frame the value is set for.

import bpy
import math

scene = bpy.data.scenes["Scene"]
mycube = bpy.data.objects['Cube']
mycube.rotation_mode = 'XYZ'

scene.frame_start = 1
scene.frame_end = 100

mycube.rotation_euler = (0, 0, 0)
mycube.keyframe_insert('rotation_euler', index=2 ,frame=1)

mycube.rotation_euler = (0, 0, math.radians(180))
mycube.keyframe_insert('rotation_euler', index=2 ,frame=100)

scene.render.use_stamp = 1
scene.render.stamp_background = (0,0,0,1)

scene.render.filepath = "render/anim"
scene.render.image_settings.file_format = "AVI_JPEG"
bpy.ops.render.render(animation=True)

• Thanks a lot @sambler - your explanation clarifies the issue, and the code works perfectly! – Tedy Kanjirathinkal Feb 9 '16 at 17:20
• I am very new to python scripting, but this script is very interesting for me. If I want to animate more geometries togheter, after selecting them, what changes would be necessary? – Diego May 29 '19 at 23:05
• Put the steps you want to repeat into a loop that goes through all selected objects, like this. Feel free to create a new question to get a better explanation - that is how this site works. – sambler May 30 '19 at 2:51