-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexport.py
116 lines (93 loc) · 3.74 KB
/
export.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import os
import json
import ntpath
import bpy
from progress_report import ProgressReport, ProgressReportSubstep
def path_leaf(path):
head, tail = ntpath.split(path)
return tail or ntpath.basename(head)
# Remove spaces from given string (so it would be spaceless)
def name_compat(name):
return 'None' if name is None else name.replace(' ', '_')
def save(context, filepath, use_selection=True, provides_mtl=False):
with ProgressReport(context.window_manager) as progress:
scene = context.scene
# Exit edit mode before exporting, so current object states are exported properly.
if bpy.ops.object.mode_set.poll():
bpy.ops.object.mode_set(mode='OBJECT')
objects = context.selected_objects if use_selection else scene.objects
progress.enter_substeps(1)
write_file(context, filepath, objects, scene, provides_mtl, progress)
progress.leave_substeps()
return {'FINISHED'}
def write_file(context, filepath, objects, scene, provides_mtl, progress=ProgressReport()):
# bpy.ops.object.select_all(action='DESELECT')
with ProgressReportSubstep(progress, 2, "JSON export path: %r" % filepath, "JSON export finished") as subprogress1:
with open(filepath, "w", encoding="utf8", newline="\n") as f:
limbs = {
'anchor': {
'opacity': 0
}
}
pose = {}
for obj in objects:
if obj.type != "MESH":
continue
name1 = obj.name
name2 = obj.data.name
if name1 == name2:
name = name_compat(name1)
else:
name = '%s_%s' % (name_compat(name1), name_compat(name2))
cursor = obj.matrix_world.translation
x = cursor[0]
y = cursor[2]
z = cursor[1]
limb = {
'origin': [x, y, -z],
'smooth': True,
'parent': 'anchor'
}
transform = {
'translate': [x * 16, y * 16, z * 16]
}
# Some automatic setup of limbs based on name
if name1 == 'head':
limb['looking'] = True
elif name1 == 'left_arm':
limb['holding'] = 'left'
limb['swinging'] = True
limb['idle'] = True
limb['invert'] = True
elif name1 == 'right_arm':
limb['holding'] = 'right'
limb['swinging'] = True
limb['idle'] = True
limb['swiping'] = True
elif name1 == 'right_leg':
limb['swinging'] = True
limb['invert'] = True
elif name1 == 'left_leg':
limb['swinging'] = True
limbs[name] = limb
pose[name] = transform
# Write JSON to the file
pose = {
'size': [0.6, 1.8, 0.6],
'limbs': pose
}
data = {
'scheme': "1.3",
'providesObj': True,
'providesMtl': provides_mtl,
'name': path_leaf(filepath),
'limbs': limbs,
'poses': {
'standing': pose,
'sneaking': pose,
'sleeping': pose,
'flying': pose
}
}
f.write(json.dumps(data, indent=4))
subprogress1.step("Finished exporting JSON")