import bpy
import numpy as np
from PIL import Image

def load_depth_map(path):
    img = Image.open(path)
    img = img.convert('L')  # Convert to grayscale
    return np.array(img)

def create_vertices(depth_data, depth_scale=10.0):
    height, width = depth_data.shape
    vertices = []
    for y in range(height):
        for x in range(width):
            z = depth_data[y, x] / 255.0 * depth_scale  # Adjust depth scale to enhance depth effect
            vertices.append((x / width * 2 - 1, y / height * 2 - 1, z))
    return vertices

def create_faces(height, width):
    faces = []
    for y in range(height - 1):
        for x in range(width - 1):
            a = y * width + x
            b = y * width + (x + 1)
            c = (y + 1) * width + (x + 1)
            d = (y + 1) * width + x
            faces.append((a, b, c, d))
    return faces

def create_mesh(name, vertices, faces):
    mesh = bpy.data.meshes.new(name)
    mesh.from_pydata(vertices, [], faces)
    mesh.update()
    return mesh

def create_object(mesh_name, obj_name, vertices, faces):
    mesh = create_mesh(mesh_name, vertices, faces)
    obj = bpy.data.objects.new(obj_name, mesh)
    bpy.context.collection.objects.link(obj)
    return obj

def main(depth_map_path, texture_path):
    depth_data = load_depth_map(depth_map_path)
    vertices = create_vertices(depth_data)
    faces = create_faces(depth_data.shape[0], depth_data.shape[1])
    obj = create_object("DepthMapMesh", "DepthMapObj", vertices, faces)

    # Ensure the object is selected
    bpy.context.view_layer.objects.active = obj
    obj.select_set(True)

    # Switch to edit mode to calculate normals
    bpy.ops.object.mode_set(mode='EDIT')
    bpy.ops.mesh.normals_make_consistent(inside=False)
    bpy.ops.object.mode_set(mode='OBJECT')

    # Add UV mapping
    bpy.ops.object.mode_set(mode='EDIT')
    bpy.ops.uv.smart_project()
    bpy.ops.object.mode_set(mode='OBJECT')

    # Create a new material
    material = bpy.data.materials.new(name="DepthMapMaterial")
    material.use_nodes = True
    bsdf = material.node_tree.nodes["Principled BSDF"]

    # Load the texture image
    tex_image_node = material.node_tree.nodes.new('ShaderNodeTexImage')
    tex_image_node.image = bpy.data.images.load(texture_path)
    material.node_tree.links.new(bsdf.inputs['Base Color'], tex_image_node.outputs['Color'])

    # Assign the material to the object
    if obj.data.materials:
        obj.data.materials[0] = material
    else:
        obj.data.materials.append(material)

    # Optionally: Update 3D View to focus on the new mesh
    # Ensure this part only runs in a suitable context
    for area in bpy.context.screen.areas:
        if area.type == 'VIEW_3D':
            override = bpy.context.copy()
            override['area'] = area
            override['region'] = area.regions[-1]

            # Set the object's origin
            bpy.ops.object.origin_set(override, type='ORIGIN_GEOMETRY', center='BOUNDS')
            # Update view to focus on the object
            bpy.ops.view3d.view_all()

# Paths to your depth map image and texture image
depth_map_path = r"C:\Users\Vusum\Downloads\image (2).png"
texture_path = r"C:\Users\Vusum\level_1_background.jpeg"
main(depth_map_path, texture_path) this is a blender script that takes a depth map and normal image to create a 3-d mesh its a 3-d game level maker was original concept. i need you to code it making it into a actual level with tangible and moveable objects and buildings based on how map looks after.