I need to find the top surface area (in one plane) and total surface area (by summing all the faces) of a complicated mesh. For that I need to know how to fit a plane to the top of a mesh to find the top surface area. The mesh can be .obj or .stl or .ply
1 Answer
$\begingroup$
$\endgroup$
1
Here as requested is faces whose normal vector has angle from up vector of less than 80 degrees.
import bpy
import bmesh
from math import radians
from mathutils import Vector
obj = bpy.context.active_object
mw = obj.matrix_world
bm = bmesh.new()
bm.from_mesh(obj.data)
up = Vector((0, 0, 1))
# local space
area = sum(f.calc_area() for f in bm.faces if up.angle(f.normal) < radians(80))
print("local", area)
# global space
area = sum(f.calc_area() for f in bm.faces if up.angle(mw * f.normal) < radians(80))
print("global", area)
# or avoid a lot of matrix mult by converting "up" to local space.
up = mw.inverted() * up
area = sum(f.calc_area() for f in bm.faces if up.angle(f.normal) < radians(80))
print("global", area)
bm.free()
-
$\begingroup$ That works great. How would that look for the ground or floor plane? I do have mesh with boxes representing buildings and would like to calculate the roof and ground surface. $\endgroup$ Commented Nov 1, 2023 at 15:49
area = sum(f.calc_area() for f in bm.faces if f.select)
Edit mode, Go to top view, box select visible, Object mode, then run code. Is that what you are after? $\endgroup$