Object Centroids?

How do I get object centroids? This returns zeros. I tried a bunch of things but still get zeros. I totally know it’s completely wrong, but I don’t know how to figure it out:

def get_volume_centroid(objref):
obj_type = objref.Object().ObjectType
brep = objref.Brep()
mesh = objref.Mesh()

if brep and brep.IsSolid:
    mp = Rhino.Geometry.VolumeMassProperties.Compute(brep)
    if mp:
        VolCent = ("{0}".format(mp.Centroid))
        return VolCent
elif mesh and mesh.IsSolid:
    mp = Rhino.Geometry.VolumeMassProperties.Compute(mesh)
    if mp:
        VolCent = ("{0}".format(mp.Centroid))
        return VolCent    
else:
    VolCent = 0
    return VolCent

Hi Peter - I am getting the expected result here. what happens if you put a breakpoint at the ‘if’ and trace through?

-Pascal

Like this seems a bit simpler:

import Rhino
go = Rhino.Input.Custom.GetObject()
go.Get()
objref = go.Object(0)
def get_volume_centroid(objref):
    
    if not objref.Object():
        return
    obj = objref.Object().Geometry
    
    if type(obj) == Rhino.Geometry.Brep or Rhino.Geometry.Extrusion or Rhino.Geometry.Mesh:
        if obj.IsSolid:
            mp = Rhino.Geometry.VolumeMassProperties.Compute(obj)
            if mp:
                VolCent = ("{0}".format(mp.Centroid))
                return VolCent
    else:
        VolCent = 0
        return VolCent
        
print get_volume_centroid(objref)

Thanks - I see my issue!

Thanks, but for some reason that doesn’t work with SubDs. I tried adding that as a filter but it didn’t work.

in that case place this in:

if type(obj) = Rhino.Geometry.SubD:
    obj = obj.ToBrep()

Thanks!

If you’re dealing with a number of different geometry types - like you seem to be -, and you don’t want to specify individual code to get the centroid for each of them, and you don’t need an exact center point, you can simply get the bounding box of the geometry object in question, and assume the bounding box center as a pseudo-centroid of the object.