bl_info = {
    "name": "My Random Rotator",
    "blender": (3, 0, 0),
    "category": "Object",
    "author": "ChatGPT",
    "version": (1, 0),
    "description": "Randomly rotate selected objects on chosen axes.",
}

import bpy, random, math
from bpy.props import BoolProperty, FloatProperty

class OBJECT_OT_my_random_rotator(bpy.types.Operator):
    bl_idname = "object.my_random_rotator"
    bl_label = "My Random Rotator"
    bl_options = {'REGISTER', 'UNDO'}

    min_angle: FloatProperty(name="Min Angle (°)", default=-45.0)
    max_angle: FloatProperty(name="Max Angle (°)", default=45.0)

    use_x: BoolProperty(name="X Axis", default=False)
    use_y: BoolProperty(name="Y Axis", default=False)
    use_z: BoolProperty(name="Z Axis", default=True)

    local_space: BoolProperty(name="Use Local Axes", default=True)

    def invoke(self, context, event):
        return context.window_manager.invoke_props_dialog(self)

    def execute(self, context):
        min_r = math.radians(self.min_angle)
        max_r = math.radians(self.max_angle)

        for obj in context.selected_objects:
            if self.use_x:
                ang = random.uniform(min_r, max_r)
                if self.local_space: obj.rotation_euler.rotate_axis('X', ang)
                else: obj.rotation_euler.x += ang

            if self.use_y:
                ang = random.uniform(min_r, max_r)
                if self.local_space: obj.rotation_euler.rotate_axis('Y', ang)
                else: obj.rotation_euler.y += ang

            if self.use_z:
                ang = random.uniform(min_r, max_r)
                if self.local_space: obj.rotation_euler.rotate_axis('Z', ang)
                else: obj.rotation_euler.z += ang

        return {'FINISHED'}

class VIEW3D_PT_my_random_rotator(bpy.types.Panel):
    bl_label = "My Random Rotator"
    bl_idname = "VIEW3D_PT_my_random_rotator"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_category = "Random"

    def draw(self, context):
        layout = self.layout
        layout.operator("object.my_random_rotator")

classes = (OBJECT_OT_my_random_rotator, VIEW3D_PT_my_random_rotator)

def register():
    for c in classes: bpy.utils.register_class(c)

def unregister():
    for c in reversed(classes): bpy.utils.unregister_class(c)
