Count number of occurences of a Name?


Is it possible to find the number of occurrences (I’d use ‘instances’ but I know that infers blocks, which these are not) of a particular name in a model? I’ve got loads of parts in a model that have all been named and show as much in Properties; it would be useful to be able to count the quantity of each. Better still, export the data to a spreadsheet.


Hi Matt,

Try the attached script: (1.5 KB)

It will export object names and its occurrences in .csv file (in two separate columns).

import rhinoscriptsyntax as rs

def exportNameOcc(_objs_ids):
    namesColumn = []
    if _objs_ids:
        for obj in _objs_ids:
            name = rs.ObjectName(obj)
            if name:
        if len(namesColumn) > 0: 
            namesColumnU = set(namesColumn)
            namesColumnU = list(namesColumnU)

            countColumn = []
            for nU in namesColumnU:
                   number = namesColumn.count(nU)

            mergedL = []
            for i in range(len(namesColumnU)):
                mergedL.append((countColumn[i], namesColumnU[i]))
            filename = rs.SaveFileName("Save csv file","*.csv||", None, "ObjectNamesOccurrences", "csv")
            file = open(filename, 'w')
            headerline = "Names, Occurrences\n"
            for i in range(len(namesColumnU)):
                name = mergedL[i][1]
                occ = mergedL[i][0]
                line = "%s,%i \n" %(name, occ)
            print "Done"

            print "You do not have named objects. Function terminated"

        print "Your 3dm file is empty or you did not select objects. Function terminated"

objs_ids = rs.GetObjects("", preselect=True)

(Steve Baer) #3

A longer way to do this than the script that djordje presented would be to run the SelName command and read how many items were selected on the command line. I would guess that the script is the best route, just providing this in the case that you didn’t know about the SelName command


Thank you for the tips. I wasn’t aware of SelName, so that helps in the short term and it allows me to get an idea of how many different names are in the file. It looks as though the script will do what I’m after - I’ll give it a go!

I’ve not used a Python script before. Is a different treatment required to set it up, compared to a normal script?