Rhino script to select all object and export as .iges file

Hi all,

I have made an object using Grasshopper and baked the model using bake objects component. What I am trying to do, as a next step is that I am trying to export the baked model as .iges file from Rhino using a rhino python script.

The code I am currently having is this

import rhinoscriptsyntax as rs
def ExportIGESFile():
filter=“IGES files|.igs||”
filename=rs.SaveFileName(“Iges export”,filter)
if not filename: return
rs.Command("_-Export “+chr(34)+filename+chr(34)+” _Enter",False)
ExportIGESFile()

This coding is NOT able to choose the baked model, I still manually have to choose the baked model before running the script.

what code should I add to make the baked model automatically chosen and export as an iges file?

Regards

You are running this python script directly in Rhino? Yes, objects need to be selected for the command Export to export something. If the script is running just after the GH bake operation, and it’s only one object, you can use obj=rs.FirstObject() to get the GUID of the object that was just created and rs.SelectObject(obj) to select it.

Otherwise, if you are trying to export from inside GH, I think you need something like the Human or Elefront plug-ins…

Thank you for the suggestion,

I think you understood my question properly.

Yes, I am trying to run the script after the GH bake operation (I used bake objects component in GH so baking is not a problem).

I am quite new in Rhino script, so where should I put the obj=rs.FirstObject() & rs.SelectObject(obj) on top of my script?

Regards

Something like this:

import rhinoscriptsyntax as rs

def ExportIGESFile():
    filter="IGES files|.igs||"
    filename=rs.SaveFileName("Iges export",filter)
    if not filename: return
    #make sure nothing is selected first
    rs.UnselectAllObjects()
    #select your baked object here
    rs.SelectObject(rs.FirstObject())
    rs.Command("_-Export "+chr(34)+filename+chr(34)+" _Enter",False)
ExportIGESFile()

(To post code here and have it formatted correctly like above, enclose the code in three “backticks” like follows)

```python   (the python part is optional)

paste your code in here

```

There are other possibilities to select objects, for example rs.SelectObjects(rs.NormalObjects()) will select everything selectable in the file. Check the rhinoscriptsyntax for more possibilities.

1 Like

I really appreciate it perfectly works!!!

Can I ask one more question?

I am changing the values of the object’s dimension (length, height) in G.H and would like to delete the previously baked object (‘delete’ means remove the baked object from Rhino screen) and bake only new object, select the new object and export as .iges file (overwrite the .iges file name when it is saved) whenever I change the values of the object’s dimension. How can I develop the script to perform this process?

I hope my explanation is enough you to understand.
Cheers

For that I think you will definitely need some plug-in for GH that references the baked objects - I don’t exactly know how that works, you might post the question to the GH section of the forum.

Thank you I will post to the G.H section forum

Cheers

Hello - I think I’d store the baked objects on a sticky

ids = rs.LastCreatedObjects() scriptcontext.sticky[baked_ids] = ids

so that you can get at them later. Or if it is one object or the objects are essentially the same and you know what is what, you can use

sctriptcontext.doc.objects.Replace(oldID, newObject)

https://developer.rhino3d.com/api/RhinoCommon/html/M_Rhino_DocObjects_Tables_ObjectTable_Replace_25.htm

-Pascal

The thing is, rs.LastCreatedObjects() does not seem to pick up GH baked objects… at least not here.

Yeah, I saw that - it has to be created in the script with a Rhino.Command() to be picked up. FirstObject() will get at least the one. GrasshopperBake might be good enough for LastCreatedObjects(), I need to try…

You can name, group or add user text in baking, that might be enough to keep track as well.

-Pascal

This works though (just after bake):

import rhinoscriptsyntax as rs
rs.UnselectAllObjects()
rs.Command("_SelLast")
objs=rs.SelectedObjects()

Hi,

Where should I locate this script to here

import rhinoscriptsyntax as rs

def ExportIGESFile():
filter=“IGES files|.igs||”
filename=rs.SaveFileName(“Iges export”,filter)
if not filename: return
#make sure nothing is selected first
rs.UnselectAllObjects()
#select your baked object here
rs.SelectObject(rs.FirstObject())
rs.Command("_-Export “+chr(34)+filename+chr(34)+” _Enter",False)
ExportIGESFile()

Not sure I understand your question - do you want to know how to run the script from an alias or toolbar button?

Sorry- my question was not precise enough.

So, the script you wrote

import rhinoscriptsyntax as rs
rs.UnselectAllObjects()
rs.Command("_SelLast")
objs=rs.SelectedObjects()

can you explain more about this?
Would this work for selecting only the last baked object?

No, this will work to select all the objects in the last “bake” operation - that is why I posted it.

So, your script might look like this:

import rhinoscriptsyntax as rs

def ExportIGESFile():
    filter="IGES files|.igs||"
    filename=rs.SaveFileName("Iges export",filter)
    if not filename: return
    #make sure nothing is selected first
    rs.UnselectAllObjects()
    #select your just-baked object(s) here - will get them all
    rs.Command("_SelLast")
    rs.Command("_-Export "+chr(34)+filename+chr(34)+" _Enter",False)
ExportIGESFile()

You do need to run the script just after the bake operation and before you have created or selected any new objects in the file.

2 Likes

Thank you it works!! really appreciate

Hi,

When I run the script (just above), only the last baked object is perfectly baked.
However, I still have to manually type a file name when this is exported.

I would like to define file name as well in the script so that I do not have to manually type the file name after running the script. How script should be in this case?

Cheers

Well the chr(34)+filename+chr(34) part is the full path to the file plus the name. Since you called rs.SaveFileName() above, it will open the browse dialog and ask you for the file name - that is its purpose.

If you want to bypass the dialog, you will need to somehow specify the full path and filename in the script instead of using rs.SaveFilename(). Something like:

filename=r"C:\Users\<yourusername>\Desktop\my_export_file.igs"

I use a “raw” string here (“r” prefix) to avoid having to use double backslashes in the path - single slashes in non-raw strings are escaped.

How do I set the export file type ‘filter’ in Rhino script (not Python)?

I want to export .obj
Do the settings I make interactively persist for the script?
In particular I need to uncheck the ‘Create NGons’
Thanks

Hello - if you name your file, on export, the specified extension (.obj,.igs) will determine the exported format and the options will be offered accordingly. A good way to figure out how to specify options is to run the -Export command and see what OBJ options are.

-Pascal