Export (object name to file name) & Import (file name to object name)

Hi all
For STL export I use script by @Helvetosaur

! _-Runscript (
Option Explicit
'Script by Mitch Heynick
'Version 21 March 2012
'added current file name and folder default

Call ExportSTL()

Sub ExportSTL()
	
	Dim arrObjs,strCurrDP,strCurrDN,strSaveDN,strFileName,strSett,filt	
	
	arrObjs = Rhino.GetObjects("Select objects to export as .stl", 8 + 16 + 32,, True)
	If Not IsArray(arrObjs) Then Exit Sub
	
	strCurrDP = Rhino.DocumentPath()
	strCurrDN = Rhino.DocumentName()
	filt = "STL Files (*.stl)|*.stl||"
	
	If Not IsNull(strCurrDN) Then
		strSaveDN =Left(strCurrDN,Len(strCurrDN)-3)&"stl"
		strFileName = Rhino.SaveFileName("Export STL", filt, strCurrDP, strSaveDN)
	Else
		strFileName = Rhino.SaveFileName("Export STL", filt)
	End If
	If IsNull(strFilename) Then Exit Sub
	
	strSett = STLSettings() 'gets your settings from the function below
	If IsNull(strSett) Then
		Call Rhino.Print("Unable to get export settings")
		Exit Sub
	End If
	Call Rhino.SelectObjects(arrObjs) 'selects your objects
	
	'runs the export using the file name/path and your settings
	Call Rhino.Command("-_Export " & chr(34) & strFileName & chr(34) & " " & strSett, False)
	
End Sub

Function STLSettings()
	
	STLSettings = Null
	Dim str1,str2,str3,str4,str5,str6,str7,str8,str9,str10
	Dim str11,str12,str13,str14,str15,str16,str17,str18
	Dim strComb
	
	str1 = "_ExportFileAs=_Binary "
	str2 = "_ExportUnfinishedObjects=_Yes "
	str3 = "_UseSimpleDialog=_No "
	str4 = "_UseSimpleParameters=_No "
	str5 = "_Enter _DetailedOptions "
	str6 = "_JaggedSeams=_No "
	str7 = "_PackTextures=_No "
	str8 = "_Refine=_Yes "
	str9 = "_SimplePlane=_No "
	str10 = "_Weld=_No "
	str11 = "_AdvancedOptions "
	str12 = "_Angle=15 "
	str13 = "_AspectRatio=0 "
	str14 = "_Distance=0.001 "
	str15 = "_Grid=16 "
	str16 = "_MaxEdgeLength=0 "
	str17 = "_MinEdgeLength=0.0001 "
	str18 = "_Enter _Enter"
	
	strComb = str1 & str2 & str3 & str4 & str5 & str6 & str7 & str8 & str9 & str10
	strComb = strComb & str11 & str12 & str13 & str14 & str15 & str16 & str17 & str18
	
	STLSettings = strComb
End Function

))

Hide

It works beautifully, but at the moment important wishes have ripened. In my work for each project I get a lot scans in STL format, each file has its own name. I need to import all this mass to Rhino and write the names manually. It is too long. Then, having slightly adjusted the models, I have to export each of them to a separate OBJ file and save its name as file name, and this also takes a lot of time. I would like to receive help in writing a script for automation this proces.

Is it possible to modify this script so that take and assign name from properties and save it name as name STL file

And another script for import STL.
So that to take the name of the imported STL and assign this name to properties

Thanks

2020-03-11_3-03-49

An interim solution if you are interested.

Import STP’s as blocks (drag drop)

Reference in Elefront, Deconstruct Block, Bake Geometry with Object Name as block name.

Reference Objects & Export to OBJ with same name.

Hi @leex -

Sorry for the delay. Here is the import script that will name the objects with the original file name.

BatchImportSTLwObjName.py (1.7 KB)

As far as exporting objects with the object name as the file name, that is a bit more problematic - several objects in Rhino can have the same name - exporting those to the same directory with the same file name will result in a conflict - one or more of the files will get overwritten. Objects in Rhino might also have no object name - what should be the file name in that case?

2 Likes

Hi Rickson! Thanks for idea!

Hi Mitch!
I was late, It was night :slight_smile:
Your script for importing STL works fine! Thank you so much bro! Is it possible to make the right to select certain STL files? And remove the STL extension from the obgect name.
And I can modify this for the OBJ format by replacing the STL to OBJ?

By export. As a rule, usually the names of my STLs contain numbers. Therefore, so that there is no confusion, if there are the same names, add letters at the end of the name in alphabetical order, separating them with an underscore. As exemple CurrentName_a CurrentName_b CurrentName_c
If there is no name, then assign it “NoName_a”, “NoName_b” …

Thanks :slight_smile:

Here’s what I have so far for Import - haven’t had time for the export scripts.
BatchImportSTLwObjName.py (1.7 KB)
SelectImportSTLwObjName.py (1.6 KB)
BatchImportObjwObjName.py (1.7 KB)
SelectImportObjwObjName.py (1.7 KB)

The ‘Batch’ scripts import every file of the specified file type in a given folder. The ‘Select’ scripts let you browse to a folder and choose which files you want to import.

Let me know how these work for you - maybe have more time tomorrow to do the rest.

2 Likes

Hi Mitch

Three scripts work just fine - as I wanted
It’s great )))
But BatchImportObjwObjName.py does not work. And I see the information on the command line:
No .obj files found in selected folder!

Hmm, odd - seems to work here in my tests… are there indeed some .obj files in the selected folder?

I have identified the cause.
If the file extension is written in capital letters, then it does not open, and if in lower case then yes

Ahh, yes, case sensitivity - forgot about that. Will need to fix all of them, hang on.

Actually, it’s just the two batch importers that need to be fixed… try these.

BatchImportObjwObjName.py (1.8 KB)
BatchImportSTLwObjName.py (1.8 KB)

1 Like

Cool, what is need!
Heartfelt thanks!!!

Here is the first .obj export script. Let me know how it works for you. I did my best to try to assure that file names are unique for export…

(removed, get new version below)

Works great!
Thank you Mitch! :sun_with_face:
I’ll try to replace something for exporting STL. I hope it works out.
I think in the lines where is OBJ replace it with STL

Everything is great! But there is one small note.
The script does not remember the history of the catalog with the last export from the script.
With a new export, it goes to the directory where the file was opened from, or to the folder where the last standard save was without a script.
Is it possible to teach a script to remember the folder where there was a previous save by script?

It turned out not so simple. I made a replacement. Seems that something is saving, but the script is executed with errors

No, that’s because the STL export settings are not the same as the OBJ settings - I have both here, I will put up an STL version in a bit.

I think so, let me see if I can store the last used folder path in sticky and recall it.
OK, I have that done - now, one more question -

As the script is written, if you run it once and export items from a file into a folder, the filenames will be the object names as designed. If the objects do not have a name for example, they will be named “Unnamed-A.obj” (-B, -C, etc.). That’s fine, but if you run the script again with a different file to the same destination folder, any objects with the same name (i.e like an unnamed object) will generate the same file name and overwrite the previous one with the same name in that folder. Is this going to cause a problem?

Edit: OK, that was an interesting problem to occupy the little gray cells…
So, the following version actually checks the selected directory for existing files of the same name to prevent overwriting.

Edit 2: And the STL exporter as well. Note, I did add one small detail to the OBJ exporter, so you can also replace the previous one with the one below.

BatchExportOBJByObjectwName.py (4.7 KB)

BatchExportSTLByObjectwName.py (4.4 KB)

3 Likes

Yes, now remembers the history of the last folders. It’s great that it prevents overwriting in the current directory
A lot of thanks Mitch! :slight_smile: