-
Notifications
You must be signed in to change notification settings - Fork 17
/
blender-mesh-to-json.py
360 lines (306 loc) · 15.4 KB
/
blender-mesh-to-json.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
# The goal of this addon is to export all of the actions for the active
# armature into a JSON file
import ast
import bpy
import collections
import json
import os
from mathutils import Vector
bl_info = {
"name": "Export Mesh to JSON",
"category": "Import-Export",
"blender": (2, 80, 0)
}
# Write our JSON to stdout by default or to a file if specified.
# Stdout mesh JSON is wrapped in a start and end indicators
# to more easily distinguish it from other Blender output.
#
# START_MESH_JSON $BLENDER_FILEPATH $MESH_NAME
# ... mesh json ...
# END_MESH_JSON $BLENDER_FILEPATH $MESH_NAME
class MeshToJSON(bpy.types.Operator):
"""Given an active armature, export it's actions and keyframed bone
pose information to a JSON file"""
# Unique identifier for the addon
bl_idname = 'import_export.mesh2json'
# Display name in the interface
bl_label = 'Export Mesh to JSON'
bl_options = {'REGISTER'}
bl_category = 'Import-Export'
# The filepath to write out JSON to
# filepath = bpy.props.StringProperty(name='filepath')
def execute(self, context):
bpy.ops.object.mode_set(mode='OBJECT')
mesh = bpy.context.view_layer.objects.active
mesh_json = {
'name': mesh.name,
'armature_name': None,
# [x, y, z]
'bounding_box': {
'min_corner': [], 'max_corner': []
},
'materials': {},
'custom_properties': {},
'attribs': {
'vertices_in_each_face': [],
'positions': {
'indices': [],
'attribute': {
'data': [],
'attribute_size': 3
}
},
'normals': {
'indices': [],
'attribute': {
'data': [],
'attribute_size': 3
}
},
'uvs': {
'indices': [],
'attribute': {
'data': [],
'attribute_size': 2
}
},
'bone_influences': {
'bones_per_vertex': {
'NonUniform': []
},
'bone_indices': [],
'bone_weights': []
}
}
}
# We maintain a list of all of the parent armature's bone names so that when exporting bone indices / weights
# we use the same order that our armature use.
# i.e. vertex group 12 that we export is the same as the 12th bone in the parent armature.
# Without this the 12th vertex group on the mesh might actually be referring the 8th bone in the armature.
# This would be a problem since our export format is currently based on the order of the bones in the armature.
allBoneNames = []
if mesh.parent is not None and mesh.parent.type == 'ARMATURE':
parentArmature = mesh.parent
mesh_json['armature_name'] = parentArmature.name
for poseBone in parentArmature.pose.bones:
allBoneNames.append(poseBone.name)
# TODO: Handle triangular polygons, not just quads
# cube.data.polygons[1].vertices[0]. Check if length
# of face is 4... Use a triangular face in Blender to unit test.
index = 0
for face in mesh.data.polygons:
num_vertices_in_face = len(face.vertices)
mesh_json['attribs']['vertices_in_each_face'].append(num_vertices_in_face)
for i in range(num_vertices_in_face):
mesh_json['attribs']['positions']['indices'].append(face.vertices[i])
# TODO: Maintain a dictionary with (x, y, z) => normal index
# for normals that we've already run into.
# Re-use an existing normal index wherever possible.
# Especially important for smoothed models that mostly re-use
# the same normals. Test this by making a cube with to faces
# that have the same normal
mesh_json['attribs']['normals']['indices'].append(index)
if mesh.data.uv_layers:
mesh_json['attribs']['uvs']['indices'].append(face.loop_indices[i])
# TODO: Don't append normals if we've already encountered them
mesh_json['attribs']['normals']['attribute']['data'].append(face.normal.x)
mesh_json['attribs']['normals']['attribute']['data'].append(face.normal.y)
mesh_json['attribs']['normals']['attribute']['data'].append(face.normal.z)
index += 1
for vert in mesh.data.vertices:
mesh_json['attribs']['positions']['attribute']['data'].append(vert.co.x)
mesh_json['attribs']['positions']['attribute']['data'].append(vert.co.y)
mesh_json['attribs']['positions']['attribute']['data'].append(vert.co.z)
num_groups = len(list(vert.groups))
for group in vert.groups:
groupName = mesh.vertex_groups[group.group].name
if groupName not in allBoneNames:
continue
boneIndex = allBoneNames.index(groupName)
mesh_json['attribs']['bone_influences']['bone_indices'].append(boneIndex)
mesh_json['attribs']['bone_influences']['bone_weights'].append(group.weight)
if mesh_json['armature_name'] is not None:
mesh_json['attribs']['bone_influences']['bones_per_vertex']['NonUniform'].append(num_groups)
if mesh.data.uv_layers:
for loop in mesh.data.uv_layers.active.data:
mesh_json['attribs']['uvs']['attribute']['data'].append(loop.uv.x)
mesh_json['attribs']['uvs']['attribute']['data'].append(loop.uv.y)
if not mesh_json['armature_name']:
mesh_json['attribs']['bone_influences'] = None
if not mesh_json['attribs']['uvs']['indices']:
mesh_json['attribs']['uvs'] = None
# TODO: Add unit test for no mesh currently selected
# if mesh == None or mesh.type != 'MESH':
# print("__NO_MESH_SELECTED__", file=sys.stderr)
# return {'FINISHED'}
# We construct our bounding box by iterating over all of the corners of the
# mesh and finding the smallest and largest x, y and z values. Remember that we
# are in a z up coordinate system in Blender.
index = 0
min_corner = [float('inf'), float('inf'), float('inf')];
max_corner = [-float('inf'), -float('inf'), -float('inf')];
# By switching to EDIT mode we'll ensure that our mesh is in its bind position.
# Otherwise we might get the bounding box of the mesh while it was in the middle of some keyframe
# which could be different from its bounding box in bind position.
bpy.ops.object.mode_set(mode = 'EDIT')
for corner in mesh.bound_box:
# Get the Blender world space (within Blender) coordinates for the corner of this mesh.
# This gives us the actual (x, y, z) coordinates of the corner in Blender's coordinate space,
# instead of relative to the model's origin.
# Modified from - https://blender.stackexchange.com/a/8470
corner = Vector(corner)
corner = mesh.matrix_world @ corner
# Min Corner
min_corner[0] = min(min_corner[0], corner.x)
min_corner[1] = min(min_corner[1], corner.y)
min_corner[2] = min(min_corner[2], corner.z)
# Max corner
max_corner[0] = max(max_corner[0], corner.x)
max_corner[1] = max(max_corner[1], corner.y)
max_corner[2] = max(max_corner[2], corner.z)
bpy.ops.object.mode_set(mode = 'OBJECT')
mesh_json['bounding_box']['min_corner'] = min_corner
mesh_json['bounding_box']['max_corner'] = max_corner
for material in mesh.data.materials:
if material.node_tree == None:
continue;
# Iterate over the nodes until we find the Principled BSDF node. Then
# read its properties
for node in material.node_tree.nodes:
baseColor = {}
roughness = {}
metallic = {}
normalMap = None
if node.type == 'BSDF_PRINCIPLED':
if len(node.inputs['Base Color'].links) > 0:
link = node.inputs['Base Color'].links[0]
# If there is a node feeding into the base_color, use
# that node's output color or image
if link.from_node.type == 'TEX_IMAGE':
baseColor['ImageTexture'] = link.from_node.image.name
else:
color = link.from_node.outputs['Color'].default_value
baseColor['Uniform'] = [
color[0], color[1], color[2]
]
else:
# Otherwise use the output color set in the principled
# nodes color selector
color = node.inputs['Base Color'].default_value
baseColor['Uniform'] = [
color[0], color[1], color[2]
]
if len(node.inputs['Roughness'].links) > 0:
link = node.inputs['Roughness'].links[0]
# If there is a node feeding into the roughness, use
# that node's output color or image
if link.from_node.type == 'TEX_IMAGE':
# If the channels weren't split, default to red
# channel
roughness['ImageTexture'] = [
link.from_node.image.name,
"R"
]
elif link.from_node.type == 'SEPRGB':
print(mesh.name)
# example: ["some-texture.png", "R"]
roughness['ImageTexture'] = [
link.from_node.inputs['Image'].links[0].from_node.image.name,
link.from_socket.name # R, G or B
]
else:
roughness['Uniform'] = link.from_node.outputs['Value'].default_value
else:
# Otherwise use the output color set in the principled
# nodes color selector
roughness['Uniform'] = node.inputs['Roughness'].default_value
if len(node.inputs['Metallic'].links) > 0:
link = node.inputs['Metallic'].links[0]
# If there is a node feeding into the metallic, use
# that node's output color or image
if link.from_node.type == 'TEX_IMAGE':
metallic['ImageTexture'] = [
# If the channels weren't split, default to
# green channel
link.from_node.image.name,
"G"
]
elif link.from_node.type == 'SEPRGB':
# example: ["some-texture.png", "G"]
metallic['ImageTexture'] = [
link.from_node.inputs['Image'].links[0].from_node.image.name,
link.from_socket.name # R, G or B
]
else:
metallic['Uniform'] = link.from_node.outputs['Value'].default_value
else:
# Otherwise use the output color set in the principled
# nodes color selector
metallic['Uniform'] = node.inputs['Metallic'].default_value
# Work backwards up to the normal map's image texture.
# Principled Node -> Normal Map -> Image Texture
if len(node.inputs['Normal'].links) > 0:
link = node.inputs['Normal'].links[0]
if link.from_node.type == 'NORMAL_MAP':
normalMapNode = link.from_node
normalMap = normalMapNode.inputs['Color'].links[0].from_node.image.name
mesh_json['materials'][material.name] = {
'base_color': baseColor,
'roughness': roughness,
'metallic': metallic,
'normal_map': normalMap
}
for property in mesh.keys():
# Not sure what this is but it gets automatically added into the properties. So we ignore it
if property == '_RNA_UI':
continue
# Some properties such as 'cycles_visibility' are automatically inserted by Blender, but can't be
# serialized.
# Here we test if a property can be serialized, and if it can't we just skip it
try:
value = mesh.get(property)
json.dumps(value)
typed_value = {}
try:
maybe_list = json.loads(value)
if isinstance(maybe_list, list):
typed_value = {"Vec": []}
for item in maybe_list:
if isinstance(item, float):
typed_value["Vec"].append({"Float": item})
elif isinstance(item, int):
typed_value["Vec"].append({"Int": item})
elif isinstance(item, str):
typed_value["Vec"].append({"String": item})
mesh_json['custom_properties'][property] = typed_value
continue
except:
if isinstance(value, float):
typed_value = {"Float": value}
elif isinstance(value, int):
typed_value = {"Int": value}
elif isinstance(value, str):
typed_value = {"String": value}
mesh_json['custom_properties'][property] = typed_value
except:
pass
# START_MESH_JSON $BLENDER_FILEPATH $MESH_NAME
# ... mesh json ...
# END_MESH_JSON $BLENDER_FILEPATH $MESH_NAME
#
# NOTE: Intentionally done in one print statement to get around
# a bug where other Blender output (in this case from bpy.ops.anim.keyframe_delete(override, type='LocRotScale')
# calls in blender-iks-to-fks) was getting mixed in with our JSON output
output = "START_MESH_JSON " + bpy.data.filepath + " " + mesh.name
output += "\n"
output += json.dumps(mesh_json)
output += "\n"
output += "END_MESH_JSON " + bpy.data.filepath + " " + mesh.name
print(output)
return {'FINISHED'}
def register():
bpy.utils.register_class(MeshToJSON)
def unregister():
bpy.utils.unregister_class(MeshToJSON)
if __name__ == "__main__":
register()