-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblends2gltfs.py
89 lines (77 loc) · 2.7 KB
/
blends2gltfs.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#===================================#
# BLENDS2GLTFS #
# #
# a .blend to .gltf mass exporter #
# #
# Author: Belery Olivier #
# Version: 1.0 #
# License: MIT #
#===================================#
#
# Description:
# ------------
# Exports all blend files (even nested in subdirectories) to GLTF 2.
# Exported files are saved in a specified location.
# This py scipt uses Blender's embeded python interpreter. No other Python required.
# Use case: Export multiple blender files content to single Unity/Godot/A-frame assets folder.
#
# USAGE:
#
# WINDOWS: "C:/program files (x86)/blender foundation/blender/blender.exe" --background --python blends2gltfs.py -- PATH_WHERE TO EXPORT YOUR_GLTF
# UNIX : blender --background --python blends2gltfs.py -- PATH_WHERE TO EXPORT YOUR_GLTF
import os
import bpy
import sys
import time
import glob
from bpy.app.handlers import persistent
########## persistent operator ##########
@persistent
def load_handler(dummy):
#getting output dir from system vars
argv = sys.argv
argv = argv[argv.index("--") + 1:]
outputdir = argv[0]
#Generate output filename and path bpy.data.filepath
outputfile = bpy.path.display_name_from_filepath(files[0]) + ".glb"
outputpath = outputdir + "\\" + outputfile
print("EXPORTING " + outputfile + " ..... "),
#exporting file
bpy.ops.export_scene.gltf(
export_format='GLB',
export_yup=True,
export_animations=False,
export_image_format ='PNG',
export_apply=True,
export_normals=True,
export_tangents=False,
export_colors =True,
export_materials=True,
filepath=outputpath)
print("done")
processed.append(files.pop(0))
if(len(files) > 0):
time.sleep(1)
bpy.ops.wm.open_mainfile(filepath=files[0])
else:
print("OPERATION FINISHED - processed files:" + str(len(processed)))
print("OPERATION OUTPUT PATH IS: " + outputdir)
blockPrint()
bpy.ops.wm.quit_blender()
########## persistent operator end ##########
#declaring persitent handler to use when opening file
bpy.app.handlers.load_post.append(load_handler)
# Disable printout
def blockPrint():
sys.stdout = open(os.devnull, 'w')
# Restore printout
def enablePrint():
sys.stdout = sys.__stdout__
# Getting .blend files list of current folder and subfolder (1 level deep)
files = []
processed = []
for f in glob.iglob("./*/*.blend"):
files.append(f)
# Opens up the first file on initial script execution.
# Folowing files loading will be handled by the persistent operator (load_handler)
bpy.ops.wm.open_mainfile(filepath=files[0])