1

I have created a cube using python in blender

bpy.ops.mesh.primitive_cube_add(radius=1, location=(x, y, z))  

I want to rotate the cube around its z-axis with a random angle between -180, 180 degrees. Is there an argument or do I need a new line of code? How do i accomplish this?

Thanks!

andrea
  • 103
  • 3
  • 14
  • 1
    this is probably a better question for http://blender.stackexchange.com/ if it's not in the [documentation](https://www.blender.org/api/blender_python_api_2_78a_release/info_quickstart.html) – Aaron Dec 06 '16 at 20:05

1 Answers1

1

You can add a rotation argument to primitive_cube_Add. Note that python rotation options use radians, so you may need to use math.radians(x)

bpy.ops.mesh.primitive_cube_add(radius=1, location=(x,y,z), rotation=(rx,ry,rz))

You can also directly change the rotatation of the object after you have created it. After primitive_cube_add() the new object is selected and is the active object.

bpy.context.active_object.rotation_mode = 'XYZ'
bpy.context.active_object.rotation_euler = (rx, ry, rz)

While the above example works it is more correct to specify the rotation using a mathutils.Euler or mathutils.Quaternion object.

sambler
  • 6,917
  • 1
  • 16
  • 23