I want to rotate an object around the world origin, not the center of its bounding box, so the usual method of setting
.rotation_euler on the object as in this answer is not what I'm looking for. It seems I need to use
bpy.ops.transform.rotate, which has a problem when used in a scripting context that I had to work around when running Blender from the command line (
Below is my best attempt to create a cylinder, translate it along the X-axis, then rotate it around the Y-axis (and therefore around the origin).
import math import bpy # Delete the default cube. bpy.ops.object.delete(use_global=False) bpy.ops.mesh.primitive_cylinder_add() bpy.ops.transform.translate(value=(3, 0, 0)) # See https://stackoverflow.com/a/67697363. context = bpy.context.copy() for area in bpy.context.screen.areas: if area.type == "VIEW_3D": context["area"] = area break else: raise RuntimeError("VIEW_3D area not found") # Based on looking at the Python Console. bpy.ops.view3d.snap_cursor_to_center(context) bpy.context.scene.tool_settings.transform_pivot_point = "CURSOR" bpy.ops.transform.rotate( context, value=math.pi / 2, orient_axis="Y", orient_type="GLOBAL", )
This has two problems:
- It still apparently rotates the cylinder around the center of its bounding box rather than the origin. In particular, the two lines starting with
bpy.ops.view3d...seem to have no effect. (I got them from experimentation with the Python Console.)
- I get two DeprecationWarnings for my use of the
.rotate, which I took from the second linked StackOverflow answer.
Any ideas on how to rotate an object about the origin in a way that doesn't produce deprecation warnings?