"""Fast mesh data access with numpy.""" import numpy as np def polygon_arrays(mesh): """Return (loop_start, loop_total, loop_vertex_index) as int arrays.""" n_polys = len(mesh.polygons) starts = np.empty(n_polys, dtype=np.int32) totals = np.empty(n_polys, dtype=np.int32) mesh.polygons.foreach_get("loop_start", starts) mesh.polygons.foreach_get("loop_total", totals) loop_verts = np.empty(len(mesh.loops), dtype=np.int32) mesh.loops.foreach_get("vertex_index", loop_verts) return starts, totals, loop_verts def loop_polygon_index(totals): """Polygon index of every loop.""" return np.repeat(np.arange(len(totals), dtype=np.int32), totals) def vertex_coords(mesh): co = np.empty(len(mesh.vertices) * 3, dtype=np.float64) mesh.vertices.foreach_get("co", co) return co.reshape(-1, 3) def polygon_normals(mesh): normals = np.empty(len(mesh.polygons) * 3, dtype=np.float64) mesh.polygon_normals.foreach_get("vector", normals) return normals.reshape(-1, 3)