Untitled

 avatar
unknown
plain_text
a month ago
2.2 kB
7
Indexable
import bpy

def process_selected_objects():
    # Store the currently selected objects
    selected_objects = bpy.context.selected_objects

    # Ensure there are selected objects to process
    if not selected_objects:
        print("No objects selected.")
        return

    # Loop through each selected object and perform an action
    for obj in selected_objects:
        # Set the object as active
        bpy.context.view_layer.objects.active = obj
        obj.select_set(True)

        print(f"Processing object: {obj.name}")

        # Ensure there's an active object
        if bpy.context.active_object is not None:
            # Switch to Edit Mode if currently in Object Mode
            if obj.mode != 'EDIT':
                bpy.ops.object.mode_set(mode='EDIT')
                print("Switched to Edit Mode.")
            else:
                print("Already in Edit Mode.")
        else:
            print("No active object to switch modes.")
            continue

        # Select all faces in edit mode
        bpy.ops.mesh.select_all(action='SELECT')

        # Fix Orientation
        area_type = 'VIEW_3D'
        areas = [area for area in bpy.context.window.screen.areas if area.type == area_type]
        bpy.context.scene.tool_settings.use_transform_data_origin = True

        with bpy.context.temp_override(area=areas[0]):
            bpy.ops.transform.create_orientation(use=True)
            bpy.ops.object.editmode_toggle()
            transform_type = bpy.context.scene.transform_orientation_slots[0].type
            bpy.ops.transform.transform(mode='ALIGN', orient_type='Face')
            bpy.ops.transform.delete_orientation(0)
            bpy.context.scene.tool_settings.use_transform_data_origin = False

        # Switch back to object mode after operation
        bpy.ops.object.mode_set(mode='OBJECT')

        # Deselect the object after processing to avoid interference
        obj.select_set(False)

    # Reselect all objects at the end (optional)
    for obj in selected_objects:
        obj.select_set(True)

# Execute the function
process_selected_objects()
Leave a Comment