0

I am running the following code. I would like to rotate the selected blocks about the x-axis in the center. However, it is rotating about the corner. Can I ask for assistance?

import bpy
from mathutils import Matrix, Vector
import math

angle = math.radians(45)


for obj in bpy.data.objects:

    if(obj.location.x > 1.1):
        obj.select_set(True)
        print(obj.location.x)
        bpy.context.view_layer.objects.active = obj



  bpy.ops.transform.rotate(value=math.radians(90) , orient_axis='X')
  print("hello33")

enter image description here

2

1 Answer 1

1

I just played around a bit and came up with this:

import bpy
from mathutils import Matrix, Vector
import math

angle = math.radians(45)

# store old pivot setting
old_pivot = bpy.context.scene.tool_settings.transform_pivot_point 
# set pivot point to bounding box
bpy.context.scene.tool_settings.transform_pivot_point = "BOUNDING_BOX_CENTER"
# --> https://docs.blender.org/api/current/bpy.types.ToolSettings.html#bpy.types.ToolSettings.transform_pivot_point

for obj in bpy.data.objects:
    if(obj.location.x > 1.1):
        obj.select_set(True)
        print(obj.location.x)
        bpy.context.view_layer.objects.active = obj

bpy.ops.transform.rotate(value=math.radians(45), orient_axis='X')

# restore old pivot setting
bpy.context.scene.tool_settings.transform_pivot_point = old_pivot

I only added the three lines. One to store the current setting of the pivot point, 2nd set the pivot point to "bounding box" and restore the old setting.

Also, I changed the angle to 45° so I can actually see the changes ;) Hope this works for you too. I tested with Blender 4.3.0.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.