Count number of occurences of a Name?


#1

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.


#2

Hi Matt,

Try the attached script:
exportNameOcc.py (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:
                namesColumn.append(name)
        if len(namesColumn) > 0: 
            namesColumnU = set(namesColumn)
            namesColumnU = list(namesColumnU)

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

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

        else:
            print "You do not have named objects. Function terminated"
            return

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

rs.Command("_SelAll")
objs_ids = rs.GetObjects("", preselect=True)
exportNameOcc(objs_ids)

(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


#4

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?