Is there a python script to calculate the volume of a model?
I really doubt it. It’s not a trivial problem. If you really need it you’ll probably have to code it yourself or convince someone else to. Here is a paper that explains one method of doing it: http://amp.ece.cmu.edu/Publication/Cha/icip01_Cha.pdf
If the reason you want it is for a game, you’re probably better off just guessing at a number that works well with the physics.
I have an idea.
Now I work on a model that I want to print on a 3d wax printer. So this is why I need the volume.
This printer works simple: it build the model from bottom to top by printing the section planes.
So we need a script that slice the model to planes (virtually) and calculate the pixels on the plane that is in the body of the model.
I am pretty sure that things like that can be done in Catia, at least I saw it done on my university. Is it necessary to do it in blender? I presume you don’t have 3d printer at home, so is that a college assignment? Nice little thing, right?!
just drop the model into a fluid sim bath, with an overflow into a known volume like a cube-jug thing with markings.
might take a while but will be fairly accurate i would hope (how detailed is the model, it might be plus/minus a bit, probably will over-estimate the volume)
yes its a huge workaround, but hey
With a bit of calculus it is easily doable. Just slice the model into very thin planes, maybe about .01 apart. Then find vertices at the intersections of the original tris and that plane. Then do a triangle fill. Then find the area of the triangles. Then multiply the sum of all the areas by the thickness (.01) and then take the sum of all those.
And that should do it.
It should be doable with sum bounding box-tree method methinks- those functions might be already in blender to use for raytracing or so- won’t be perfectly accurate but the more you subdivide the closer you get. It’s doable, but I guess the functions will be slow if you do it all in python ?
Scorpius wrote a script for surface volume and (if I remember correctly) Bullx did the ‘paper layers’ thing but ended up using some other program (both in about 2003 though). I’ll see what I can find in search.
It wasn’t Bullx, it was MrMunkily:
I’ve got a script online that does surface areas and such… volume is tricky though. There are a lot of standards for volume as well.
Fligh: surface volume doesn’t sound like volume at all… a surface is normally quite flat.
Slicing a model shouldn’t be too hard, tricky but not too hard. very codeable.
Alltaken: I think that’s how Socrates came to idea of mass; at least that’s the urban legend he sat in a bathtub and it overflowed.
Umm, that was Archimedes in the bathtub. Socrates was the guy they poisoned for corrupting the youth. His main interest was politics and logic, not physics.
I can give you an algorithm but don’t have time to code it. The mesh must be completely triangulated, manifold, with normals pointing outside. Imagine a plane (say zplane) that is parallel to the z axis located some distance underneath your model.
area A = 0
iterate through all faces (triangles)
if the face normal vector’s z component is positive
–calculate the volume between the face and zplane and add to A
if the face normal vector’s z component is negative
–calculate the volume between the face and zplane and subtract from A
if the face normal vector’s z component is zero
finished all faces
Will work for all meshes including ones with holes – even completely enclosed interior holes (carefull with the normals though)
PS: its based on the way surveyors calculate areas and volumes from coordinates. I believe its the same method as discussed in RobCossens link but in terms a non-mathematician can understand.
I had some time this evening so I banged out the script (It just prints the volume to the console).
Here was one of my test objects:
The volume if it was a true sphere is calculated by hand to be 1.814 and the script gives a volume of 1.800 using the triangulated surface.
P.S. be sure to heed the warnings or you will get incorrect results.
Thank you! I will try this script!
GreyBeard, very cool… I know more people have been looking for this. I’ll test it out later.
Would you mind if I added a version that also works for quads to my odd measurements script (seems natural to have this function in there), and would you have a link I can use to give you credit?
Help yourself to the code. I am unfamiliar with python and the differences between NMesh and Mesh so I don’t know when I am creating a copy of the mesh data or referencing the original data — line 24 can probably be changed
mymesh = NMesh.GetRawFromObject(obname)
mymesh = obj.getData()
I am also not sure if “set smooth” changes the stored mesh data’s face normals – doing a “set solid” would probably be a good idea. Also apply any subsurfing before the triangulation of the mesh – it appears that NMesh stores the subsurfed verts as well as the cage. The blender wiki python cookbook already has the code to convert the mesh to triangles.
Attribution is not required (only a few lines of code). I just found the algorithm rather elegant.
GreyBeard, thank you so much! I had been looking for something like this for years.
You see, there is a script like this for Lightwave:
There are science fiction fans in general, and Star Trek fans in particular, who obsess over such pointless issues as what is the internal volume of the starship Enterprise. Since the ships are seldom simple Euclidean shapes, this is not easy.
A fan was using the Lightwave script to determine the volume of various trek ships.
I had wanted something similar for Blender, and now I have it.
Nyrath, cool now you can make awsome websites like that
This is very, very useful script!!! Thanks!