forked from sugiany/blender_mmd_tools
-
Notifications
You must be signed in to change notification settings - Fork 280
/
exporter.py
1169 lines (1045 loc) · 49.4 KB
/
exporter.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
import os
import copy
import logging
import shutil
import time
import mathutils
import bpy
import bmesh
from collections import OrderedDict
from mmd_tools.core import pmx
from mmd_tools.core.bone import FnBone
from mmd_tools.core.material import FnMaterial
from mmd_tools.core.vmd.importer import BoneConverter
from mmd_tools import bpyutils
from mmd_tools.utils import saferelpath
class _Vertex:
def __init__(self, co, groups, offsets, old_index, edge_scale, vertex_order):
self.co = co
self.groups = groups # [(group_number, weight), ...]
self.offsets = offsets
self.old_index = old_index # used for exporting uv morphs
self.edge_scale = edge_scale
self.vertex_order = vertex_order # used for controlling vertex order
self.index = None
self.uv = None
self.normal = None
self.sdef_data = None # (C, R0, R1)
class _Face:
def __init__(self, vertices):
''' Temporary Face Class
'''
self.vertices = vertices
class _Mesh:
def __init__(self, mesh_data, material_faces, shape_key_names, vertex_group_names, materials):
self.mesh_data = mesh_data
self.material_faces = material_faces # dict of {material_index => [face1, face2, ....]}
self.shape_key_names = shape_key_names
self.vertex_group_names = vertex_group_names
self.materials = materials
def __del__(self):
logging.debug('remove mesh data: %s', str(self.mesh_data))
bpy.data.meshes.remove(self.mesh_data)
class _DefaultMaterial:
def __init__(self):
mat = bpy.data.materials.new('')
#mat.mmd_material.diffuse_color = (0, 0, 0)
#mat.mmd_material.specular_color = (0, 0, 0)
#mat.mmd_material.ambient_color = (0, 0, 0)
self.material = mat
logging.debug('create default material: %s', str(self.material))
def __del__(self):
if self.material:
logging.debug('remove default material: %s', str(self.material))
bpy.data.materials.remove(self.material)
class __PmxExporter:
TO_PMX_MATRIX = mathutils.Matrix([
[1.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 1.0, 0.0],
[0.0, 1.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 1.0]])
CATEGORIES = {
'SYSTEM': pmx.Morph.CATEGORY_SYSTEM,
'EYEBROW': pmx.Morph.CATEGORY_EYEBROW,
'EYE': pmx.Morph.CATEGORY_EYE,
'MOUTH': pmx.Morph.CATEGORY_MOUTH,
}
def __init__(self):
self.__model = None
self.__bone_name_table = []
self.__material_name_table = []
self.__vertex_index_map = {} # used for exporting uv morphs
self.__default_material = None
self.__vertex_order_map = None # used for controlling vertex order
self.__disable_specular = False
@staticmethod
def flipUV_V(uv):
u, v = uv
return [u, 1.0-v]
def __getDefaultMaterial(self):
if self.__default_material is None:
self.__default_material = _DefaultMaterial()
return self.__default_material.material
def __sortVertices(self):
logging.info(' - Sorting vertices ...')
weight_items = self.__vertex_order_map.items()
sorted_indices = [i[0] for i in sorted(weight_items, key=lambda x: x[1].vertex_order)]
vertices = self.__model.vertices
self.__model.vertices = [vertices[i] for i in sorted_indices]
# update indices
index_map = {x:i for i, x in enumerate(sorted_indices)}
for v in self.__vertex_order_map.values(): # for vertex morphs
v.index = index_map[v.index]
for v in self.__vertex_index_map.values(): # for uv morphs
v[:] = [index_map[i] for i in v]
for f in self.__model.faces:
f[:] = [index_map[i] for i in f]
logging.debug(' - Done (count:%d)', len(self.__vertex_order_map))
def __exportMeshes(self, meshes, bone_map):
mat_map = OrderedDict()
for mesh in meshes:
for index, mat_faces in sorted(mesh.material_faces.items(), key=lambda x: x[0]):
name = mesh.materials[index].name
if name not in mat_map:
mat_map[name] = []
mat_map[name].append((mat_faces, mesh.vertex_group_names))
sort_vertices = self.__vertex_order_map is not None
if sort_vertices:
self.__vertex_order_map.clear()
# export vertices
for mat_name, mat_meshes in mat_map.items():
face_count = 0
for mat_faces, vertex_group_names in mat_meshes:
mesh_vertices = []
for face in mat_faces:
mesh_vertices.extend(face.vertices)
for v in mesh_vertices:
if v.index is not None:
continue
v.index = len(self.__model.vertices)
if v.old_index is not None:
self.__vertex_index_map[v.old_index].append(v.index)
if sort_vertices:
self.__vertex_order_map[v.index] = v
pv = pmx.Vertex()
pv.co = list(v.co)
pv.normal = v.normal
pv.uv = self.flipUV_V(v.uv)
pv.edge_scale = v.edge_scale
t = len(v.groups)
if t == 0:
weight = pmx.BoneWeight()
weight.type = pmx.BoneWeight.BDEF1
weight.bones = [-1]
pv.weight = weight
elif t == 1:
weight = pmx.BoneWeight()
weight.type = pmx.BoneWeight.BDEF1
weight.bones = [bone_map[vertex_group_names[v.groups[0][0]]]]
pv.weight = weight
elif t == 2:
vg1, vg2 = v.groups
weight = pmx.BoneWeight()
weight.type = pmx.BoneWeight.BDEF2
weight.bones = [
bone_map[vertex_group_names[vg1[0]]],
bone_map[vertex_group_names[vg2[0]]]
]
w1, w2 = vg1[1], vg2[1]
weight.weights = [w1/(w1+w2)]
if v.sdef_data:
weight.type = pmx.BoneWeight.SDEF
sdef_weights = pmx.BoneWeightSDEF()
sdef_weights.weight = weight.weights[0]
sdef_weights.c = v.sdef_data[0]
sdef_weights.r0 = v.sdef_data[1]
sdef_weights.r1 = v.sdef_data[2]
weight.weights = sdef_weights
pv.weight = weight
else:
weight = pmx.BoneWeight()
weight.type = pmx.BoneWeight.BDEF4
weight.bones = [-1, -1, -1, -1]
weight.weights = [0.0, 0.0, 0.0, 0.0]
w_all = 0.0
for i in range(min(len(v.groups), 4)):
gn, w = v.groups[i]
weight.bones[i] = bone_map[vertex_group_names[gn]]
weight.weights[i] = w
w_all += w
for i in range(4):
weight.weights[i] /= w_all
pv.weight = weight
self.__model.vertices.append(pv)
for face in mat_faces:
self.__model.faces.append([x.index for x in face.vertices])
face_count += len(mat_faces)
self.__exportMaterial(bpy.data.materials[mat_name], face_count)
if sort_vertices:
self.__sortVertices()
def __exportTexture(self, filepath):
if filepath.strip() == '':
return -1
# Use bpy.path to resolve '//' in .blend relative filepaths
filepath = bpy.path.abspath(filepath)
filepath = os.path.abspath(filepath)
for i, tex in enumerate(self.__model.textures):
if tex.path == filepath:
return i
t = pmx.Texture()
t.path = filepath
self.__model.textures.append(t)
if not os.path.isfile(t.path):
logging.warning(' The texture file does not exist: %s', t.path)
return len(self.__model.textures) - 1
def __copy_textures(self, output_dir, base_folder=''):
tex_dir = output_dir
tex_dir_fallback = os.path.join(tex_dir, 'textures')
tex_dir_preference = bpyutils.addon_preferences('base_texture_folder', '')
for texture in self.__model.textures:
path = texture.path
tex_dir = output_dir # restart to the default directory at each loop
if not os.path.isfile(path):
logging.warning('*** skipping texture file which does not exist: %s', path)
continue
dst_name = os.path.basename(path)
if base_folder != '':
dst_name = saferelpath(path, base_folder, strategy='outside')
if dst_name.startswith('..'):
# Check if the texture comes from the preferred folder
if tex_dir_preference:
dst_name = saferelpath(path, tex_dir_preference, strategy='outside')
if dst_name.startswith('..'):
# If the code reaches here the texture is somewhere else
logging.warning('The texture %s is not inside the base texture folder', path)
# Fall back to basename and textures folder
dst_name = os.path.basename(path)
tex_dir = tex_dir_fallback
else:
tex_dir = tex_dir_fallback
dest_path = os.path.join(tex_dir, dst_name)
os.makedirs(os.path.dirname(dest_path), exist_ok=True)
if path != dest_path: # Only copy if the paths are different
shutil.copyfile(path, dest_path)
logging.info('Copy file %s --> %s', path, dest_path)
texture.path = dest_path
def __exportMaterial(self, material, num_faces):
p_mat = pmx.Material()
mmd_mat = material.mmd_material
p_mat.name = mmd_mat.name_j or material.name
p_mat.name_e = mmd_mat.name_e or material.name
p_mat.diffuse = list(mmd_mat.diffuse_color) + [mmd_mat.alpha]
p_mat.ambient = mmd_mat.ambient_color
p_mat.specular = mmd_mat.specular_color
p_mat.shininess = mmd_mat.shininess
p_mat.is_double_sided = mmd_mat.is_double_sided
p_mat.enabled_drop_shadow = mmd_mat.enabled_drop_shadow
p_mat.enabled_self_shadow_map = mmd_mat.enabled_self_shadow_map
p_mat.enabled_self_shadow = mmd_mat.enabled_self_shadow
p_mat.enabled_toon_edge = mmd_mat.enabled_toon_edge
p_mat.edge_color = mmd_mat.edge_color
p_mat.edge_size = mmd_mat.edge_weight
p_mat.sphere_texture_mode = int(mmd_mat.sphere_texture_type)
if self.__disable_specular:
p_mat.sphere_texture_mode = pmx.Material.SPHERE_MODE_OFF
p_mat.comment = mmd_mat.comment
p_mat.vertex_count = num_faces * 3
fnMat = FnMaterial(material)
tex = fnMat.get_texture()
if tex and tex.type == 'IMAGE' and tex.image: # Ensure the texture is an image
index = self.__exportTexture(tex.image.filepath)
p_mat.texture = index
tex = fnMat.get_sphere_texture()
if tex and tex.type == 'IMAGE' and tex.image: # Ensure the texture is an image
index = self.__exportTexture(tex.image.filepath)
p_mat.sphere_texture = index
if mmd_mat.is_shared_toon_texture:
p_mat.toon_texture = mmd_mat.shared_toon_texture
p_mat.is_shared_toon_texture = True
else:
p_mat.toon_texture = self.__exportTexture(mmd_mat.toon_texture)
p_mat.is_shared_toon_texture = False
self.__material_name_table.append(material.name)
self.__model.materials.append(p_mat)
@classmethod
def __countBoneDepth(cls, bone):
if bone.parent is None:
return 0
else:
return cls.__countBoneDepth(bone.parent) + 1
def __exportBones(self, meshes):
""" Export bones.
Returns:
A dictionary to map Blender bone names to bone indices of the pmx.model instance.
"""
arm = self.__armature
boneMap = {}
pmx_bones = []
pose_bones = arm.pose.bones
world_mat = arm.matrix_world
r = {}
# determine the bone order
vtx_grps = {}
for mesh in meshes:
if mesh.modifiers.get('mmd_bone_order_override', None):
vtx_grps = mesh.vertex_groups
break
class _Dummy:
index = float('inf')
sorted_bones = sorted(pose_bones, key=lambda x: vtx_grps.get(x.name, _Dummy).index)
#sorted_bones = sorted(pose_bones, key=self.__countBoneDepth)
pmx_matrix = self.TO_PMX_MATRIX * world_mat * self.__scale
def __to_pmx_location(loc):
return pmx_matrix * mathutils.Vector(loc)
if True: # no need to enter edit mode
for p_bone in sorted_bones:
if p_bone.is_mmd_shadow_bone:
continue
bone = p_bone.bone
mmd_bone = p_bone.mmd_bone
pmx_bone = pmx.Bone()
pmx_bone.name = mmd_bone.name_j or bone.name
pmx_bone.name_e = mmd_bone.name_e or bone.name
pmx_bone.hasAdditionalRotate = mmd_bone.has_additional_rotation
pmx_bone.hasAdditionalLocation = mmd_bone.has_additional_location
pmx_bone.additionalTransform = [None, mmd_bone.additional_transform_influence]
if mmd_bone.additional_transform_bone_id != -1:
fnBone = FnBone.from_bone_id(arm, mmd_bone.additional_transform_bone_id)
if fnBone:
pmx_bone.additionalTransform[0] = fnBone.pose_bone
pmx_bone.location = __to_pmx_location(p_bone.head)
pmx_bone.parent = bone.parent
pmx_bone.visible = mmd_bone.is_visible
pmx_bone.isControllable = mmd_bone.is_controllable
pmx_bone.isMovable = not all(p_bone.lock_location)
pmx_bone.isRotatable = not all(p_bone.lock_rotation)
pmx_bone.transform_order = mmd_bone.transform_order
pmx_bone.transAfterPhis = mmd_bone.transform_after_dynamics
pmx_bones.append(pmx_bone)
self.__bone_name_table.append(p_bone.name)
boneMap[bone] = pmx_bone
r[bone.name] = len(pmx_bones) - 1
if bone.use_connect and p_bone.parent.mmd_bone.is_tip:
logging.debug(' * fix location of bone %s, parent %s is tip', bone.name, bone.parent.name)
pmx_bone.location = boneMap[bone.parent].location
# a connected child bone is preferred
pmx_bone.displayConnection = None
for child in bone.children:
if child.use_connect:
pmx_bone.displayConnection = child
break
if not pmx_bone.displayConnection:
if mmd_bone.is_tip:
pmx_bone.displayConnection = -1
else:
tail_loc = __to_pmx_location(p_bone.tail)
pmx_bone.displayConnection = tail_loc - pmx_bone.location
#add fixed and local axes
if mmd_bone.enabled_fixed_axis:
pmx_bone.axis = mmd_bone.fixed_axis
if mmd_bone.enabled_local_axes:
pmx_bone.localCoordinate = pmx.Coordinate(
mmd_bone.local_axis_x, mmd_bone.local_axis_z)
for idx, i in enumerate(pmx_bones):
if i.parent is not None:
i.parent = pmx_bones.index(boneMap[i.parent])
logging.debug('the parent of %s:%s: %s', idx, i.name, i.parent)
if isinstance(i.displayConnection, pmx.Bone):
i.displayConnection = pmx_bones.index(i.displayConnection)
elif isinstance(i.displayConnection, bpy.types.Bone):
i.displayConnection = pmx_bones.index(boneMap[i.displayConnection])
pose_bone = i.additionalTransform[0]
i.additionalTransform[0] = r.get(pose_bone.name, -1) if pose_bone else -1
if len(pmx_bones) == 0:
# avoid crashing MMD
pmx_bone = pmx.Bone()
pmx_bone.name = u'全ての親'
pmx_bone.name_e = 'Root'
pmx_bone.location = __to_pmx_location([0,0,0])
tail_loc = __to_pmx_location([0,0,1])
pmx_bone.displayConnection = tail_loc - pmx_bone.location
pmx_bones.append(pmx_bone)
self.__model.bones = pmx_bones
return r
def __exportIKLinks(self, pose_bone, pmx_bones, bone_map, ik_links, count):
if count <= 0:
return ik_links
logging.debug(' Create IK Link for %s', pose_bone.name)
ik_link = pmx.IKLink()
ik_link.target = bone_map[pose_bone.name]
if pose_bone.use_ik_limit_x or pose_bone.use_ik_limit_y or pose_bone.use_ik_limit_z:
minimum = []
maximum = []
if pose_bone.use_ik_limit_x:
minimum.append(pose_bone.ik_min_x)
maximum.append(pose_bone.ik_max_x)
else:
minimum.append(0.0)
maximum.append(0.0)
if pose_bone.use_ik_limit_y:
minimum.append(pose_bone.ik_min_y)
maximum.append(pose_bone.ik_max_y)
else:
minimum.append(0.0)
maximum.append(0.0)
if pose_bone.use_ik_limit_z:
minimum.append(pose_bone.ik_min_z)
maximum.append(pose_bone.ik_max_z)
else:
minimum.append(0.0)
maximum.append(0.0)
ik_limit_override = pose_bone.constraints.get('mmd_ik_limit_override', None)
if ik_limit_override:
if ik_limit_override.use_limit_x:
minimum[0] = ik_limit_override.min_x
maximum[0] = ik_limit_override.max_x
if ik_limit_override.use_limit_y:
minimum[1] = ik_limit_override.min_y
maximum[1] = ik_limit_override.max_y
if ik_limit_override.use_limit_z:
minimum[2] = ik_limit_override.min_z
maximum[2] = ik_limit_override.max_z
convertIKLimitAngles = pmx.importer.PMXImporter.convertIKLimitAngles
minimum, maximum = convertIKLimitAngles(minimum, maximum, pose_bone, invert=True)
ik_link.minimumAngle = list(minimum)
ik_link.maximumAngle = list(maximum)
if pose_bone.parent is not None:
return self.__exportIKLinks(pose_bone.parent, pmx_bones, bone_map, ik_links + [ik_link], count - 1)
else:
return ik_links + [ik_link]
def __exportIK(self, bone_map):
""" Export IK constraints
@param bone_map the dictionary to map Blender bone names to bone indices of the pmx.model instance.
"""
pmx_bones = self.__model.bones
arm = self.__armature
ik_loop_factor = max(arm.get('mmd_ik_loop_factor', 1), 1)
pose_bones = arm.pose.bones
for bone in pose_bones:
if bone.is_mmd_shadow_bone:
continue
for c in bone.constraints:
if c.type == 'IK' and not c.mute:
logging.debug(' Found IK constraint.')
ik_pose_bone = pose_bones[c.subtarget]
if ik_pose_bone.mmd_shadow_bone_type == 'IK_TARGET':
ik_bone_index = bone_map[ik_pose_bone.parent.name]
logging.debug(' Found IK proxy bone: %s -> %s', ik_pose_bone.name, ik_pose_bone.parent.name)
else:
ik_bone_index = bone_map[c.subtarget]
ik_target_bone = self.__get_ik_target_bone(bone)
pmx_ik_bone = pmx_bones[ik_bone_index]
if ik_target_bone is None:
logging.warning(' - IK bone: %s, IK Target not found !!!', pmx_ik_bone.name)
continue
logging.debug(' - IK bone: %s, IK Target: %s', pmx_ik_bone.name, ik_target_bone.name)
pmx_ik_bone.isIK = True
pmx_ik_bone.loopCount = max(int(c.iterations/ik_loop_factor), 1)
pmx_ik_bone.rotationConstraint = bone.mmd_bone.ik_rotation_constraint
pmx_ik_bone.target = bone_map[ik_target_bone.name]
pmx_ik_bone.ik_links = self.__exportIKLinks(bone, pmx_bones, bone_map, [], c.chain_count)
def __get_ik_target_bone(self, target_bone):
""" Get mmd ik target bone.
Args:
target_bone: A blender PoseBone
Returns:
A bpy.types.PoseBone object which is the closest bone from the tail position of target_bone.
Return None if target_bone has no child bones.
"""
valid_children = [c for c in target_bone.children if not c.is_mmd_shadow_bone]
# search 'mmd_ik_target_override' first
for c in valid_children:
ik_target_override = c.constraints.get('mmd_ik_target_override', None)
if ik_target_override and ik_target_override.subtarget == target_bone.name:
logging.debug(' (use "mmd_ik_target_override")')
return c
r = None
min_length = None
for c in valid_children:
if c.bone.use_connect:
return c
length = (c.head - target_bone.tail).length
if min_length is None or length < min_length:
min_length = length
r = c
return r
def __exportVertexMorphs(self, meshes, root):
shape_key_names = []
for mesh in meshes:
for i in mesh.shape_key_names:
if i not in shape_key_names:
shape_key_names.append(i)
morph_categories = {}
morph_english_names = {}
if root:
categories = self.CATEGORIES
for vtx_morph in root.mmd_root.vertex_morphs:
morph_english_names[vtx_morph.name] = vtx_morph.name_e
morph_categories[vtx_morph.name] = categories.get(vtx_morph.category, pmx.Morph.CATEGORY_OHTER)
shape_key_names.sort(key=lambda x: root.mmd_root.vertex_morphs.find(x))
for i in shape_key_names:
morph = pmx.VertexMorph(i, '', 4)
morph.name_e = morph_english_names.get(i, '')
morph.category = morph_categories.get(i, pmx.Morph.CATEGORY_OHTER)
self.__model.morphs.append(morph)
append_table = dict(zip(shape_key_names, [m.offsets.append for m in self.__model.morphs]))
exported_vert = set()
for mesh in meshes:
for mf in mesh.material_faces.values():
for f in mf:
for v in f.vertices:
if v.index in exported_vert:
continue
exported_vert.add(v.index)
for i, offset in v.offsets.items():
mo = pmx.VertexMorphOffset()
mo.index = v.index
mo.offset = offset
append_table[i](mo)
def __export_material_morphs(self, root):
mmd_root = root.mmd_root
categories = self.CATEGORIES
for morph in mmd_root.material_morphs:
mat_morph = pmx.MaterialMorph(
name=morph.name,
name_e=morph.name_e,
category=categories.get(morph.category, pmx.Morph.CATEGORY_OHTER)
)
for data in morph.data:
morph_data = pmx.MaterialMorphOffset()
try:
if data.material != '':
morph_data.index = self.__material_name_table.index(data.material)
else:
morph_data.index = -1
except ValueError:
logging.warning('Material Morph (%s): Material %s was not found.', morph.name, data.material)
continue
morph_data.offset_type = ['MULT', 'ADD'].index(data.offset_type)
morph_data.diffuse_offset = data.diffuse_color
morph_data.specular_offset = data.specular_color
morph_data.shininess_offset = data.shininess
morph_data.ambient_offset = data.ambient_color
morph_data.edge_color_offset = data.edge_color
morph_data.edge_size_offset = data.edge_weight
morph_data.texture_factor = data.texture_factor
morph_data.sphere_texture_factor = data.sphere_texture_factor
morph_data.toon_texture_factor = data.toon_texture_factor
mat_morph.offsets.append(morph_data)
self.__model.morphs.append(mat_morph)
def __sortMaterials(self):
""" sort materials for alpha blending
モデル内全頂点の平均座標をモデルの中心と考えて、
モデル中心座標とマテリアルがアサインされている全ての面の構成頂点との平均距離を算出。
この値が小さい順にソートしてみる。
モデル中心座標から離れている位置で使用されているマテリアルほどリストの後ろ側にくるように。
かなりいいかげんな実装
"""
center = mathutils.Vector([0, 0, 0])
vertices = self.__model.vertices
vert_num = len(vertices)
for v in self.__model.vertices:
center += mathutils.Vector(v.co) / vert_num
faces = self.__model.faces
offset = 0
distances = []
for mat, bl_mat_name in zip(self.__model.materials, self.__material_name_table):
d = 0
face_num = int(mat.vertex_count / 3)
for i in range(offset, offset + face_num):
face = faces[i]
d += (mathutils.Vector(vertices[face[0]].co) - center).length
d += (mathutils.Vector(vertices[face[1]].co) - center).length
d += (mathutils.Vector(vertices[face[2]].co) - center).length
distances.append((d/mat.vertex_count, mat, offset, face_num, bl_mat_name))
offset += face_num
sorted_faces = []
sorted_mat = []
self.__material_name_table.clear()
for d, mat, offset, vert_count, bl_mat_name in sorted(distances, key=lambda x: x[0]):
sorted_faces.extend(faces[offset:offset+vert_count])
sorted_mat.append(mat)
self.__material_name_table.append(bl_mat_name)
self.__model.materials = sorted_mat
self.__model.faces = sorted_faces
def __export_bone_morphs(self, root):
mmd_root = root.mmd_root
if len(mmd_root.bone_morphs) == 0:
return
categories = self.CATEGORIES
pose_bones = self.__armature.pose.bones
for morph in mmd_root.bone_morphs:
bone_morph = pmx.BoneMorph(
name=morph.name,
name_e=morph.name_e,
category=categories.get(morph.category, pmx.Morph.CATEGORY_OHTER)
)
for data in morph.data:
morph_data = pmx.BoneMorphOffset()
try:
morph_data.index = self.__bone_name_table.index(data.bone)
except ValueError:
morph_data.index = -1
blender_bone = pose_bones.get(data.bone, None)
if blender_bone is None:
logging.warning('Bone Morph (%s): Bone %s was not found.', morph.name, data.bone)
continue
converter = BoneConverter(blender_bone, self.__scale, invert=True)
morph_data.location_offset = converter.convert_location(data.location)
rw, rx, ry, rz = data.rotation
rw, rx, ry, rz = converter.convert_rotation([rx, ry, rz, rw])
morph_data.rotation_offset = (rx, ry, rz, rw)
bone_morph.offsets.append(morph_data)
self.__model.morphs.append(bone_morph)
def __export_uv_morphs(self, root):
mmd_root = root.mmd_root
if len(mmd_root.uv_morphs) == 0:
return
categories = self.CATEGORIES
for morph in mmd_root.uv_morphs:
uv_morph = pmx.UVMorph(
name=morph.name,
name_e=morph.name_e,
category=categories.get(morph.category, pmx.Morph.CATEGORY_OHTER)
)
offsets = []
for data in morph.data:
dx, dy, dz, dw = data.offset
offset = (dx, -dy, 0, 0) # dz, dw are not used
for idx in self.__vertex_index_map.get(data.index, []):
morph_data = pmx.UVMorphOffset()
morph_data.index = idx
morph_data.offset = offset
offsets.append(morph_data)
uv_morph.offsets = sorted(offsets, key=lambda x: x.index)
self.__model.morphs.append(uv_morph)
def __export_group_morphs(self, root):
mmd_root = root.mmd_root
if len(mmd_root.group_morphs) == 0:
return
categories = self.CATEGORIES
start_index = len(self.__model.morphs)
for morph in mmd_root.group_morphs:
group_morph = pmx.GroupMorph(
name=morph.name,
name_e=morph.name_e,
category=categories.get(morph.category, pmx.Morph.CATEGORY_OHTER)
)
self.__model.morphs.append(group_morph)
morph_map = self.__get_pmx_morph_map()
for morph, group_morph in zip(mmd_root.group_morphs, self.__model.morphs[start_index:]):
for data in morph.data:
morph_index = morph_map.get((data.morph_type, data.name), -1)
if morph_index < 0:
logging.warning('Group Morph (%s): Morph %s was not found.', morph.name, data.name)
continue
morph_data = pmx.GroupMorphOffset()
morph_data.morph = morph_index
morph_data.factor = data.factor
group_morph.offsets.append(morph_data)
def __exportDisplayItems(self, root, bone_map):
res = []
morph_map = self.__get_pmx_morph_map()
for i in root.mmd_root.display_item_frames:
d = pmx.Display()
d.name = i.name
d.name_e = i.name_e
d.isSpecial = i.is_special
items = []
for j in i.items:
if j.type == 'BONE' and j.name in bone_map:
items.append((0, bone_map[j.name]))
elif j.type == 'MORPH' and (j.morph_type, j.name) in morph_map:
items.append((1, morph_map[(j.morph_type, j.name)]))
else:
logging.warning('Display item (%s, %s) was not found.', j.type, j.name)
d.data = items
res.append(d)
self.__model.display = res
def __get_pmx_morph_map(self):
morph_types = {
pmx.GroupMorph : 'group_morphs',
pmx.VertexMorph : 'vertex_morphs',
pmx.BoneMorph : 'bone_morphs',
pmx.UVMorph : 'uv_morphs',
pmx.MaterialMorph : 'material_morphs',
}
morph_map = {}
for i, m in enumerate(self.__model.morphs):
morph_map[(morph_types[type(m)], m.name)] = i
return morph_map
def __exportRigidBodies(self, rigid_bodies, bone_map):
rigid_map = {}
rigid_cnt = 0
for obj in rigid_bodies:
t, r, s = obj.matrix_world.decompose()
r = r.to_euler('YXZ')
rb = obj.rigid_body
if rb is None:
logging.warning(' * Settings of rigid body "%s" not found, skipped!', obj.name)
continue
p_rigid = pmx.Rigid()
mmd_rigid = obj.mmd_rigid
p_rigid.name = mmd_rigid.name_j
p_rigid.name_e = mmd_rigid.name_e
p_rigid.location = mathutils.Vector(t) * self.TO_PMX_MATRIX * self.__scale
p_rigid.rotation = mathutils.Vector(r) * self.TO_PMX_MATRIX * -1
p_rigid.mode = int(mmd_rigid.type)
rigid_shape = mmd_rigid.shape
shape_size = mathutils.Vector(mmd_rigid.size) * (sum(s) / 3)
if rigid_shape == 'SPHERE':
p_rigid.type = 0
p_rigid.size = shape_size * self.__scale
elif rigid_shape == 'BOX':
p_rigid.type = 1
p_rigid.size = shape_size * self.__scale * self.TO_PMX_MATRIX
elif rigid_shape == 'CAPSULE':
p_rigid.type = 2
p_rigid.size = shape_size * self.__scale
else:
raise Exception('Invalid rigid body type: %s %s', obj.name, rigid_shape)
p_rigid.bone = bone_map.get(mmd_rigid.bone, -1)
p_rigid.collision_group_number = mmd_rigid.collision_group_number
mask = 0
for i, v in enumerate(mmd_rigid.collision_group_mask):
if not v:
mask += (1<<i)
p_rigid.collision_group_mask = mask
p_rigid.mass = rb.mass
p_rigid.friction = rb.friction
p_rigid.bounce = rb.restitution
p_rigid.velocity_attenuation = rb.linear_damping
p_rigid.rotation_attenuation = rb.angular_damping
self.__model.rigids.append(p_rigid)
rigid_map[obj] = rigid_cnt
rigid_cnt += 1
return rigid_map
def __exportJoints(self, joints, rigid_map):
for joint in joints:
t, r, s = joint.matrix_world.decompose()
r = r.to_euler('YXZ')
rbc = joint.rigid_body_constraint
if rbc is None:
logging.warning(' * Settings of joint "%s" not found, skipped!', joint.name)
continue
p_joint = pmx.Joint()
mmd_joint = joint.mmd_joint
p_joint.name = mmd_joint.name_j
p_joint.name_e = mmd_joint.name_e
p_joint.location = mathutils.Vector(t) * self.TO_PMX_MATRIX * self.__scale
p_joint.rotation = mathutils.Vector(r) * self.TO_PMX_MATRIX * -1
p_joint.src_rigid = rigid_map.get(rbc.object1, -1)
p_joint.dest_rigid = rigid_map.get(rbc.object2, -1)
p_joint.maximum_location = (mathutils.Vector([
rbc.limit_lin_x_upper,
rbc.limit_lin_y_upper,
rbc.limit_lin_z_upper,
]) * self.TO_PMX_MATRIX * self.__scale).xyz
p_joint.minimum_location =(mathutils.Vector([
rbc.limit_lin_x_lower,
rbc.limit_lin_y_lower,
rbc.limit_lin_z_lower,
]) * self.TO_PMX_MATRIX * self.__scale).xyz
p_joint.maximum_rotation = (mathutils.Vector([
rbc.limit_ang_x_lower,
rbc.limit_ang_y_lower,
rbc.limit_ang_z_lower,
]) * self.TO_PMX_MATRIX * -1).xyz
p_joint.minimum_rotation = (mathutils.Vector([
rbc.limit_ang_x_upper,
rbc.limit_ang_y_upper,
rbc.limit_ang_z_upper,
]) * self.TO_PMX_MATRIX * -1).xyz
p_joint.spring_constant = (mathutils.Vector(mmd_joint.spring_linear) * self.TO_PMX_MATRIX).xyz
p_joint.spring_rotation_constant = (mathutils.Vector(mmd_joint.spring_angular) * self.TO_PMX_MATRIX).xyz
self.__model.joints.append(p_joint)
@staticmethod
def __convertFaceUVToVertexUV(vert_index, uv, normal, vertices_map):
vertices = vertices_map[vert_index]
for i in vertices:
if i.uv is None:
i.uv = uv
i.normal = normal
return i
elif (i.uv - uv).length < 0.001 and (normal - i.normal).length < 0.01:
return i
n = copy.copy(i) # shallow copy should be fine
n.uv = uv
n.normal = normal
vertices.append(n)
return n
@staticmethod
def __triangulate(mesh, custom_normals):
bm = bmesh.new()
bm.from_mesh(mesh)
is_triangulated = True
face_verts_to_loop_id_map = {}
loop_id = 0
for f in bm.faces:
vert_to_loop_id = face_verts_to_loop_id_map.setdefault(f, {})
if is_triangulated and len(f.verts) != 3:
is_triangulated = False
for v in f.verts:
vert_to_loop_id[v] = loop_id
loop_id += 1
loop_normals = None
if is_triangulated:
loop_normals = custom_normals
else:
face_map = bmesh.ops.triangulate(bm, faces=bm.faces, quad_method=1, ngon_method=1)['face_map']
logging.debug(' - Remapping custom normals...')
loop_normals = []
for f in bm.faces:
vert_to_loop_id = face_verts_to_loop_id_map[face_map.get(f, f)]
for v in f.verts:
loop_normals.append(custom_normals[vert_to_loop_id[v]])
logging.debug(' - Done (faces:%d)', len(bm.faces))
bm.to_mesh(mesh)
face_map.clear()
face_verts_to_loop_id_map.clear()
bm.free()
assert(len(loop_normals) == len(mesh.loops))
return loop_normals
@staticmethod
def __get_normals(mesh, matrix):
custom_normals = None
if hasattr(mesh, 'has_custom_normals'):
logging.debug(' - Calculating normals split...')
mesh.calc_normals_split()
custom_normals = [(matrix * l.normal).normalized() for l in mesh.loops]
mesh.free_normals_split()
elif mesh.use_auto_smooth:
logging.debug(' - Calculating normals split (angle:%f)...', mesh.auto_smooth_angle)
mesh.calc_normals_split(mesh.auto_smooth_angle)
custom_normals = [(matrix * l.normal).normalized() for l in mesh.loops]
mesh.free_normals_split()
else:
logging.debug(' - Calculating normals...')
mesh.calc_normals()
#custom_normals = [(matrix * mesh.vertices[l.vertex_index].normal).normalized() for l in mesh.loops]
custom_normals = []
for f in mesh.polygons:
if f.use_smooth:
for v in f.vertices:
custom_normals.append((matrix * mesh.vertices[v].normal).normalized())
else:
for v in f.vertices:
custom_normals.append((matrix * f.normal).normalized())
logging.debug(' - Done (polygons:%d)', len(mesh.polygons))
return custom_normals
def __doLoadMeshData(self, meshObj, bone_map):
vertex_group_names = {i:x.name for i, x in enumerate(meshObj.vertex_groups) if x.name in bone_map}
vg_edge_scale = meshObj.vertex_groups.get('mmd_edge_scale', None)
vg_vertex_order = meshObj.vertex_groups.get('mmd_vertex_order', None)
pmx_matrix = self.TO_PMX_MATRIX * meshObj.matrix_world * self.__scale
sx, sy, sz = meshObj.matrix_world.to_scale()
normal_matrix = pmx_matrix.to_3x3()
if not (sx == sy == sz):
invert_scale_matrix = mathutils.Matrix([[1.0/sx,0,0], [0,1.0/sy,0], [0,0,1.0/sz]])
normal_matrix *= invert_scale_matrix # reset the scale of meshObj.matrix_world
normal_matrix *= invert_scale_matrix # the scale transform of normals
base_mesh = meshObj.to_mesh(bpy.context.scene, True, 'PREVIEW', False)
loop_normals = self.__triangulate(base_mesh, self.__get_normals(base_mesh, normal_matrix))
base_mesh.transform(pmx_matrix)
base_mesh.update(calc_tessface=True)
sort_vertices = self.__vertex_order_map is not None
has_uv_morphs = self.__vertex_index_map is None # currently support for first mesh only
if has_uv_morphs:
self.__vertex_index_map = dict([(v.index, []) for v in base_mesh.vertices])
def _get_weight(vertex_group, vertex, default_weight):
for i in vertex.groups:
if i.group == vertex_group.index:
return vertex_group.weight(vertex.index)
return default_weight
get_edge_scale = None
if vg_edge_scale:
get_edge_scale = lambda x: _get_weight(vg_edge_scale, x, 1)
else:
get_edge_scale = lambda x: 1
get_vertex_order = None
if sort_vertices:
mesh_id = self.__vertex_order_map.setdefault('mesh_id', 0)
self.__vertex_order_map['mesh_id'] += 1
if vg_vertex_order and self.__vertex_order_map['method'] == 'CUSTOM':
get_vertex_order = lambda x: (mesh_id, _get_weight(vg_vertex_order, x, 2), x.index)
else:
get_vertex_order = lambda x: (mesh_id, x.index)
else:
get_vertex_order = lambda x: None
base_vertices = {}
for v in base_mesh.vertices:
base_vertices[v.index] = [_Vertex(
v.co,
[(x.group, x.weight) for x in v.groups if x.weight > 0 and x.group in vertex_group_names],
{},
v.index if has_uv_morphs else None,
get_edge_scale(v),
get_vertex_order(v),
)]
# calculate offsets
shape_key_list = []
if meshObj.data.shape_keys:
for i, kb in enumerate(meshObj.data.shape_keys.key_blocks):
if i == 0: # Basis
continue
if kb.name == 'mmd_sdef_c': # make sure 'mmd_sdef_c' is at first
shape_key_list = [(i, kb)] + shape_key_list
else:
shape_key_list.append((i, kb))
shape_key_names = []
sdef_counts = 0
for i, kb in shape_key_list:
shape_key_name = kb.name
logging.info(' - processing shape key: %s', shape_key_name)
meshObj.active_shape_key_index = i