Export from script

I’m using rs.Command(“_Export”) in a script. Is there some way to call Export and have the script specify what file format I want so I don’t have to pick it from the list, and still let me name the file myself with the export dialog?

Yes, if you specify the full path including the extension, Rhino will know what file format to export. Of course, for certain file types, there are also going to be a number of command line export options to specify as well.

I can specify the path and the extension with the script, but I want to give it my own filename whenever I run this. I don’t suppose I can put a * in for the filename so it will give me the save-as dialog box and let assign a name? Is there a way I can just call the save-as dialog box with just a path and once I either give it a name or pick a file I want to overwrite, it will return whatever I selected and I can pass the full path / filename / extension to Export?

Two ways to do this in a script -

  1. throw up a StringBox dialog to get the filename and then add your path and extension to save as a specific file type in a specific place.

  2. Use SaveFileName() to open the save file and use the filter argument to filter the file format to the one you want. Rhino - RhinoScriptSyntax

Example of No. 1 with .dxf file extension:

import rhinoscriptsyntax as rs
import os

filename=rs.StringBox("Filename",title="Save DXF file as")
folder=rs.WorkingFolder() #or can use specific hard coded folder
fullpath=os.path.join(folder,filename+".dxf")
print fullpath

or

import rhinoscriptsyntax as rs
import os

folder=rs.BrowseForFolder() #user selects folder
filename=rs.StringBox("Filename",title="Save DXF file as")
fullpath=os.path.join(folder,filename+".dxf")
print fullpath

Example of No. 2 with .dxf file extension:

import rhinoscriptsyntax as rs

dxf_filt="DXF files|*.dxf||"
fullpath=rs.SaveFileName("Save .dxf file",dxf_filt)
print fullpath
1 Like

@Helvetosaur Thank You very much for the link and examples! I implemented method 2 as it makes it easy if I need to re-export things, I can just select them from the save-as dialog.

I saw that the parameters for SaveFileName are:

SaveFileName(title=None, filter=None, folder=None, filename=None, extension=None)

So I’m using this:

    filename = rs.SaveFileName ("Export iges", "Iges Files (*.igs)|*.igs||",rs.DocumentPath(),os.path.splitext(rs.DocumentName())[0])
    rs.Command("_-Export _Pause \"{0}\" _Enter".format(filename))

This does work, but when I type the _-Export command, I am given a way to select the Iges Type, units etc, so I expanded it like this:

rs.Command("_-Export _Pause \"{0}\" vectorcam UnitSystem=Inches  StringType=Unicode  Tolerance=0.00001 _Enter".format(filename))
    

And now it will always do the export in a consistent way, even if I mess up the Iges settings with some other project.

Thanks again for the help, this is exactly what I was hoping for!

If I cancel the SaveFileName dialog, it returns ‘None’ so to prevent from inadvertently saving a file named None.3dm I added a test for this:

filename = rs.SaveFileName ("Export iges", "Iges Files (*.igs)|*.igs||",rs.DocumentPath(),os.path.splitext(rs.DocumentName())[0])
if str(filename) <> "None":
   rs.Command("_-Export _Pause \"{0}\" vectorcam UnitSystem=Inches  StringType=Unicode  Tolerance=0.00001 _Enter".format(filename))

Yes, for brevity/clarity, I didn’t include any error checking in the code above. One should always check for valid input when using the interactive methods that require a user action, and also for any ‘risky’ operations where the result might be unexpected or fail.

While this works in the current version of IronPython, it’s not very good…

First, the <> symbol for not equals is deprecated, use != instead. <> will not run in Python 3.
Second, it’s not necessary to convert None to a string and compare to "None". You can write:

if filename is not None:
    #do something

#or

if filename != None
    #do something

However, as one can assume that if there is something returned by rs.SaveFileName(), the return will be a valid file path, one could simply use:

if filename:
    #do something

if xxx will return True if xxx is anything valid, i.e. the presence of ‘something’.

if xxx will return False if xxx is an empty string, None, False or 0 i.e. ‘nothing’.

Yes, zero is also considered to be ‘nothing’ in this case, so if you are looking at a method that could return a number and zero should also be a valid result, then you should not use

if xxx:
    #do something

which will fail if 0 is returned, but rather

if xxx is not None:
    #do something

which will allow you to continue with zero as a number result.

1 Like

@Helvetosaur Thank you for explaining all this, It helps a lot. I guess None is Python’s version of Null that I would normally be testing for… but it’s strange because converting it to a string results in a String “None” while converting a Null to a string would result in “”

If I understand this correctly, If I do:

if filename:

and filename is a string “0” or “None” then it would still fail?

but if I do

if filename != None:

and filename is a string “0” or “None” then it would pass?

I don’t think that distinction would matter in this case anyway because if I put a filename in SaveFileName, then I would get the .igs at the end anyway, so even if I put in None, I would be returned “None.igs” so since anything valid coming from SaveFileName would have the .igs at the end, I should be safe just doing:

if filename:

I appreciate the help and the explanation because this might not always be the case, in fact I think I will change my program to

if filename != None:

to remind me not to use <> and to also remind me that this is a more reliable test if the variable could be 0 and I would want the 0 to pass

Yes.

No, as rs.SaveFileName() always returns a string or None if it doesn’t get a valid input, so it will return “0” as a string which is not the same as 0 as a number. The same thing for None, "None" is a valid string and not the same thing as Python’s data type None. The method will not let you actually enter a completely invalid file name, if you enter just spaces or special characters, punctuation marks or similar, it simply won’t accept it.

If you enter None at the prompt for SaveFileName(), you will get something like
C:\Users\username\Desktop\None
as a file name…

I see, so the only way to get the data type None out of rs.SaveFileName() is to hit the cancel button.
Thanks for clarifying that.