Skip to content

Instantly share code, notes, and snippets.

@riicchhaarrd
Last active November 5, 2023 05:26
Show Gist options
  • Save riicchhaarrd/c38c940a49101e3bdd4a92af265ba610 to your computer and use it in GitHub Desktop.
Save riicchhaarrd/c38c940a49101e3bdd4a92af265ba610 to your computer and use it in GitHub Desktop.
blender batch decimate
# blender.exe -b -P decimate.py -- --input_file crate.glb --output_file output.glb
import bpy
import sys
import math
args = sys.argv
input_file = None
output_file = None
i = 0
while i < len(args):
if args[i] == '--input_file' and i + 1 < len(args):
input_file = args[i + 1]
i += 2
elif args[i] == '--output_file' and i + 1 < len(args):
output_file = args[i + 1]
i += 2
else:
i += 1
if not output_file:
sys.exit("No output file specified. Please provide an output file path.")
print(f'Input file: {input_file}')
print(f'Output file: {output_file}')
# input()
# bpy.ops.wm.open_mainfile(filepath=input_file)
bpy.ops.import_scene.gltf(filepath=input_file)
bpy.ops.object.select_all(action='DESELECT')
for obj in bpy.context.scene.objects:
if obj.type == 'MESH':
obj.select_set(True)
bpy.context.view_layer.objects.active = obj
bpy.ops.object.modifier_add(type='DECIMATE')
modifier = obj.modifiers["Decimate"]
modifier.decimate_type = 'DISSOLVE'
angle_deg = 30
modifier.angle_limit = (angle_deg / 180) * math.pi
# modifier.use_collapse_triangulate = True
bpy.ops.object.modifier_apply(modifier="Decimate")
bpy.ops.export_scene.gltf(filepath=output_file, export_format="GLB")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment