1
$\begingroup$

Using the 3D-Print Toolbox add-on you can obtain object volumes. I have a lot of objects in my scene that I would like to print the volume value from and I was hoping I could do this through python. When I manually click on the Analyse -> Statistics -> "volume button" nothing shows up in the info panel where usually a new entry shows up after an action in the blender UI.

I would highly appreciate any ideas or feedback!

Alex

enter image description here

$\endgroup$
0

4 Answers 4

1
$\begingroup$

If you look at the source code for the addon, all that operator is is a wrapper for the bmesh.calc_volume() function:

# from the addon
bm = mesh_helpers.bmesh_copy_from_object(obj, apply_modifiers=True)
volume = bm.calc_volume()
bm.free()

So you could probably just write something like:

import bpy
import bmesh


objs = bpy.context.selected_objects
for obj in objs:
    mesh = obj.data
    bm = bmesh.new()
    bm.from_mesh(mesh)
    vol = bm.calc_volume()
    bm.free()
    print(vol)

$\endgroup$
1
$\begingroup$

Navigating into the add-on's folder you can find the function under operators.py > MESH_OT_print3d_info_volume

enter image description here

from object_print3d_utils import mesh_helpers
import bpy

context = bpy.context
scene = context.scene
unit = scene.unit_settings
scale = 1.0 if unit.system == 'NONE' else unit.scale_length
obj = context.active_object

bm = mesh_helpers.bmesh_copy_from_object(obj, apply_modifiers=True)
volume = bm.calc_volume()
bm.free()

print("volume: ", volume)
$\endgroup$
0
$\begingroup$

It uses internal calc_volume function from bmesh. You can replicate it like this:

import bpy
import bmesh

objs = bpy.context.selected_objects

for obj in objs:
    me = obj.data
    if obj.mode == 'EDIT':
        bm = bmesh.from_edit_mesh(me)
    else:
        bm = bmesh.new()
        bm.from_mesh(me)
        
    volume = bm.calc_volume()

    print(volume)
$\endgroup$
0
$\begingroup$

i used this from simplifying the existing code:

from object_print3d_utils import operators, mesh_helpers
print(str(mesh_helpers.bmesh_copy_from_object(bpy.context.active_object, apply_modifiers=True).calc_volume()/(operators.get_unit(bpy.context.scene.unit_settings.system, bpy.context.scene.unit_settings.length_unit)[0]/bpy.context.scene.unit_settings.scale_length)**(3))+" "+operators.get_unit(bpy.context.scene.unit_settings.system, bpy.context.scene.unit_settings.length_unit)[1]+str("³"))
$\endgroup$
2
  • $\begingroup$ As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center. $\endgroup$
    – Daniel
    Commented Feb 22 at 0:49
  • $\begingroup$ How is it unclear? Perhaps it needs instructions? Select your object and run this script from the scripting workspace. Also, this code stands on its own without any add-on scripts. $\endgroup$ Commented May 28 at 19:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .