@@ -29,7 +29,7 @@ def read_struct_from_file(file, fmt):
2929
3030
3131def cleanup_string (b ):
32- return b .replace (b'\0 ' , b'' ).decode ('utf-8' )
32+ return b .replace (b'\0 ' , b'' ).decode ('utf-8' , errors = 'ignore' )
3333
3434
3535def read_n_items (ctx , file , n , offset , func ):
@@ -45,24 +45,36 @@ def read_frame(ctx, i, file):
4545 ctx ['frameName{}' .format (i )] = cleanup_string (frame_name )
4646
4747
48- def read_tag (ctx , i , file ):
49- name = read_struct_from_file (file , '<64s' )[0 ]
50- b = read_struct_from_file (file , '<3f3f3f3f' )
48+ def get_tag_parameters (b ):
5149 o = [None , None , None ]
5250 origin , o [0 ], o [1 ], o [2 ] = (b [k :k + 3 ] for k in range (0 , 12 , 3 ))
51+ o = [mathutils .Vector (item ) for item in o ]
52+ basis = mathutils .Matrix ()
53+ for j in range (3 ):
54+ basis [j ].xyz = o [j ]
55+ basis .translation = mathutils .Vector (origin )
56+ return basis
57+
58+
59+ def read_tag (ctx , i , file ):
60+ name = read_struct_from_file (file , '<64s' )[0 ]
5361 bpy .ops .object .add (type = 'EMPTY' )
5462 tag = bpy .context .object
5563 tag .name = cleanup_string (name )
5664 tag .empty_draw_type = 'ARROWS'
57- tag .location = mathutils .Vector (origin )
58- o = [mathutils .Vector (item ) for item in o ]
59- tag .scale = mathutils .Vector (tuple (item .length for item in o ))
60- for item in o :
61- item .normalize ()
62- mx = mathutils .Matrix ()
63- for j in range (3 ):
64- mx [j ].xyz = o [j ]
65- tag .rotation_euler = mx .to_euler () # TODO: use tag.matrix_basis?
65+ tag .rotation_mode = 'QUATERNION'
66+ tag .matrix_basis = get_tag_parameters (read_struct_from_file (file , '<3f3f3f3f' ))
67+ ctx ['tags' ].append (tag )
68+
69+
70+ def read_tag_animation (ctx , i , file ):
71+ nTags = len (ctx ['tags' ])
72+ tag = ctx ['tags' ][i % nTags ]
73+ read_struct_from_file (file , '<64s' )
74+ tag .matrix_basis = get_tag_parameters (read_struct_from_file (file , '<3f3f3f3f' ))
75+ frame = i // nTags
76+ tag .keyframe_insert ('location' , frame = frame , group = 'LocRot' )
77+ tag .keyframe_insert ('rotation_quaternion' , frame = frame , group = 'LocRot' )
6678
6779
6880def guess_texture_filepath (modelpath , imagepath ):
@@ -224,7 +236,11 @@ def importMD3(context, filename):
224236 context .scene .frame_end = nFrames - 1
225237
226238 read_n_items (ctx , file , nFrames , offFrames , read_frame )
239+ ctx ['tags' ] = []
227240 read_n_items (ctx , file , nTags , offTags , read_tag )
241+ if nFrames > 1 :
242+ read_n_items (ctx , file , nTags * nFrames , offTags , read_tag_animation )
243+ del ctx ['tags' ]
228244 read_n_items (ctx , file , nSurfaces , offSurfaces , read_surface )
229245
230246 context .scene .frame_set (0 )
0 commit comments