Get an exported list of ID's of selected objects

Hello,

As a bit of a continuation of my last post, I’m trying to make a script work that based on a selection of objects gives me a list (either CSV or txt) that I can later use to read with another script to re select these objects again.

It is similar to the function “export selected - Object properties” with only Object ID selected.

I tried the following in python but that did not seem to work:

import rhinoscriptsyntax as rs

def main():
    # Prompt the user to select objects
    objects = rs.GetObjects("Select objects")
    
    if objects:
        # Create a new file to write the object IDs to
        file_path = rs.SaveFileName("Save object IDs as", "*.txt")
        
        if file_path:
            # Write the object IDs to the file
            with open(file_path, "w") as f:
                for obj in objects:
                    f.write(str(obj) + "\n")

Then I also tried this:

import rhinoscriptsyntax as rs

def main():
    # Prompt the user to select objects
    objects = rs.GetObjects("Select objects")
    
    if objects:
        # Get the IDs of the selected objects as a list
        object_ids = [rs.ObjectName(obj) for obj in objects]

        # Export the selected objects to a CSV file
        file_path = rs.SaveFileName("Export selected to CSV", "*.csv")
        rs.Command("_-Export {} _Enter _Objects {} _Enter".format(file_path, ",".join(object_ids)))

Also did not work.

Any help is greatly appreciated.

Best regards,
Bastiaan van Oudheusden

Hi @Bastiaan_van_Oudheus, below seems to do it:

import rhinoscriptsyntax as rs

def DoSomething():
    obj_ids = rs.GetObjects("Select objects", 0, False, True, False)
    if not obj_ids: return
    
    file_path = rs.SaveFileName ("Save", "Text Files (*.txt)|*.txt||", None, "ObjIds.txt")
    if not file_path: return
    
    with open(file_path, "w") as f:
        for obj_id in obj_ids:
            f.write("{}\n".format(obj_id))

if __name__=="__main__":
    DoSomething()

_
c.

1 Like