Sequential Batch Export

Hi,
I have been looking around batch export scripts on this forum and have not found a solution. I have automated Grasshopper to create sequential baking. The end goal is to bring the animation into Maya. If I choose to have 6 seconds then I will have 600 meshes.

Is there a way to export them as .obj files in the order that they are baked? Or possibly to skip storing them in Rhino altogether?

Thank you.

import rhinoscriptsyntax as rs
import Rhino


#load grsshopper plugin as GH
gh = Rhino.RhinoApp.GetPlugInObject("Grasshopper")

for func in dir(gh):
    if not func.startswith('_'): print func
    
    #SetSliderValue("GUID," Number)
    
rs.EnableRedraw(True)

for i in range(0,6,1):
    gh.SetSliderValue("ff322d6c-afb3-4bba-8e93-bde17d9499f7", i)
    gh.RunSolver("animationGH_Test2")
    
    #Bake data in object
    baked = gh.BakeDataInObject("e7188adf-dd85-4399-ad96-61fe88b556c7")
    
    #move baked iterations
    transVect = (0, 100*i, 0)
    rs.MoveObject(baked, transVect)

#export section that is not working
path = “C:\Users\ERIK SVEN BROBERG\Desktop\teste\test.obj” #C:\MyFolder\ObjectName.obj
cmd = "_-Export " + path + " _EnterEnd"
rs.Command(cmd)

I think you need to put the path in quotes and escape the backslashes in the path, or use string-literal string for it:

path = "C:\\Users\\ERIK SVEN BROBERG\\Desktop\\teste\\test.obj"
cmd = "_-Export \"" + path + "\" _EnterEnd"
path = r"C:\Users\ERIK SVEN BROBERG\Desktop\teste\test.obj"
cmd = "_-Export \"" + path + "\" _EnterEnd"
1 Like

Thank you Menno,

This is helpful and makes sense. This following option is also help for me; however, I cannot figure out how to suppress the rs.command options. For example, when I export as an .obj, every time I have to manually enter the polygon density. Can I predefine this data? I thought that adding a 'hyphen before the command in quotations would suppress this dialogue, but I suppose i need to predefine the values.

Thank you again for your help.

import rhinoscriptsyntax as rs
import Rhino
import Rhino.Geometry as rg

#load grsshopper plugin as GH
gh = Rhino.RhinoApp.GetPlugInObject("Grasshopper")
    
#SetSliderValue("GUID," Number)
    
rs.EnableRedraw(True)

#set working directory
workingDir = rs.BrowseForFolder(None, "Pick a folder to save files in")
rs.WorkingFolder(workingDir)

#create a variable for file naming
num = 0

for i in range(0,5,1):
    #slider animation
    gh.SetSliderValue("ff322d6c-afb3-4bba-8e93-bde17d9499f7", i)
    
    #bakedObjects
    gh.RunSolver("animationGH_Test2.gh")
    baked = gh.BakeDataInObject("e7188adf-dd85-4399-ad96-61fe88b556c7")
    vect = rs.VectorCreate(rg.Point3d(0,0,0), rg.Point3d(0,i*100,0))
    rs.MoveObject(baked, vect)
    
    #convert to string and add filename
    strNum = str(num)
    filename = "islandMassing" + strNum + ".obj"
    
    #call many rhino commands with this function
    #suppress dialogue by adding a '-'
    rs.Command("_SelNone", True)
    rs.Command("_SelLast", True)
    rs.Command("_-Export " + filename + " _Enter", False)
    rs.Command("_SelNone", True)
    
    num = num + 1

hmmm… I think that is because the export to .obj needs to convert the objects to meshes and therefore asks for the polygon density. If you were to convert the objects to meshes yourself (either from grasshopper or by scripting the Mesh command) I think your export would be without interrruptions (but I haven’t tested this).

Strangely, I removed the spaces from the line, " _Enter" to “_Enter” and now it works. I thought that adding a space after the initial quotation and before the string would emulate hitting the enter key; however, this seems to be working differently.

Thanks again for your help.

rs.Command("_-Export " + filename + “_Enter”, True)

Scratch that. I restarted Rhino and now I have to hit ‘enter’ between every save. Any advice on how to automate this with the above code would be greatly appreciated.

Thanks in advance.
Erik

Maybe repeat the Enter command?
rs.Command("-Export " + filename + " _Enter _Enter", True)

1 Like

Ok. This works perfectly. Thank you.

Why is this?

Thank you again for all the help.

I just noticed you had to press ENTER each time. I thought, why not script that ENTER as well? I’ve seen many scripts before in which the _Enter command was repeated several times.

To end a command you can also instead use _EnterEnd instead of _Enter _Enter.
With EnterEnd it will continue to enter untill the command ends.

3 Likes

Interesting. Thank you both. Good solutions that both work. I appreciate the help and insight.
Erik