in tools.py [0:0]
def llama_mesh_generate(context: Any, object_name: str, mesh_data: str):
try:
import bmesh
view_area_3d = next(
(area for area in context.screen.areas if area.type == "VIEW_3D"), None
)
if view_area_3d is None:
raise RuntimeError("View 3D area not found")
override = context.copy()
override["area"] = view_area_3d
with context.temp_override(**override):
mesh_data_obj = bpy.data.meshes.new(object_name)
mesh_obj = bpy.data.objects.new(object_name, mesh_data_obj)
context.collection.objects.link(mesh_obj)
bm = bmesh.new()
def add_vertex(x, y, z):
try:
bm.verts.new((x, y, z))
bm.verts.ensure_lookup_table()
return True
except ValueError as e:
print(f"Error adding vertex: ({x}, {y}, {z}): {e}")
return False
def add_face(a, b, c):
try:
bm.faces.new((bm.verts[a - 1], bm.verts[b - 1], bm.verts[c - 1]))
bm.faces.ensure_lookup_table()
return True
except IndexError as e:
print(f"IndexError adding face: ({a}, {b}, {c}): {e}")
return False
except ValueError as e:
print(f"ValueError adding face: ({a}, {b}, {c}): {e}")
return False
def process_line(line):
print(line)
line = line.strip()
if line.startswith("v "):
parts = line.split()
if len(parts) == 4:
try:
x, y, z = map(int, parts[1:])
scale = 1 / 64.0
add_vertex(
x * scale - 0.5, z * scale - 0.5, y * scale - 0.5
)
except ValueError:
pass
elif line.startswith("f "):
parts = line.split()
if len(parts) > 1:
try:
a, b, c = map(int, parts[1:])
add_face(a, b, c)
except ValueError:
pass
for line in mesh_data.splitlines():
process_line(line)
bm.to_mesh(mesh_data_obj)
mesh_data_obj.update()
context.view_layer.update()
bm.free()
return {"status": "success", "data": object_name}
except Exception as e:
print(f"Error in generate_mesh: {str(e)}")
traceback.print_exc()
return {"status": "error", "data": str(e)}