Script for Naming Imported STLs

I’m trying to do a script to import a number of STLs, and for each one imported to name it as the filename.

browsed = rs.BrowseForFolder(title = "Auto-name Imported Files - Location Picker")
files = rs.OpenFileNames(title = "Auto-name Imported Files - File Picker", folder=browsed)

for filename_iteration in files:
    rs.Command('_-Import ' + filename_iteration + ' _Enter')
    object_guid = rs.GetObject(preselect=True)
    rs.ObjectName(object_guid, filename_iteration)
    rs.Command ('_SelNone ')

The only thing so far thrwing it off is if a filename has a space in it e.g. filepath… 519430376-4 WO.stl

My guess is I need to ‘re-stringify’ each full filepath and loop those instead. So far I am just using the Rhino behaviour of auto selecting something imported to get the imported GUID and then rename it.

For a file called: 519430376-4 WO.stl, the below is reported:

C:\Users\j.hutchinson\Downloads\wetransfer-1ba4b3\519430376-4.3dm not found, unable to open

So after the space it’s assuming a 3dm.

change that line to this:

rs.Command('_-Import "{}" _Enter'.format(filename_iteration) )

this way the file name is parsed between double quotes not parsing the space in the name as an enter

does this help?
-Willem

That worked great!

Successfully read file “C:\Users\j.hutchinson\Downloads\wetransfer\519430376-4 WO.stl”

Next step will be to name them by chopping off the directory to just get “filename.fileformat…”.

Thank you Willem! So this kind of substitutes the filename within the string and interprets it, rather than the quotation marks being used to refer to it as a string?

You can use the os module to help splitting the file path string:

import rhinoscriptsyntax as rs
import os

browsed = rs.BrowseForFolder(title = "Auto-name Imported Files - Location Picker")
files = rs.OpenFileNames(title = "Auto-name Imported Files - File Picker", folder=browsed)

for filename_iteration in files:
    
    file_full_name = os.path.basename(filename_iteration)
    print 'file_full_name : {}'.format( file_full_name)
    file_name, extention =   os.path.splitext(file_full_name)
    print 'file_name : {}  extention : {}'.format(file_name, extention)
    
    rs.Command('_-Import "{}" _Enter')
    object_guid = rs.GetObject(preselect=True)
    rs.ObjectName(object_guid, filename_iteration)
    rs.Command ('_SelNone ')

Amazing! All I had to do was change rs.ObjectName to have file_full_name used instead of my filename_iteration.

NameImportedObjects.py (1.1 KB)

Thanks for the help Willem.

1 Like