Skip to content

Commit

Permalink
Tag animations
Browse files Browse the repository at this point in the history
  • Loading branch information
neumond committed Dec 24, 2014
1 parent 7fd81d7 commit a4e2419
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 15 deletions.
12 changes: 10 additions & 2 deletions io_scene_md3/export_md3.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def write_frame(ctx, i, file):
write_struct_to_file(file, '<16s', (b'',)) # frame name, ignored


def write_tag(ctx, i, file):
def write_tag(ctx, frame, i, file):
tag = bpy.context.scene.objects[ctx['tagNames'][i]]
origin = tuple(tag.location)
ox = tuple(tag.matrix_basis[0][:3])
Expand All @@ -81,6 +81,14 @@ def write_tag(ctx, i, file):
write_struct_to_file(file, '<64s12f', (prepare_name(tag.name).encode('utf-8'),) + origin + ox + oy + oz)


def tag_start_frame(ctx, i, file):
bpy.context.scene.frame_set(bpy.context.scene.frame_start + i)


def tag_end_frame(ctx, i, file):
pass


def gather_shader_info(mesh):
'Returning uvmap name, texture name list'
uv_maps = {}
Expand Down Expand Up @@ -355,7 +363,7 @@ def exportMD3(context, filename):
resolve_delayed(ctx, file, 'offFrames', (file.tell(),))
write_n_items(ctx, file, nFrames, write_frame)
resolve_delayed(ctx, file, 'offTags', (file.tell(),))
write_n_items(ctx, file, len(ctx['tagNames']), write_tag)
write_nm_items(ctx, file, nFrames, len(ctx['tagNames']), write_tag, tag_start_frame, tag_end_frame)
resolve_delayed(ctx, file, 'offSurfaces', (file.tell(),))
write_n_items(ctx, file, len(ctx['surfNames']), write_surface)
resolve_delayed(ctx, file, 'offEnd', (file.tell(),))
Expand Down
42 changes: 29 additions & 13 deletions io_scene_md3/import_md3.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def read_struct_from_file(file, fmt):


def cleanup_string(b):
return b.replace(b'\0', b'').decode('utf-8')
return b.replace(b'\0', b'').decode('utf-8', errors='ignore')


def read_n_items(ctx, file, n, offset, func):
Expand All @@ -45,24 +45,36 @@ def read_frame(ctx, i, file):
ctx['frameName{}'.format(i)] = cleanup_string(frame_name)


def read_tag(ctx, i, file):
name = read_struct_from_file(file, '<64s')[0]
b = read_struct_from_file(file, '<3f3f3f3f')
def get_tag_parameters(b):
o = [None, None, None]
origin, o[0], o[1], o[2] = (b[k:k+3] for k in range(0, 12, 3))
o = [mathutils.Vector(item) for item in o]
basis = mathutils.Matrix()
for j in range(3):
basis[j].xyz = o[j]
basis.translation = mathutils.Vector(origin)
return basis


def read_tag(ctx, i, file):
name = read_struct_from_file(file, '<64s')[0]
bpy.ops.object.add(type='EMPTY')
tag = bpy.context.object
tag.name = cleanup_string(name)
tag.empty_draw_type = 'ARROWS'
tag.location = mathutils.Vector(origin)
o = [mathutils.Vector(item) for item in o]
tag.scale = mathutils.Vector(tuple(item.length for item in o))
for item in o:
item.normalize()
mx = mathutils.Matrix()
for j in range(3):
mx[j].xyz = o[j]
tag.rotation_euler = mx.to_euler() # TODO: use tag.matrix_basis?
tag.rotation_mode = 'QUATERNION'
tag.matrix_basis = get_tag_parameters(read_struct_from_file(file, '<3f3f3f3f'))
ctx['tags'].append(tag)


def read_tag_animation(ctx, i, file):
nTags = len(ctx['tags'])
tag = ctx['tags'][i % nTags]
read_struct_from_file(file, '<64s')
tag.matrix_basis = get_tag_parameters(read_struct_from_file(file, '<3f3f3f3f'))
frame = i // nTags
tag.keyframe_insert('location', frame=frame, group='LocRot')
tag.keyframe_insert('rotation_quaternion', frame=frame, group='LocRot')


def guess_texture_filepath(modelpath, imagepath):
Expand Down Expand Up @@ -224,7 +236,11 @@ def importMD3(context, filename):
context.scene.frame_end = nFrames - 1

read_n_items(ctx, file, nFrames, offFrames, read_frame)
ctx['tags'] = []
read_n_items(ctx, file, nTags, offTags, read_tag)
if nFrames > 1:
read_n_items(ctx, file, nTags * nFrames, offTags, read_tag_animation)
del ctx['tags']
read_n_items(ctx, file, nSurfaces, offSurfaces, read_surface)

context.scene.frame_set(0)
Expand Down

0 comments on commit a4e2419

Please sign in to comment.