forked from felipe-fg/batch-blender-godot-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exporter_blender_godot.py
75 lines (56 loc) · 2.12 KB
/
exporter_blender_godot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
"""
Batch tool for importing .obj models and exporting .dae models.
This script requires Godot's Collada Exporter.
blender --background --python exporter_blender_godot.py -- input_path output_path
blender --background --python exporter_blender_godot.py -- "../Assets/Nature Kit OBJ/" "../Assets/Nature Kit DAE/"
"""
import sys
import os
import bpy
def run(input_path, output_path):
"""
Runs the batch tool.
:param input_path: Input folder with .obj files.
:param output_path: Output folder for .dae files.
"""
all_files = sorted(os.listdir(input_path))
obj_files = [f for f in all_files if f.endswith(".obj")]
for filename in obj_files:
input_filepath = os.path.join(input_path, filename)
output_filepath = os.path.join(output_path, filename.replace(".obj", ".dae"))
os.makedirs(output_path, exist_ok=True)
bpy.ops.wm.read_homefile()
bpy.ops.object.select_all(action="SELECT")
bpy.ops.object.delete()
process_file(input_filepath, output_filepath)
def process_file(input_filepath, output_filepath):
"""
Imports the .obj file and exports as .dae file.
:param input_filepath: Complete input filepath for .obj model.
:param output_filepath: Complete output filepath for .dae model.
"""
# import and select
bpy.ops.import_scene.obj(
filepath=input_filepath, use_edges=True, use_smooth_groups=False
)
current = bpy.context.selected_objects[0]
bpy.context.scene.objects.active = current
# clean shading
bpy.ops.object.mode_set(mode="OBJECT")
current.data.use_auto_smooth = 0
bpy.ops.object.shade_smooth()
bpy.ops.object.modifier_add(type="EDGE_SPLIT")
# clean mesh
bpy.ops.object.mode_set(mode="EDIT")
bpy.ops.mesh.dissolve_limited()
bpy.ops.mesh.remove_doubles()
bpy.ops.mesh.normals_make_consistent(inside=False)
# export model
bpy.ops.object.mode_set(mode="OBJECT")
bpy.ops.export_scene.dae(
filepath=output_filepath, use_mesh_modifiers=True, use_triangles=True
)
# run the batch tool
argv = sys.argv
argv = argv[argv.index("--") + 1 :]
run(argv[0], argv[1])