neoedmund
neoedmund

Reputation: 601

Why rotate using python not work in blender 2.6?

I'm new to blender. Did I missed something?

however type: bpy.data.objects['Suzanne'].rotation_euler[2] = 1.25 in console window will make model to rotate.

But following code not rotate the model at all. Why?

import bpy
import math

cam = bpy.data.objects['Camera']
origin = bpy.data.objects['Suzanne']

step_count = 5
bpy.data.scenes["Scene"].cycles.samples=10

for step in range(0, step_count):
    r = math.pi * step * (360.0 / step_count) / 180.0
    print(r)
    origin.rotation_euler[2] = r       # seems not work!
    fn = '/tmp/mokey_%02d.jpg' % step
    print(fn)
    bpy.data.scenes["Scene"].render.filepath = fn
    bpy.ops.render.render( write_still=True )

Upvotes: 2

Views: 3588

Answers (1)

Random Citizen
Random Citizen

Reputation: 1376

Your code works perfectly fine. I've just verified it using Blender 2.71

However, rotation in blender has a small pitfall: There are multiple possible rotation-modes in blender. Modifying euler angles, won't have an effect, as long as a different rotation-mode is activated for the particular object.

You can enforce the right rotation-mode using the rotation_mode member (see the documentation for a full list of possible rotation-modes).
In your example, you probably want to use the xyz-Euler angles:

origin.rotation_mode = 'XYZ' # Force using euler angles

Here is the workaround integrated into your example:

import bpy
import math

cam = bpy.data.objects['Camera']
origin = bpy.data.objects['Suzanne']

step_count = 5
bpy.data.scenes["Scene"].cycles.samples=10

origin.rotation_mode = 'XYZ' # Force the right rotation mode

for step in range(0, step_count):
    r = math.pi * step * (360.0 / step_count) / 180.0
    print(r)
    origin.rotation_euler[2] = r
    fn = '/home/robert/mokey_%02d.jpg' % step
    print(fn)
    bpy.data.scenes["Scene"].render.filepath = fn
    bpy.ops.render.render( write_still=True )

Upvotes: 1

Related Questions