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?
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…
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.
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.
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.
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()
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.
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?
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:
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.