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 // 2):  # Reduce the height of the 3D model
        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) * 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):
    depth_data = load_depth_map(depth_map_path)
    vertices = create_vertices(depth_data)
    faces = create_faces(depth_data.shape[0] // 2, depth_data.shape[1])  # Reduce the height of the 3D model
    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"]

    # 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(override, center=True)

# Path to your depth map image
depth_map_path = r"C:\Users\Vusum\Downloads\image (2).png"
main(depth_map_path) got this error Python: Traceback (most recent call last):
  File "\Text", line 88, in <module>
  File "\Text", line 82, in main
  File "C:\Program Files\Blender Foundation\Blender 4.1\4.1\scripts\modules\bpy\ops.py", line 106, in __call__
    C_exec, C_undo = _BPyOpsSubModOp._parse_args(args)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Blender Foundation\Blender 4.1\4.1\scripts\modules\bpy\ops.py", line 60, in _parse_args
    raise ValueError("1-2 args execution context is supported")
ValueError: 1-2 args execution context is supported