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
Alltaken
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.
%<
http://blenderartists.org/forum/showthread.php?t=49216
It wasnât Bullx, it was MrMunkily:
http://blenderartists.org/forum/showthread.php?t=17579
%<
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.
eurekaâŚ
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.
pseudo code:
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
âdo nothing
finished all faces
print A
Will work for all meshes including ones with holes â even completely enclosed interior holes (carefull with the normals though)
GreyBeard
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).
http://members.shaw.ca/rjplus/volume.py
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.
GreyBeard
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?
macouno,
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
from:
mymesh = NMesh.GetRawFromObject(obname)
to:
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
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:
http://mywebpages.comcast.net/erniew/getstuff/areavol.html
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.
http://www.st-v-sw.net/STSWvolumetrics.html
I had wanted something similar for Blender, and now I have it.
Thanks again!
Nyrath, cool now you can make awsome websites like that
This is very, very useful script!!! Thanks!