Need help - creating array

Hi everyone,
Bare with me, I used to know Maxscript pretty well, and could write different scripts/macros to perform different tasks. This was over 10 years ago now, and this isn’t Max or Maxscript, this is Rhino and Python. I’ve roughed out this script, which works for one pre-selected object, as long as your units are set to CM by default. It then outputs the weight of the solid/closed object in kg:

import rhinoscriptsyntax as rs
import scriptcontext as sc

def ObjectWeight():
    intUnits = rs.UnitSystem(1)
    factor = rs.UnitScale(intUnits, 3)
    obj = rs.SelectedObjects()
    vol = rs.SurfaceVolume(obj)

    volume = vol[0]
    print "Volume =", volume
    print "Objects Weight in Aluminum is:", ((volume * 2.73)/1000), "kg"


I need to figure out how to create an array so I can collect the volume for each object pre-selected, run ‘ObjectWeight()’ on it, and add up all of the volumes, and do my weight calculation based on density.

for items in obj() do ObjectWeight()

I forget how its laid out, how to add together each value from the array into a new value. I know i’m way in over my head, but if I can learn how to do this again, I’ll be able to stumble through the rest I think.


Hi Carl,

You are setting your unit system to microns, not centimeters.
Check the script bellow.

import rhinoscriptsyntax as rs

def objectWeight():
    unitSystem = rs.UnitSystem()
    lengthFactor = rs.UnitScale(3, unitSystem)
    volumeFactor = lengthFactor**3
    ids = rs.GetObjects("choose the objects", 16, preselect=True)
    # materials and densities
    materials = ["Aluminium", "Brass", "Bronze", "Copper", "Gold", "Iron", "Lead", "Magnesium", "Mercury", "Nickel", "Platinum", "Silver", "Silver, Sterling", "Steel", "Titanium", "Tungsten", "Zinc"]
    densities_gr_per_cm3 = [2.72, 8.72, 8.8, 8.96, 19.28, 7.84, 11.36, 1.76, 13.52, 8.88, 21.44, 10.48, 10.32, 8.0, 4.48, 19.28, 7.12]
    material = rs.ListBox(materials, "choose the material", default = "Aluminium")
    if ids and material:
        materialIndex = materials.index(material)
        totalVolume = 0
        for id in ids:
            if rs.IsPolysurfaceClosed(id):
                volume = rs.SurfaceVolume(id)[0]
            totalVolume += volume * volumeFactor  # in cm3
        totalWeight_kg = densities_gr_per_cm3[materialIndex] * totalVolume / 1000 # in kg
        print "total objects Volume: %s cm3" % totalVolume
        print "total objects Weight in %s: %s kg" % (material, totalWeight_kg)


That’s awesome, I’ll give it a try. How come you set ‘totalVolume = 0’ before running the array? To reset the totalVolume value in memory so it doesn’t keep adding up?

Thank you

Hi Carl,

Before adding up volumes (totalVolume += volume * volumeFactor), one needs to assign an initial value to the totalVolume variable. Which is 0.

Hey djordje,
Thanks again, works great. Will be using this one a fair bit as I design.