Get Volume Centroid of Multiple Objects

Hi, I would like to take the volume centroids of lots of boxes (closed solid polysurfaces).

When I select all the boxes (hundreds of them) and use VolumeCentroid, it gives me the centroid of their grouped bounding box. I need the individual centroid of each object selected. It’s easy enough if I’m just selecting a few boxes but I need to do it for hundreds of them.

For example, I’ve got some boxes:

When I select them all and use volumeCentroid I get their combined centroid, not what I want.

This is the result I want, a centroid for each selected volume.

The reason I can’t do it box-by-box is I have a lot of boxes.

I’m not sure if I need .rvb or grasshopper for this. If I do, that’s fine, just please be specific because I don’t have any experience using them.

Thank you!

With so many objects, I’d use Grasshopper.

had this floating around

Option Explicit
'adds a Dot at the selected objects' centroid
'with the object volume in current file units

' Rhino.AddStartupScript Rhino.LastLoadedScriptFile
' Rhino.AddAlias "VolumeTag","_NoEcho _-Runscript (VolumeTag)"

Call VolumeTag()
Sub VolumeTag()

	Dim arrObj, strObj,  strVol, arrVolume, strVolume, arrCent, arr3dCent, arrThings, strDot, strGroup

	arrObj = Rhino.GetObjects("Select closed objects for volume tag", 8 + 16 + 32,, True)

	If IsArray(arrObj) Then

		For Each strObj In arrobj

			arrvolume = Null
			arrCent = Null
			arr3dCent = Null


			If Rhino.IsSurface(strObj) Or Rhino.isPolysurface(strObj) Then

				If Rhino.isObjectSolid(strObj) Then
					arrVolume = Rhino.SurfaceVolume(strObj)
					strVolume = Round(arrVolume(0), 4)
					arrCent = Rhino.SurfaceVolumeCentroid(strObj)
					arr3dCent = arrCent(0)
				End If

			ElseIf  Rhino.IsMesh(strObj) Then

				If Rhino.IsMeshClosed(strObj) Then
					arrVolume = Rhino.MeshVolume(strObj)
					strVolume = Round(arrVolume(1), 4)
					arr3dCent = Rhino.MeshAreaCentroid(strObj)
				End If

			End If

			If IsArray(arr3dCent) Then
				strDot = Rhino.AddTextDot("Vol = " & strVolume, arr3dCent)
				strgroup = Rhino.addGroup
				arrThings = array(strDot, strObj)
				Rhino.AddObjectsToGroup arrThings, strGroup
			End If

		Next

	End If

End Sub

My ‘laying around’ version -

VolCentroidEach.py (549 Bytes)

To use the Python script use RunPythonScript, or a macro:

_-RunPythonScript "Full path to py file inside double-quotes"

-Pascal

Thank you Pascal! This is exactly what I needed.

Hi Bahji, I am trying to accomplish same thing. Can you please share a gh file or a screenshot of how exactly you used the script (new to grasshopper). Thank you

Hi!

I thought this would solve my problem, but the script doesn’t seem to work with open meshes, or bad polysurfaces which is what I have to work with at the moment. Is there a script that would work in my situation?

Thanks!
-Christopher

objects that are open don’t contain a volume (if you tip a jar without its top on, the sauce falls on the floor) and bad objects are constructed in a way that creates ambiguity about what’s inside/outside. if the geometry is relatively simple, rhino can calc a centroid, but in general both open objects and bad objects lead to unpredictable volume centroid calculations.

ie, fix the geometry to be closed before running volume centroid. if on R8, you can run shrink-wrap to get a general volume