I’m hoping to create a script that can search a folder for all files ending in .obj and apply ReduceMesh to them (I have a large number of files to do this to).
So far my script is as so:
import os
import rhinoscriptsyntax as rs
def getObjHelper(dir_path, matches):
for root, dirnames, filenames in os.walk(dir_path):
for file in filenames:
if file.endswith('.obj'):
matches.append(os.path.join(root, file))
for dir in dirnames:
getObjHelper(dir, matches)
return matches
def getObjFiles(dir_path):
matches = []
for root, dirnames, filenames in os.walk(dir_path):
for file in filenames:
if file.endswith('.obj'):
matches.append(os.path.join(root, file))
for dir in dirnames:
getObjHelper(dir, matches)
return matches
def reduceFileSize(filepath):
filepath = filepath.replace(' ', "\\ ")
rs.DocumentModified(False)
print("Opening {0}".format(filepath))
rs.Command('_-Open {0} _Enter'.format(filepath))
rs.UnselectAllObjects()
rs.SelectObjects(id)
#rs.Command('_-ReduceMesh"')
#rs.Command('_-Export "' + filepath + _Enter _Enter')
#rs.UnselectAllObjects()
if __name__ == "__main__":
root_path = rs.BrowseForFolder()
obj_files = getObjFiles(root_path)
for file in obj_files:
reduceFileSize(file)
So far getObjFiles()
works as expected and I get a list of all .obj files. However, when I run the _-Open
command with rs.Command()
, I get an error where the filepath is truncated at the first space.
I’ve tried a handful of different methods to prepare the filepath string, but none seem to work – the script always cause issues at the first space. Given that these paths are automatically found using os.listdir
and os.walk
, I’m not sure what the best way to fix them is.
An example of the error I get, for selecting the folder E:\Physical Models\models\
is:
Opening E:\Physical\ Models\models\cat_stage01_model.obj
>>> ( Debugging=Off ): _-Open
Name of the file to open ( UpdatePromptUpdateBlocks=Yes Browse ): E:\Physical\
File E:\Physical\.3dm.3dm not found, unable to open
Any thoughts on how to do this?
thanks!