Rhino for Mac / Python script for batch-converting .stp to .obj

I tried to write the following script for opening all files from one folder and saving them in an other one.
However, it’s not working and I can’t figure out what I’m doing wrong. Im working with Version 5.3.2

Someone here that can point me to what exactly is wrong with this script?

import rhinoscriptsyntax as rs
import os.path

def BatchSaveAs():

    folder = rs.BrowseForFolder("/Users/jehrenstein/Desktop/TEST/stp")

    if folder: print folder

    saveFolder = rs.BrowseForFolder("/Users/jehrenstein/Desktop/TEST/obj")

    if saveFolder: print saveFolder

    for filename in os.listdir(folder):
        if filename.endswith(".stp"):
            fullpath = os.path.join(folder,filename).lower()
            rs.EnableRedraw(False)
            rs.DocumentModified(False)
            rs.Command("_-New _None",False)


            rs.Command('_-Open {} _Enter'.format(fullpath))
            comm="_-Export"
            rs.Command(comm + chr(34) + saveFolder + "/" + filename + chr(34) + ".obj" + "_Enter")


    rs.DocumentModified(False)
    rs.Command("_-New _None",False)
    rs.EnableRedraw(True)

BatchSaveAs()

I didn’t chekc your code, but using double slashes in path names with python scripts does marvelous things… :sunglasses:

// Rolf

… in Windows, where slashes are BACKslashes … :wink:

OSX is Unix … slashes are on the right side … :sunglasses:

Hi Jonas,

I have no Mac … just guessing …
Haven’t you to select the objects before you can export them ?

Regards

i don’t think rs.BrowseForFolder works on Mac.

after a quick test, it appears to always return none.

Hi guys,

first of all: thank you for your replies!

I just started with Rhino and Python, so I’m really thankful for any help.

@RIL I’m with emilio, but thanks for your input Rolf!
@emilio I added “SelAll” to the commands
@jeff_hammond do I even need it, or can I just use the path as string?

import rhinoscriptsyntax as rs
import os.path

def BatchSaveAs():

    folder = "/Users/jehrenstein/Desktop/TEST/stp"

    if folder: print folder

    saveFolder = "/Users/jehrenstein/Desktop/TEST/obj"

    if saveFolder: print saveFolder

    for filename in os.listdir(folder):
        if filename.endswith(".stp"):
            fullpath = os.path.join(folder,filename).lower()
            rs.EnableRedraw(False)
            rs.DocumentModified(False)
            rs.Command("_-New _None",False)


            rs.Command('_-Open {} _Enter'.format(fullpath))
            rs.Command("_-SelAll")
            comm="_-Export"
            rs.Command(comm + chr(34) + saveFolder + "/" + filename + chr(34) + ".obj" + "_Enter")


    rs.DocumentModified(False)
    rs.Command("_-New _None",False)
    rs.EnableRedraw(True)

BatchSaveAs()

Thanks,

Jonas

import rhinoscriptsyntax as rs
import os.path

def BatchSaveAs():

    folder = "/Users/jeff/Desktop/stp"
    saveFolder = "/Users/jeff/Desktop/obj"

    for filename in os.listdir(folder):
        if filename.endswith(".stp"):
            fullpath = os.path.join(folder,filename).lower()
            rs.Command("-Import {} _Enter".format(fullpath))
            comm="_-ExportAll "
            rs.Command(comm + chr(34) + saveFolder + "/" + filename + ".obj" + chr(34)  + " _Enter" + " _Enter")
            rs.Command("Delete")
    
BatchSaveAs()

that works… it’s using -Import instead of -Open because i don’t think Open is scriptable on Mac but Import is.

so, open a blank file in Rhino and run the script from there… it just imports then deletes when finished instead of opening a new file then closing it.


(oh… the Users/jeff path won’t work… switch it back to your user name :wink: )

Thats exactly what I was going for. Thanks alot for your help! @jeff_hammond

I’m trying to do something similar on a windows computer.

I successfully cycle through the script but when it comes to the Export (I’ve also tried Save), it prompts me to save the filename as opposed to using the existing filename.

Any ideas?

import rhinoscriptsyntax as rs
import os.path

def BatchSaveAs():

#folder = "/Users/Owner/Desktop/hold/"
#saveFolder = "/Users/Owner/Desktop/hold1/"

folder = "C:\Users\Owner\Desktop\hold"
saveFolder = "C:\Users\Owner\Desktop\hold1"

for filename in os.listdir(folder):
    if filename.endswith(".3dm"):
        fullpath = os.path.join(folder,filename).lower()
        rs.Command("-Import {} _Enter".format(fullpath))
       
        
        rs.Command ("_Import " "C:\Desktop\Parts\Sphere.3dm ")

        
        rs.Command ("_SelAll ")
        comm="_Export "
        rs.Command(comm + chr(34) + saveFolder + "/" + filename + ".3dm" + chr(34)  + " _Enter" + " _Enter")
        rs.Command(comm + chr(34) + saveFolder + "/" + filename + ".3dm" + chr(34)  + " _Enter" + " _Enter")
        rs.Command ("_SelAll ")
        rs.Command("_Delete ")
   BatchSaveAs()

Put a “-“ (dash) in front of Export to avoid the save dialog… i.e. _-Export

That worked. Thanks so much Mitch! This forum is amazing!