-
Notifications
You must be signed in to change notification settings - Fork 18
/
import_utils.py
825 lines (638 loc) · 28.9 KB
/
import_utils.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
# <pep8 compliant>
# Written by Stephan Vedder and Michael Schnabel
import os
import bmesh
import bpy
from bpy_extras import node_shader_utils
from bpy_extras.image_utils import load_image
from io_mesh_w3d.shared.structs.animation import *
from io_mesh_w3d.w3d.adaptive_delta import decode
from io_mesh_w3d.w3d.structs.mesh_structs.vertex_material import *
def insensitive_path(path):
# find the io_stream on unix
directory = os.path.dirname(path)
name = os.path.basename(path)
for io_streamname in os.listdir(directory):
if io_streamname.lower() == name.lower():
path = os.path.join(directory, io_streamname)
return path
def link_object_to_active_scene(obj, coll):
coll.objects.link(obj)
bpy.context.view_layer.objects.active = obj
obj.select_set(True)
def smooth_mesh(mesh_ob, mesh):
if mesh_ob.mode != 'OBJECT':
bpy.ops.object.mode_set(mode='OBJECT')
for polygon in mesh.polygons:
polygon.use_smooth = True
def get_collection(hlod=None, index=''):
if hlod is not None:
name = hlod.model_name() + index
if name in bpy.data.collections:
return bpy.data.collections[name]
coll = bpy.data.collections.new(name)
bpy.context.scene.collection.children.link(coll)
return coll
return bpy.context.scene.collection
##########################################################################
# data creation
##########################################################################
def create_data(
context,
meshes,
hlod=None,
hierarchy=None,
boxes=[],
animation=None,
compressed_animation=None,
dazzles=[]):
rig = None
coll = get_collection(hlod)
rig = get_or_create_skeleton(hlod, hierarchy, coll)
if hlod is not None:
current_coll = coll
for i, lod_array in enumerate(reversed(hlod.lod_arrays)):
if i > 0:
current_coll = get_collection(hlod, '.' + str(i))
# collection has no hide_set()
current_coll.hide_viewport = True
for sub_object in lod_array.sub_objects:
for mesh in meshes:
if mesh.name() == sub_object.name:
create_mesh(context, mesh, hierarchy, current_coll)
for box in boxes:
if box.name() == sub_object.name:
create_box(box, hlod, hierarchy, rig, coll)
for dazzle in dazzles:
if dazzle.name() == sub_object.name:
create_dazzle(context, dazzle, hlod, hierarchy, rig, coll)
for lod_array in reversed(hlod.lod_arrays):
for sub_object in lod_array.sub_objects:
for mesh in meshes:
if mesh.name() == sub_object.name:
rig_mesh(mesh, hierarchy, rig, sub_object)
for dazzle in dazzles:
if dazzle.name() == sub_object.name:
dazzle_object = bpy.data.objects[dazzle.name()]
rig_object(dazzle_object, hierarchy, rig, sub_object)
else:
for mesh in meshes:
create_mesh(context, mesh, hierarchy, coll)
create_animation(context, rig, animation, hierarchy)
create_animation(context, rig, compressed_animation, hierarchy, compressed=True)
def rig_object(object, hierarchy, rig, sub_object):
if hierarchy is None or not sub_object or sub_object.bone_index <= 0:
return
pivot = hierarchy.pivots[sub_object.bone_index]
if rig is not None and pivot.name in rig.pose.bones:
object.parent = rig
object.parent_bone = pivot.name
object.parent_type = 'BONE'
return
object.rotation_mode = 'QUATERNION'
object.delta_location = pivot.translation
object.delta_rotation_quaternion = pivot.rotation
if pivot.parent_id <= 0:
return
parent_pivot = hierarchy.pivots[pivot.parent_id]
if parent_pivot.name in bpy.data.objects:
object.parent = bpy.data.objects[parent_pivot.name]
elif rig is not None and parent_pivot.name in rig.pose.bones:
object.parent = rig
object.parent_bone = parent_pivot.name
object.parent_type = 'BONE'
##########################################################################
# mesh
##########################################################################
def create_mesh(context, mesh_struct, hierarchy, coll):
triangles = []
for triangle in mesh_struct.triangles:
triangles.append(tuple(triangle.vert_ids))
mesh = bpy.data.meshes.new(mesh_struct.name())
mesh.from_pydata(mesh_struct.verts, [], triangles)
mesh.update()
mesh.validate()
mesh_ob = bpy.data.objects.new(mesh_struct.name(), mesh)
mesh_ob.object_type = 'NORMAL'
mesh_ob.userText = mesh_struct.user_text
mesh_ob.use_empty_image_alpha = True
smooth_mesh(mesh_ob, mesh)
link_object_to_active_scene(mesh_ob, coll)
if mesh_struct.is_hidden():
mesh_ob.hide_set(True)
principleds = []
# vertex material stuff
name = mesh_struct.name()
if mesh_struct.vert_materials:
create_vertex_material(context, principleds, mesh_struct, mesh, name, triangles)
for i, shader in enumerate(mesh_struct.shaders):
set_shader_properties(mesh.materials[i], shader)
# shader material stuff
if mesh_struct.shader_materials:
for i, shaderMat in enumerate(mesh_struct.shader_materials):
(material, principled) = create_material_from_shader_material(
context, mesh_struct.name(), shaderMat)
mesh.materials.append(material)
principleds.append(principled)
if mesh_struct.material_passes:
b_mesh = bmesh.new()
b_mesh.from_mesh(mesh)
for mat_pass in mesh_struct.material_passes:
create_uvlayer(context, mesh, b_mesh, triangles, mat_pass)
def rig_mesh(mesh_struct, hierarchy, rig, sub_object=None):
mesh_ob = bpy.data.objects[mesh_struct.name()]
if hierarchy is None or not hierarchy.pivots:
return
if mesh_struct.is_skin():
mesh = bpy.data.meshes[mesh_ob.name]
for i, vert_inf in enumerate(mesh_struct.vert_infs):
weight = vert_inf.bone_inf
if weight < 0.01:
weight = 1.0
pivot = hierarchy.pivots[vert_inf.bone_idx]
if vert_inf.bone_idx <= 0:
bone = rig
else:
bone = rig.data.bones[pivot.name]
mesh.vertices[i].co = bone.matrix_local @ mesh.vertices[i].co
if pivot.name not in mesh_ob.vertex_groups:
mesh_ob.vertex_groups.new(name=pivot.name)
mesh_ob.vertex_groups[pivot.name].add(
[i], weight, 'REPLACE')
if vert_inf.xtra_idx > 0:
xtra_pivot = hierarchy.pivots[vert_inf.xtra_idx]
if xtra_pivot.name not in mesh_ob.vertex_groups:
mesh_ob.vertex_groups.new(name=xtra_pivot.name)
mesh_ob.vertex_groups[xtra_pivot.name].add(
[i], vert_inf.xtra_inf, 'ADD')
modifier = mesh_ob.modifiers.new(rig.name, 'ARMATURE')
modifier.object = rig
modifier.use_bone_envelopes = False
modifier.use_vertex_groups = True
else:
rig_object(mesh_ob, hierarchy, rig, sub_object)
##########################################################################
# skeleton
##########################################################################
def get_or_create_skeleton(hlod, hierarchy, coll):
if hierarchy is None:
return None
if hierarchy.header.name in bpy.data.objects:
obj = bpy.data.objects[hierarchy.header.name]
if obj.type == 'ARMATURE':
return obj
return None
sub_objects = []
if hlod is not None:
sub_objects = hlod.lod_arrays[-1].sub_objects
return create_bone_hierarchy(hierarchy, sub_objects, coll)
def make_transform_matrix(loc, rot):
mat_loc = Matrix.Translation(loc)
mat_rot = Quaternion(rot).to_matrix().to_4x4()
return mat_loc @ mat_rot
def create_rig(name, location, coll):
basic_sphere = create_sphere()
armature = bpy.data.armatures.new(name)
armature.show_names = False
rig = bpy.data.objects.new(name, armature)
rig.location = location
rig.rotation_mode = 'QUATERNION'
rig.track_axis = 'POS_X'
link_object_to_active_scene(rig, coll)
bpy.ops.object.mode_set(mode='EDIT')
return (rig, armature)
def create_bone_hierarchy(hierarchy, sub_objects, coll):
root = hierarchy.pivots[0]
rig = None
for i, pivot in enumerate(hierarchy.pivots):
pivot.is_bone = True
for obj in sub_objects:
if obj.bone_index == i and obj.name == pivot.name:
pivot.is_bone = False
for i, pivot in reversed(list(enumerate(hierarchy.pivots))):
childs = [child for child in hierarchy.pivots if child.parent_id == i]
for child in childs:
if child.is_bone:
pivot.is_bone = True
armature = None
for pivot in hierarchy.pivots:
if pivot.parent_id == -1 or not pivot.is_bone:
continue
if rig is None:
(rig, armature) = create_rig(
hierarchy.name(), root.translation, coll)
# TODO: also rotate armature/rig
bone = armature.edit_bones.new(pivot.name)
matrix = make_transform_matrix(pivot.translation, pivot.rotation)
if pivot.parent_id > 0:
parent_pivot = hierarchy.pivots[pivot.parent_id]
if parent_pivot.name in armature.edit_bones:
bone.parent = armature.edit_bones[parent_pivot.name]
matrix = bone.parent.matrix @ matrix
bone.head = Vector((0.0, 0.0, 0.0))
# has to point in y direction, so rotation is applied correctly
bone.tail = Vector((0.0, 0.01, 0.0))
bone.matrix = matrix
if rig is not None:
if rig.mode != 'POSE':
bpy.ops.object.mode_set(mode='POSE')
basic_sphere = create_sphere()
for bone in rig.pose.bones:
bone.custom_shape = basic_sphere
if rig.mode != 'OBJECT':
bpy.ops.object.mode_set(mode='OBJECT')
return rig
##########################################################################
# create material
##########################################################################
def create_vertex_material(context, principleds, struct, mesh, name, triangles):
for vertMat in struct.vert_materials:
(material, principled) = create_material_from_vertex_material(
context, name, vertMat)
mesh.materials.append(material)
principleds.append(principled)
if struct.material_passes:
b_mesh = bmesh.new()
b_mesh.from_mesh(mesh)
for mat_pass in struct.material_passes:
create_uvlayer(context, mesh, b_mesh, triangles, mat_pass)
if mat_pass.tx_stages:
tx_stage = mat_pass.tx_stages[0]
mat_id = mat_pass.vertex_material_ids[0]
tex_id = tx_stage.tx_ids[0]
texture = struct.textures[tex_id]
tex = find_texture(context, texture.file, texture.id)
principleds[mat_id].base_color_texture.image = tex
#principleds[mat_id].alpha_texture.image = tex
def create_material_from_vertex_material(context, name, vert_mat):
name = name + "." + vert_mat.vm_name
if name in bpy.data.materials:
material = bpy.data.materials[name]
principled = node_shader_utils.PrincipledBSDFWrapper(material, is_readonly=False)
return (material, principled)
material = bpy.data.materials.new(name)
material.material_type = 'VERTEX_MATERIAL'
material.use_nodes = True
material.blend_method = 'BLEND'
material.show_transparent_back = False
atts = {'DEFAULT'}
attributes = vert_mat.vm_info.attributes
if attributes & USE_DEPTH_CUE:
atts.add('USE_DEPTH_CUE')
if attributes & ARGB_EMISSIVE_ONLY:
atts.add('ARGB_EMISSIVE_ONLY')
if attributes & COPY_SPECULAR_TO_DIFFUSE:
atts.add('COPY_SPECULAR_TO_DIFFUSE')
if attributes & DEPTH_CUE_TO_ALPHA:
atts.add('DEPTH_CUE_TO_ALPHA')
principled = node_shader_utils.PrincipledBSDFWrapper(material, is_readonly=False)
principled.base_color = vert_mat.vm_info.diffuse.to_vector_rgb()
principled.alpha = vert_mat.vm_info.opacity
material.attributes = atts
material.specular_intensity = vert_mat.vm_info.shininess
material.specular_color = vert_mat.vm_info.specular.to_vector_rgb()
material.emission = vert_mat.vm_info.emissive.to_vector_rgba()
material.ambient = vert_mat.vm_info.ambient.to_vector_rgba()
material.translucency = vert_mat.vm_info.translucency
material.opacity = vert_mat.vm_info.opacity
material.vm_args_0 = vert_mat.vm_args_0
material.vm_args_1 = vert_mat.vm_args_1
return (material, principled)
def create_material_from_shader_material(context, name, shader_mat):
name = name + '.' + shader_mat.header.type_name
if name in bpy.data.materials:
material = bpy.data.materials[name]
principled = node_shader_utils.PrincipledBSDFWrapper(material, is_readonly=False)
return (material, principled)
material = bpy.data.materials.new(name)
material.material_type = 'SHADER_MATERIAL'
material.use_nodes = True
material.blend_method = 'BLEND'
material.show_transparent_back = False
if shader_mat.header.technique_index is not None:
material.technique = shader_mat.header.technique_index
principled = node_shader_utils.PrincipledBSDFWrapper(material, is_readonly=False)
for prop in shader_mat.properties:
if prop.name == 'DiffuseTexture' and prop.value != '':
principled.base_color_texture.image = find_texture(context, prop.value)
elif prop.name == 'NormalMap' and prop.value != '':
principled.normalmap_texture.image = find_texture(context, prop.value)
elif prop.name == 'BumpScale':
principled.normalmap_strength = prop.value
elif prop.name == 'SpecMap' and prop.value != '':
principled.specular_texture.image = find_texture(context, prop.value)
elif prop.name == 'SpecularExponent' or prop.name == 'Shininess':
material.specular_intensity = prop.value
elif prop.name == 'DiffuseColor' or prop.name == 'ColorDiffuse':
if isinstance(prop.value, RGBA):
material.diffuse_color = prop.value.to_vector_rgba(alpha=1.0)
else:
material.diffuse_color = RGBA(vec=prop.value, a=1.0).to_vector_rgba(alpha=1.0)
elif prop.name == 'SpecularColor' or prop.name == 'ColorSpecular':
if isinstance(prop.value, RGBA):
material.specular_color = prop.value.to_vector_rgb()
else:
material.specular_color = prop.value
elif prop.name == 'CullingEnable':
material.use_backface_culling = prop.value
# all props below have no effect on shading -> custom properties for roundtrip purpose
elif prop.name == 'AmbientColor' or prop.name == 'ColorAmbient':
if isinstance(prop.value, RGBA):
material.ambient = prop.value.to_vector_rgba()
else:
material.ambient = RGBA(vec=prop.value, a=1.0).to_vector_rgba(alpha=1.0)
elif prop.name == 'EmissiveColor' or prop.name == 'ColorEmissive':
if isinstance(prop.value, RGBA):
material.emission = prop.value.to_vector_rgba()
else:
material.emission = RGBA(vec=prop.value, a=1.0).to_vector_rgba(alpha=1.0)
elif prop.name == 'Opacity':
material.opacity = prop.value
elif prop.name == 'AlphaTestEnable':
material.alpha_test = prop.value
elif prop.name == 'BlendMode': # is blend_method ?
material.blend_mode = prop.value
elif prop.name == 'BumpUVScale':
material.bump_uv_scale = prop.value.xy
elif prop.name == 'EdgeFadeOut':
material.edge_fade_out = prop.value
elif prop.name == 'DepthWriteEnable':
material.depth_write = prop.value
elif prop.name == 'Sampler_ClampU_ClampV_NoMip_0':
material.sampler_clamp_uv_no_mip_0 = prop.value
elif prop.name == 'Sampler_ClampU_ClampV_NoMip_1':
material.sampler_clamp_uv_no_mip_1 = prop.value
elif prop.name == 'NumTextures':
material.num_textures = prop.value # is 1 if texture_0 and texture_1 are set
elif prop.name == 'Texture_0': # diffuse texture
material.texture_0 = prop.value
elif prop.name == 'Texture_1': # second diffuse texture
material.texture_1 = prop.value
elif prop.name == 'SecondaryTextureBlendMode':
material.secondary_texture_blend_mode = prop.value
elif prop.name == 'TexCoordMapper_0':
material.tex_coord_mapper_0 = prop.value
elif prop.name == 'TexCoordMapper_1':
material.tex_coord_mapper_1 = prop.value
elif prop.name == 'TexCoordTransform_0':
material.tex_coord_transform_0 = prop.value.to_vector_rgba()
elif prop.name == 'TexCoordTransform_1':
material.tex_coord_transform_1 = prop.value.to_vector_rgba()
elif prop.name == 'EnvironmentTexture':
material.environment_texture = prop.value
elif prop.name == 'EnvMult':
material.environment_mult = prop.value
elif prop.name == 'RecolorTexture':
material.recolor_texture = prop.value
elif prop.name == 'RecolorMultiplier':
material.recolor_mult = prop.value
elif prop.name == 'UseRecolorColors':
material.use_recolor = prop.value
elif prop.name == 'HouseColorPulse':
material.house_color_pulse = prop.value
elif prop.name == 'ScrollingMaskTexture':
material.scrolling_mask_texture = prop.value
elif prop.name == 'TexCoordTransformAngle_0':
material.tex_coord_transform_angle = prop.value
elif prop.name == 'TexCoordTransformU_0':
material.tex_coord_transform_u_0 = prop.value
elif prop.name == 'TexCoordTransformV_0':
material.tex_coord_transform_v_0 = prop.value
elif prop.name == 'TexCoordTransformU_1':
material.tex_coord_transform_u_1 = prop.value
elif prop.name == 'TexCoordTransformV_1':
material.tex_coord_transform_v_1 = prop.value
elif prop.name == 'TexCoordTransformU_2':
material.tex_coord_transform_u_2 = prop.value
elif prop.name == 'TexCoordTransformV_2':
material.tex_coord_transform_v_2 = prop.value
elif prop.name == 'TextureAnimation_FPS_NumPerRow_LastFrame_FrameOffset_0':
material.tex_ani_fps_NPR_lastFrame_frameOffset_0 = prop.value.to_vector_rgba()
elif prop.name == 'IonHullTexture':
material.ion_hull_texture = prop.value
elif prop.name == 'MultiTextureEnable':
material.multi_texture_enable = prop.value
else:
context.error('shader property not implemented: ' + prop.name)
return (material, principled)
##########################################################################
# set shader properties
##########################################################################
def set_shader_properties(material, shader):
material.shader.depth_compare = shader.depth_compare
material.shader.depth_mask = shader.depth_mask
material.shader.color_mask = shader.color_mask
material.shader.dest_blend = shader.dest_blend
material.shader.fog_func = shader.fog_func
material.shader.pri_gradient = shader.pri_gradient
material.shader.sec_gradient = shader.sec_gradient
material.shader.src_blend = shader.src_blend
material.shader.texturing = shader.texturing
material.shader.detail_color_func = shader.detail_color_func
material.shader.detail_alpha_func = shader.detail_alpha_func
material.shader.shader_preset = shader.shader_preset
material.shader.alpha_test = shader.alpha_test
material.shader.post_detail_color_func = shader.post_detail_color_func
material.shader.post_detail_alpha_func = shader.post_detail_alpha_func
##########################################################################
# create uvlayer
##########################################################################
def create_uvlayer(context, mesh, b_mesh, tris, mat_pass):
tx_coords = None
if mat_pass.tx_coords:
tx_coords = mat_pass.tx_coords
else:
if len(mat_pass.tx_stages) > 0:
tx_coords = mat_pass.tx_stages[0].tx_coords
if len(mat_pass.tx_stages) > 1:
context.warning('only one texture stage per material pass supported on export')
if not tx_coords:
return
uv_layer = mesh.uv_layers.new(do_init=False)
for i, face in enumerate(b_mesh.faces):
for loop in face.loops:
idx = tris[i][loop.index % 3]
uv_layer.data[loop.index].uv = tx_coords[idx].xy
##########################################################################
# load texture
##########################################################################
def find_texture(context, file, name=None):
if name is None:
name = file
file = file.split('.', -1)[0]
if name in bpy.data.images:
return bpy.data.images[name]
path = insensitive_path(os.path.dirname(context.filepath))
filepath = path + os.path.sep + file
extensions = ['.dds', '.tga', '.jpg', '.jpeg', '.png', '.bmp']
for extension in extensions:
img = load_image(filepath + extension)
if img is not None:
context.info('loaded texture: ' + filepath + extension)
break
if img is None:
context.warning('texture not found: ' + filepath + ' ' + str(extensions))
img = bpy.data.images.new(name, width=2048, height=2048)
img.generated_type = 'COLOR_GRID'
img.source = 'GENERATED'
img.name = name
img.alpha_mode = 'STRAIGHT'
return img
##########################################################################
# createAnimation
##########################################################################
def is_roottransform(channel):
return channel.pivot == 0
def is_translation(channel):
return channel.type < 3
def is_rotation(channel):
return channel.type == 6
def is_visibility(channel):
return isinstance(channel, AnimationBitChannel)
def get_bone(context, rig, hierarchy, channel):
if is_roottransform(channel):
return rig
pivot = hierarchy.pivots[channel.pivot]
if rig is not None:
if is_visibility(channel) and pivot.name in rig.data.bones:
return rig.data.bones[pivot.name]
elif pivot.name in rig.pose.bones:
return rig.pose.bones[pivot.name]
return bpy.data.objects[pivot.name]
def setup_animation(animation):
bpy.context.scene.render.fps = animation.header.frame_rate
bpy.context.scene.frame_start = 0
bpy.context.scene.frame_end = animation.header.num_frames - 1
# this causes issues on timecoded animation export (if e.g. quat.w has less keyframes as x,y,z of that quat)
creation_options = {'INSERTKEY_NEEDED'}
def set_translation(bone, index, frame, value):
bone.location[index] = value
bone.keyframe_insert(data_path='location', index=index,
frame=frame) # , options=creation_options)
def set_rotation(bone, frame, value):
bone.rotation_quaternion = value
bone.keyframe_insert(data_path='rotation_quaternion',
frame=frame) # , options=creation_options)
def set_visibility(bone, frame, value):
if isinstance(bone, bpy.types.Bone):
bone.hide = value
bone.keyframe_insert(data_path='hide', frame=frame) # , options=creation_options)
else:
bone.hide_viewport = value
bone.keyframe_insert(data_path='hide_viewport',
frame=frame) # , options=creation_options)
def set_keyframe(bone, channel, frame, value):
if is_visibility(channel):
set_visibility(bone, frame, value)
elif is_translation(channel):
set_translation(bone, channel.type, frame, value)
elif is_rotation(channel):
set_rotation(bone, frame, value)
def apply_timecoded(bone, channel, _):
for key in channel.time_codes:
set_keyframe(bone, channel, key.time_code, key.value)
def apply_motion_channel_time_coded(bone, channel):
for dat in channel.data:
set_keyframe(bone, channel, dat.time_code, dat.value)
def apply_adaptive_delta(bone, channel):
data = decode(channel)
for i in range(channel.num_time_codes):
set_keyframe(bone, channel, i, data[i])
def apply_uncompressed(bone, channel, hierarchy):
for index in range(channel.last_frame - channel.first_frame + 1):
data = channel.data[index]
frame = index + channel.first_frame
set_keyframe(bone, channel, frame, data)
def process_channels(context, hierarchy, channels, rig, apply_func):
for channel in channels:
obj = get_bone(context, rig, hierarchy, channel)
apply_func(obj, channel, hierarchy)
def process_motion_channels(context, hierarchy, channels, rig):
for channel in channels:
obj = get_bone(context, rig, hierarchy, channel)
if channel.delta_type == 0:
apply_motion_channel_time_coded(obj, channel)
else:
apply_adaptive_delta(obj, channel)
def create_animation(context, rig, animation, hierarchy, compressed=False):
if animation is None:
return
setup_animation(animation)
if not compressed:
process_channels(context, hierarchy, animation.channels,
rig, apply_uncompressed)
else:
process_channels(context, hierarchy, animation.time_coded_channels,
rig, apply_timecoded)
process_channels(context, hierarchy, animation.adaptive_delta_channels,
rig, apply_adaptive_delta)
process_motion_channels(context, hierarchy, animation.motion_channels, rig)
bpy.context.scene.frame_set(0)
##########################################################################
# create basic meshes
##########################################################################
def create_sphere():
mesh = bpy.data.meshes.new('Basic_Sphere')
basic_sphere = bpy.data.objects.new("Basic_Sphere", mesh)
b_mesh = bmesh.new()
bmesh.ops.create_uvsphere(b_mesh, u_segments=12, v_segments=6, diameter=35)
b_mesh.to_mesh(mesh)
b_mesh.free()
return basic_sphere
def create_cone(name):
mesh = bpy.data.meshes.new(name)
cone = bpy.data.objects.new(name, mesh)
b_mesh = bmesh.new()
bmesh.ops.create_cone(b_mesh, cap_ends=True, cap_tris=True,
segments=10, diameter1=0, diameter2=1.0, depth=2.0, calc_uvs=True)
b_mesh.to_mesh(mesh)
b_mesh.free()
return (mesh, cone)
def create_dazzle(context, dazzle, hlod, hierarchy, rig, coll):
# Todo: proper dimensions for cone
(dazzle_mesh, dazzle_cone) = create_cone(dazzle.name())
dazzle_cone.object_type = 'DAZZLE'
dazzle_cone.dazzle_type = dazzle.type_name
link_object_to_active_scene(dazzle_cone, coll)
material = bpy.data.materials.new(dazzle.name())
material.use_nodes = True
material.blend_method = 'BLEND'
material.show_transparent_back = False
principled = node_shader_utils.PrincipledBSDFWrapper(material, is_readonly=False)
principled.base_color = (255, 255, 255)
principled.base_color_texture.image = find_texture(context, 'SunDazzle.tga')
dazzle_mesh.materials.append(material)
def create_box(box, hlod, hierarchy, rig, coll):
if box is None:
return
x = box.extend[0] / 2.0
y = box.extend[1] / 2.0
z = box.extend[2]
verts = [(x, y, z), (-x, y, z), (-x, -y, z), (x, -y, z),
(x, y, 0), (-x, y, 0), (-x, -y, 0), (x, -y, 0)]
faces = [(0, 1, 2, 3), (4, 5, 6, 7), (0, 4, 5, 1),
(1, 5, 6, 2), (2, 6, 7, 3), (3, 7, 4, 0)]
cube = bpy.data.meshes.new(box.name())
cube.from_pydata(verts, [], faces)
cube.update(calc_edges=True)
box_object = bpy.data.objects.new(box.name(), cube)
box_object.object_type = 'BOX'
box_object.display_type = 'WIRE'
mat = bpy.data.materials.new(box.name() + ".Material")
mat.diffuse_color = box.color.to_vector_rgba()
cube.materials.append(mat)
box_object.location = box.center
link_object_to_active_scene(box_object, coll)
if hierarchy is None or rig is None:
return
sub_objects = [
sub_object for sub_object in hlod.lod_arrays[-1].sub_objects if sub_object.name == box.name()]
if not sub_objects:
return
sub_object = sub_objects[0]
if sub_object.bone_index == 0:
return
pivot = hierarchy.pivots[sub_object.bone_index]
box_object.parent = rig
box_object.parent_bone = pivot.name
box_object.parent_type = 'BONE'