# How to automate polygon reduction using python?

I have several models(more than 1000) in FBX format and I want to decrease their polygon count. I'm using decimate modifier. Since there are so many models and the task is not too complicated, I was wondering if it was possible to do this with python scripting. I looked around the internet but all the scripting tutorials I found were doing scripting inside the blender. But what I want is a script which scans all the models in a folder and outputs their low poly version.

• You can import and export and scan directories for certain files in Blender scripting so this kind of batch converter is entirely possible and quite straight forward to make. But are you sure a simple Decimate modifier will serve you well for all the >1000 models with one setting? Decimate modifier butchers mesh geometry and the raw end result is generally not considered a production ready 3D model. – kheetor May 11 '18 at 9:16
• @kheetor I'll be using LOD for my models. So, when the user gets near them, the high poly count version will be used. And if some models are not good enough, I'll do them manually. Anyway, If I'm unable to automate the process, I'll eventually have to do manually but if automation could be done, it would be great. Could you please provide links for import, export and applying decimate modifier scripting! – geekyJock May 11 '18 at 9:22
• You can find Blender API documentation here to get started: docs.blender.org/api/2.79/info_quickstart.html The general idea for your script could be make list of the files in given path and iterate through the files. If it's a .fbx file, use fbx importer on it. Then find all the mesh objects, add and apply decimate modifier, select mesh objects and use fbx exporter. Restoring the startup file between fbx files could be fast way to flush data from previous imports but at least delete the mesh and object data and armatures, materials and textures if you import any. – kheetor May 11 '18 at 9:48

I'd recommend using instant-meshes with it's batch mode. You could write a python script like this one to do all the files in your folder:

import subprocess
import os

models_folder = 'C:/folder'
models = sorted(os.listdir(models_folder))

output_folder = 'C:/output_folder'

instant_meshes_path = 'C:/Instant_Meshes.exe'

for file in models:
# Desired number of faces for the model
faces = 2500

subprocess.call([
instant_meshes_path,
'--faces', str(faces),
os.path.join(models_folder, file),
os.path.join(output_folder, file)
])

• I got the following error: File "C:\Users\x\AppData\Local\Programs\Python\Python36-32\lib\subprocess.py", line 267, in call with Popen(*popenargs, **kwargs) as p: File "C:\Users\x\AppData\Local\Programs\Python\Python36-32\lib\subprocess.py", line 709, in init restore_signals, start_new_session) File "C:\Users\x\AppData\Local\Programs\Python\Python36-32\lib\subprocess.py", line 997, in _execute_child startupinfo) FileNotFoundError: [WinError 2] The system cannot find the file specified Also, instant-meshes was unable to load FBX files. – geekyJock May 14 '18 at 5:24
• It's a FileNotFoundError. You have to edit the example script and put in the correct folder paths. Use forward slashes ('/') because back slashes are an escape character in python. – doakey3 May 14 '18 at 20:43