How do I calculate the Volume of an Object?

MAXScript FAQ > MAXScript Values > How do I calculate the Volume of an Object?

There is currently no access to the Measure Utility from MAXScript, but you can use the following MAXScript function to get the same results:

SCRIPT

fn CalculateVolumeAndCenterOfMass obj =
(
local Volume= 0.0
local Center= [0.0, 0.0, 0.0]
local theMesh = snapshotasmesh obj
local numFaces = theMesh.numfaces
for i = 1 to numFaces do
(
  local Face= getFace theMesh i
  local vert2 = getVert theMesh Face.z
  local vert1 = getVert theMesh Face.y
  local vert0 = getVert theMesh Face.x
  local dV = Dot (Cross (vert1 - vert0) (vert2 - vert0)) vert0
  Volume+= dV
  Center+= (vert0 + vert1 + vert2) * dV
)
delete theMesh
Volume /= 6
Center /= 24
Center /= Volume
#(Volume,Center)
)
--Call the function on a geometry object - the result will be a list
--containing the Volume and the Center of mass in local space.
s = sphere()
theVolAndCom = CalculateVolumeAndCenterOfMass $Sphere001
--To get the world space of the Center of Mass just like in the Utility,
--you have to do some extra work:
theComInWorld = theVolAndCom[2] * $Sphere001.objectTransform

See Also