import bpy

def environmentLightingIsFromColor(blender_scene):
    world = blender_scene.world
    if world is not None:
        if world.use_nodes == False:
            return True
    return False

def getWorldColor(blender_scene):
    world = blender_scene.world
    if world is not None:
        return world.color
    return None

def getCurrentSkyboxPath(blender_scene) -> str or None:
        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
        if shading.use_scene_world_render == False:
            assignedEnvironmentLight = shading.selected_studio_light
            if assignedEnvironmentLight:
                exrPath = assignedEnvironmentLight.path
                return exrPath
        else:
            shading = blender_scene.display.shading
            assignedEnvironmentLight = shading.selected_studio_light
            if assignedEnvironmentLight:
                exrPath = assignedEnvironmentLight.path
                if exrPath is not None and exrPath != "":
                    return exrPath
            
            # get world background image
            world = blender_scene.world
            if world is not None:
                if world.use_nodes == False:
                    return None
                node_tree = world.node_tree
                if node_tree is not None:
                    nodes = node_tree.nodes
                    for node in nodes:
                        if node.type == 'TEX_ENVIRONMENT':
                            image = node.image
                            if image is not None:
                                return image.filepath
                
        return None

def getCurrentSkyboxImage(blender_scene) -> bpy.types.Image or None:
        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
        if shading.use_scene_world_render == False:
            assignedEnvironmentLight = shading.selected_studio_light
            return assignedEnvironmentLight
        else:
            shading = blender_scene.display.shading
            if shading is not None:
                return shading.selected_studio_light
            
            # get world background image
            world = blender_scene.world
            if world is not None:
                node_tree = world.node_tree
                if node_tree is not None:
                    nodes = node_tree.nodes
                    for node in nodes:
                        if node.type == 'TEX_ENVIRONMENT':
                            image = node.image
                            if image is not None:
                                return image
                
        return None