Advertisement

You're blocking ads, which pay for BlenderNation. Read about other ways to support us.

Python Script: Calculating the Volume of Objects

21

testobj.jpgUpdate 14/01/2014: You no longer need this script - this functionality is now included in the new 3D printing toolbox that was added in Blender 2.67.

If you need to calculate the volume of an object, here's a nice script that will help you do it. It was coded by GreyBeard following a question on BlenderArtists.

There are a few things to keep in mind:

  • Mesh must be triangulated (ctrl-t)
  • Mesh must me manifold
  • Must have normals pointing outside
  • Apply size and rotation before using (ctrl-a)

If you're unfamiliar with running external Python scripts, here's how to use it: open the Text Editor window and load the script into it. In the 3D window, select the object whose volume you want to calculate, then in the Text Editor window press ALT+R to run the script. The result will be displayed in the console:

term.png

OSX Users: if you need a console, you'll need to start Blender from the commandline. Open a terminal and enter the following command:

/Applications/Blender/Blender.app/Contents/MacOS/blender

Further down the discussion, Yorik uses this script in another way: he has modified it so that it will spit out a bill of materials for his architectural projects. Quite clever - I wonder what else you could use it for :)

This script is still a little rough around the edges, for example it doesn't handle object scaling well yet, and of course it could do with a nice GUI. Maybe someone is interested in improving it?

About the Author

Avatar image for Bart Veldhuizen
Bart Veldhuizen

I have a LONG history with Blender - I wrote some of the earliest Blender tutorials, worked for Not a Number and helped run the crowdfunding campaign that open sourced Blender (the first one on the internet!). I founded BlenderNation in 2006 and have been editing it every single day since then ;-) I also run the Blender Artists forum and I'm Head of Community at Sketchfab.

21 Comments

  1. OS X users don't have to use the terminal, you can just run blender as normal and use the 'Console' application in the Utilities folder :) Though it would make a lot more sense to just report the value back in the UI with a notification.

  2. That script is so primitive. I dont understand why somebody didnt it sooner.
    There is only one expresion for each face. Complexicity O(N) where N is number of faces. I think it could be added as internal function to Blender. For example like length or angle of edges. :)

  3. Well, one thing it could be used for outside of 3D and architecture... Molecular science.
    Just export a molecular surface (in vrml, .obj whatever) and you can obtain the volume. Brilliant.

  4. The linked thread describes certain methods of going about this, one of them being slicing the mesh up into tiny planes, which made me wonder (yes, offtopic, so sorry) is there a way to evenly cut/subdivide a mesh along a certain axis? Suppose I have a belt with a relief/motives in it, I want to deform it using a curve, I could of course do xxx time using the knife script or something which takes forever to make an understatement, but is there a way or a script to cut a mesh in even parts?
    There are soo many little things I keep finding in blender lately, that and my dizzying deadline makes my head spin... I need a store that sells time.

  5. Michael Schardt on

    Not sure if it's identical, but this is what i used for one of my scripts:

    import Blender; from Blender import *

    def Volume(mesh):
    volume = 0.0
    for face in mesh.faces: volume += face.area * (face.cent * face.no)
    return volume / 3.0

    mesh = Object.GetSelected()[0].getData(0, 1)
    print Volume(mesh)

    Idea is to sum the signed volumes of tetrahedrons (each formed by a triangle and the origin)
    The signed volume is 1/3 * tiranglearea * signed distance of the triangle hyperplane to the origin.
    = 1/3 * face.area * dotvecs(facecenter, facenormal)
    The division is just delayed until the summation is done.
    The mesh must be converted to triangles also, normals pointing outside.
    It gives the same values as the script posted above in most cases.
    Only for a susanne model (converted to triangles) it gives slightly different values - I don't know if this is just a floating point rounding issue. I'll need to figure out how the script does the calculation (understand what's going on geometrically) to see...

  6. Michael Schardt on

    Sorry - tabs got lost.
    The three codelines under "def Volume(mesh):" should be intended of course.

  7. @Michael: Your method only works if the mesh is a convex manifold with regard to its center - that is, each and every triangle is unobstructed from the center by any other triangle. In the case of concave manifold (like Suzanne), parts of some of the tetrahedrons may end up outside the mesh - causing the inaccuracy.

  8. Michael Schardt on

    Not sure about the convexity thing since i'm summing signed values - result was correct for a torus for example.
    Well - i used a "square torus" for testing - something that looks like this:
    -------
    | --- |
    | | | |
    | --- |
    -------

    ... easy check - simply count boxes...

    One little thing though: i'm not summing tetrahedrons (wrong expression) but triangle pyramids.

  9. Wow, great advertising for that script! I also didn't know mesh volume calculation could be that simple. Greybeard, who made the script, says it is a method widely used in engineering. In what I've read too, it looks like it is a very standard way of calculating volume in the 3D world. I guess the complication is with all those "accidents" in meshes, like inverted normals, etc... Probably to make the script very, very accurate, one should make routines that check and fix all those possible bad modelling, errors, distorsions and accidents.

    If you follow down the blenderartists thread, you'll see that Hiower worked a bit more on the script and already solved a couple of things, it handles scaled meshes correctly now (you don't need to apply scale anymore)...

  10. Yea, I followed you on the "tetrahedrons" but pyramid is the correct primitive here :)

    You're right about the convex/concave issue; I didn't take into account the consideration of signed values.

  11. Sheesh, I needed a Blender volume calculation script today, googled, and what was at the top of the search results: this post that I wrote myself one year ago!

    *celebrates google and a bad memory*

  12. Excellent script, thanks to all who contributed to it :-)

    Various versions of the script can be found in the Blenderartists thread linked in the main article.

  13. The linked page to access the script is no longer available. Does anyone know where I can get it?
    Cheers !

Leave A Reply

To add a profile picture to your message, register your email address with Gravatar.com. To protect your email address, create an account on BlenderNation and log in when posting a message.

Advertisement

×