in tools.py [0:0]
def get_mesh_obj_data(context: Any, name: str):
try:
obj = bpy.data.objects.get(name)
if not obj:
raise ValueError(f"Object with name {name} not found")
if not obj.type == "MESH":
raise ValueError(f"Object with name {name} is not a mesh")
bounding_box = get_aabb(obj)
mesh = obj.data
bpy.context.view_layer.objects.active = obj
bpy.ops.object.mode_set(mode="EDIT")
bpy.ops.mesh.quads_convert_to_tris(quad_method="BEAUTY", ngon_method="BEAUTY")
bpy.ops.object.mode_set(mode="OBJECT")
min_coords = bounding_box[0]
max_coords = bounding_box[1]
ranges = [max_coords[i] - min_coords[i] for i in range(3)]
max_range = max(ranges)
vertices_with_indices = []
for i, vertex in enumerate(mesh.vertices):
world_vertex = obj.matrix_world @ vertex.co
quantized_vertex = [
int((world_vertex[0] - min_coords[0]) / max_range * 63),
int((world_vertex[2] - min_coords[2]) / max_range * 63),
int((world_vertex[1] - min_coords[1]) / max_range * 63),
]
vertices_with_indices.append((quantized_vertex, i))
vertices_with_indices.sort(key=lambda x: (x[0][0], x[0][1], x[0][2]))
old_to_new_indices = {
old_idx: new_idx
for new_idx, (_, old_idx) in enumerate(vertices_with_indices)
}
obj_lines = []
for vertex in vertices_with_indices:
v = vertex[0]
obj_lines.append(f"v {v[0]} {v[1]} {v[2]}")
faces = []
for face in mesh.polygons:
old_indices = list(face.vertices)
new_indices = [old_to_new_indices[old_idx] for old_idx in old_indices]
min_pos = new_indices.index(min(new_indices))
new_indices = new_indices[min_pos:] + new_indices[:min_pos]
faces.append(new_indices)
faces.sort(key=lambda x: (x[0], x[1], x[2]))
for face in faces:
obj_lines.append(f"f {face[0] + 1} {face[1] + 1} {face[2] + 1}")
obj_data = "\n".join(obj_lines)
return {"status": "success", "data": obj_data}
except Exception as e:
print(f"Error in llama_mesh_describe: {str(e)}")
traceback.print_exc()
return {"status": "error", "data": str(e)}