Python Script: Calculating the Volume of Objects

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?


It looks like you're using an ad blocker. I really need the income to keep this site running, so please enable advertisements on BlenderNation, or leave me a small donation. Everything helps!

Thanks!
  • http://mke3.net Matt

    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.

  • Filyp

    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. :)

  • aws357

    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.

  • http://www.blendernation.com Bart

    @Matt: cool, I learned something new :)

  • Esben Stien

    Parametric Solid Blender. Now we only need NURBS;)

  • freakydude

    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.

  • Michael Schardt

    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…

  • Michael Schardt

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

  • Niko Suni

    @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.

  • Michael Schardt

    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.

  • http://yorik.orgfree.com Yorik

    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)…

  • Niko Suni

    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.

  • http://www.quantumanomaly.com Quantum Anomaly

    I wonder how accurately this will calculate sphere volumes? If I could use a NURBS sphere that would be ideal, but maybe it would be close enough if the sphere is high-poly.

  • http://www.blendernation.com Bart

    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*

  • miq

    where is sctipt?

  • FunkyWyrm

    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.

  • ErebusNco

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

  • http://www.artdds.com Alexander Zubkov

    A simple tool for Volume calculation: http://www.artdds.com/studio/toolbox/vol_n_mass_calc/

    • http://pancake.io/545359/thepoison.htm abitgeekie

      That link no longer works. Is there another link to it? Thanks