I was working with Blender on a large scale project and was using the Print3D add-on to obtain area and volume.
But Print3D ignores scene scale and returns everything in centimeters, not meters, which is a real pain, especially with the lack of commas to break up the dozen significant figures.
So I figured out some code to look it up and be more flexible. If it is METRIC and isn’t set to METERS then it defaults back to using centimeters.
But I’m an old, self-taught coder who isn’t current and I’d rather not screw things up.
Can someone who knows the process please review my code below for operators.py in '\scripts\addons\object_print3d_utils" ? New lines (7) are commented accordingly, and comma and indent for else block added to 2 original lines.
# ---------
# Mesh Info
class MESH_OT_print3d_info_volume(Operator):
bl_idname = "mesh.print3d_info_volume"
bl_label = "3D-Print Info Volume"
bl_description = "Report the volume of the active mesh"
def execute(self, context):
scene = context.scene
unit = scene.unit_settings
scale = 1.0 if unit.system == 'NONE' else unit.scale_length
length = "cm" if unit.system == 'NONE' else unit.length_unit #adds variable to check unit length in use
obj = context.active_object
bm = mesh_helpers.bmesh_copy_from_object(obj, apply_modifiers=True)
volume = bm.calc_volume()
bm.free()
if unit.system == 'METRIC':
volume_cm = volume * (scale ** 3.0) / (0.01 ** 3.0)
volume_metres = volume_cm/1000000
volume_fmt = f"{length}"
if length == "METERS": #uses new variable to apply correct format
volume_fmt = "{} m".format(clean_float(f"{volume_metres:,.2f}"))
else: #on else default to the original code, but with commas too
volume_fmt = "{} cm".format(clean_float(f"{volume_cm:,.4f}"))
elif unit.system == 'IMPERIAL':
volume_inch = volume * (scale ** 3.0) / (0.0254 ** 3.0)
volume_fmt = '{} "'.format(clean_float(f"{volume_inch:.4f}"))
else:
volume_fmt = clean_float(f"{volume:.8f}")
report.update((f"Volume: {volume_fmt}³", None))
return {'FINISHED'}
class MESH_OT_print3d_info_area(Operator):
bl_idname = "mesh.print3d_info_area"
bl_label = "3D-Print Info Area"
bl_description = "Report the surface area of the active mesh"
def execute(self, context):
scene = context.scene
unit = scene.unit_settings
scale = 1.0 if unit.system == 'NONE' else unit.scale_length
length = "cm" if unit.system == 'NONE' else unit.length_unit #adds variable to check unit length in use
obj = context.active_object
bm = mesh_helpers.bmesh_copy_from_object(obj, apply_modifiers=True)
area = mesh_helpers.bmesh_calc_area(bm)
bm.free()
if unit.system == 'METRIC':
area_cm = area * (scale ** 2.0) / (0.01 ** 2.0)
area_metres = area_cm /10000
if length == "METERS": #uses new variable to apply correct format**
area_fmt = "{} m".format(clean_float(f"{area_metres:,.2f}"))**
else: #on else default to the original code, but with commas too
area_fmt = "{} cm".format(clean_float(f"{area_cm:,.4f}"))
elif unit.system == 'IMPERIAL':
area_inch = area * (scale ** 2.0) / (0.0254 ** 2.0)
area_fmt = '{} "'.format(clean_float(f"{area_inch:.4f}"))
else:
area_fmt = clean_float(f"{area:.8f}")
report.update((f"Area: {area_fmt}²", None))
return {'FINISHED'}