Created
March 16, 2018 04:42
-
-
Save neon-ninja/9e535ec20e08979c5f8860d5804bdfae to your computer and use it in GitHub Desktop.
Blender script to load in an FBX then decimate all meshes it contains
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import bpy | |
| import sys | |
| import time | |
| argv = sys.argv | |
| argv = argv[argv.index("--") + 1:] # get all args after "--" | |
| if len(argv) < 2: | |
| print("Give arguments -- fbx_file decimate_ratio") | |
| startTime = time.time() | |
| def log(msg): | |
| s = round(time.time() - startTime, 2) | |
| print("{}s {}".format(s, msg)) | |
| fbxFile = argv[0] | |
| decimateRatio = float(argv[1]) | |
| # Clear Blender scene | |
| for obj in bpy.data.objects: | |
| bpy.data.objects.remove(obj, do_unlink=True) | |
| for mesh in bpy.data.meshes: | |
| bpy.data.meshes.remove(mesh) | |
| s = time.time() | |
| bpy.ops.import_scene.fbx(filepath=fbxFile) | |
| log("Loaded") | |
| modifierName='DecimateMod' | |
| objectList=bpy.data.objects | |
| meshes = [] | |
| for obj in objectList: | |
| if(obj.type == "MESH"): | |
| meshes.append(obj) | |
| log("{} meshes".format(len(meshes))) | |
| for i, obj in enumerate(meshes): | |
| bpy.context.scene.objects.active = obj | |
| log("{}/{} meshes, name: {}".format(i, len(meshes), obj.name)) | |
| log("{} has {} verts, {} edges, {} polys".format(obj.name, len(obj.data.vertices), len(obj.data.edges), len(obj.data.polygons))) | |
| modifier = obj.modifiers.new(modifierName,'DECIMATE') | |
| modifier.ratio = decimateRatio | |
| modifier.use_collapse_triangulate = True | |
| bpy.ops.object.modifier_apply(apply_as='DATA', modifier=modifierName) | |
| log("{} has {} verts, {} edges, {} polys after decimation".format(obj.name, len(obj.data.vertices), len(obj.data.edges), len(obj.data.polygons))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sankyu very much