I have created an operator to calculate the surface area of the currently selected faces. To enable selection across objects, it loops through context.selected_objects. Then for each of these, it reads the polygons with select=true.
The problem is, when I change my face selection manually in the viewport and re-run the operator, the select attribute doesn't seem to be changing in real-time. There's some delay, and as a result my plugin is reporting an inaccurate result.
Code is as follows:
class Utils():
def get_selected_object_faces(object):
mesh = object.data
polys = mesh.polygons
return [poly for poly in polys if poly.select]
def sq_meters_to_sq_feet(sq_meters, precision):
return round(sq_meters * 10.7639, precision);
class Calc_Selected_Face_Area(bpy.types.Operator):
"""Calculates Selected Face Area"""
bl_idname = "interior.calc_selected_face_area"
bl_label = ""
def execute(self, context):
selected_polys = []
for selected_object in context.selected_objects:
print(selected_object)
selected_polys += Utils.get_selected_object_faces(selected_object)
poly_areas = [Utils.sq_meters_to_sq_feet(poly.area, precision=0) for poly in selected_polys]
print(poly_areas)
context.scene.shared_props.areas = ", ".join([str(int(poly)) for poly in poly_areas])
return {'FINISHED'}
See specifically in the get_selected_object_faces method. The poly.select attirbute I'm reading in there, doesn't seem to be updating immediately when I change my face selection in the UI.
Any tips would be appreciated. New to Python and Blender.
The full script if you want to try it out.
Object.update_from_edit_mode()... however if to be used in edit mode would go for edit bmesh and using a live selection. – batFINGER Oct 01 '21 at 23:26