2
$\begingroup$

I have created a grid of hexagons. I want to animate the rotation of a series of them diagonally, so I have parented each hexagon to its own Empty, and rotated the Empty's orientation to the angle I want to rotate the hexagons at. But when I try to rotate each Empty on its local x-axis using python script, I can't seem to get the expected rotation. What I've tried so far:

Selected Empty in 3D viewport, then ran (where the value "1.5708" is the equivalent of rotating 90 degrees):

bpy.context.object.rotation.euler[0] = 1.5708

or . . .

bpy.ops.transform.rotate(value=1.5708, 
                         axis=(0.866025, 0.5, 0), 
                         constraint_axis=(True, False, False), 
                         constraint_orientation='LOCAL', 
                         mirror=False,
                         proportional='DISABLED', 
                         proportional_edit_falloff='SMOOTH', 
                         proportional_size=1, 
                         release_confirm=True)

The first one results in an error:

Traceback (most recent call last):
File "", line 1, in
AttributeError: 'Object' object has no attribute 'rotation'

The second one rotates the Empty, but not according to local x-axis. (looks like maybe global x-axis)

Note: the above commands come from the info panel where first one is the same as setting rotation in the "Properties" panel (N key in 3D Viewport) and second one is the result of using the 3D Manipulation Widget

I am using Blender 2.73a on Mac OSX 10.8.5

$\endgroup$
  • $\begingroup$ The first one needs to be rotation_euler (underscore instead of . ) $\endgroup$ – Ray Mairlot Feb 7 '15 at 19:13
3
$\begingroup$

Your first attempt did not work because (as Ray Mairlot pointed out) the attribute is rotation_euler, not rotation.euler. Further more, with the proper notation it will only rotate the active object, not all objects selected.

This five line script rotates all selected objects by 90° on the local X axis.

import bpy
from math import pi
for ob in bpy.context.selected_objects:
    ob.rotation_euler[0] = pi/2
    ob.convert_space(from_space='LOCAL', to_space='WORLD')

The from math import pi line is just so we can use pi/2 instead of 1.5708 as the value to rotate by.

$\endgroup$
1
$\begingroup$

Rotate the matrix directly;

import bpy

from math import pi
from mathutils import Matrix

for obj in bpy.context.selected_objects:
    obj.matrix_world *= Matrix.Rotation(pi * 0.5, 4, 'X')
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.