import bpy

# https://docs.blender.org/api/current/bpy.types.View3DShading.html
# https://blender.stackexchange.com/questions/17745/changing-viewport-shading-with-python

# get list of builtin EXRs https://blender.stackexchange.com/questions/235501/how-can-i-get-the-filepaths-of-all-default-built-in-hdris


class setEnvironmentOp(bpy.types.Operator):
    bl_idname = 'scene.set_environment'
    bl_label = 'Set Environment'
    bl_options = {"REGISTER"}

    def execute(self, context):
        area = next(area for area in bpy.context.screen.areas if area.type == 'VIEW_3D')
        space = next(space for space in area.spaces if space.type == 'VIEW_3D')
        shading = space.shading
        print(space)
        shading.type = 'MATERIAL'
        shading.studiolight_intensity = 0.5
        assignedEnvironmentLight = shading.selected_studio_light
        if assignedEnvironmentLight:
            exrPath = assignedEnvironmentLight.path
            print(exrPath)
        # print(light, light.path)
        # lights = bpy.context.preferences.studio_lights
        # # find light
        # for light in lights:
        #     print(light.name)
        #     if light.name == light.name:
        #         print(light)
        #         break
        return {"FINISHED"} 