("submesh_count",c_uint32),
("classtype",c_uint32),
("offset",c_uint32),
- ("children",c_uint32),
+ ("parent",c_uint32),
("pstr_name",c_uint32)]
class mdl_header(Structure):
("node_count",c_uint32),
("node_offset",c_uint32),
+ ("anim_count",c_uint32),
+ ("anim_offset",c_uint32),
+
("strings_offset",c_uint32),
("entdata_offset",c_uint32),
-
- ("anim_count",c_uint32),
- ("anim_offset",c_uint32)
+ ("animdata_offset",c_uint32)
]
+class mdl_animation(Structure):
+ _pack_ = 1
+ _fields_ = [("pstr_name",c_uint32),
+ ("length",c_uint32),
+ ("rate",c_float),
+ ("offset",c_uint32)]
+
+class mdl_keyframe(Structure):
+ _pack_ = 1
+ _fields_ = [("co",c_float*3),
+ ("q",c_float*4),
+ ("s",c_float*3)]
+
# Entity types
# ==========================================
class classtype_skeleton(Structure):
_pack_ = 1
- _fields_ = [("anim_start",c_uint32),
+ _fields_ = [("channels",c_uint32),
+ ("ik_count",c_uint32),
+ ("collider_count",c_uint32),
+ ("anim_start",c_uint32),
("anim_count",c_uint32)]
class classtype_bone(Structure):
_pack_ = 1
- _fields_ = [("deform",c_uint32)]
+ _fields_ = [("deform",c_uint32),
+ ("ik_target",c_uint32),
+ ("ik_pole",c_uint32),
+ ("collider",c_uint32),
+ ("hitbox",(c_float*3)*2)]
# Exporter
# ==============================================================================
entdata_buffer = []
entdata_length = 0
+ anim_buffer = []
+ animdata_length = 0
+ animdata_buffer = []
+
def emplace_string( s ):
nonlocal string_cache, strings_buffer
return uid
print( " creating scene graph" )
- graph = {"obj": None, "depth": 0, "children": [], "uid": _uid()}
+ graph = {}
+ graph["obj"] = None
+ graph["depth"] = 0
+ graph["children"] = []
+ graph["uid"] = _uid()
+ graph["parent"] = None
+
graph_lookup = {} # object can lookup its graph def here
for obj in collection.all_objects:
def _extend( p, n, d ):
uid = _uid()
- tree = {"obj":n, "depth": d, "children":[], "uid": uid}
+ tree = {}
+ tree["uid"] = uid
+ tree["children"] = []
+ tree["depth"] = d
+ tree["obj"] = n
+ tree["parent"] = p
n.cv_data.uid = uid
if n.type == 'ARMATURE':
tree["bones"] = [None] # None is the root transform
+ tree["ik_count"] = 0
+ tree["collider_count"] = 0
def _extendb( p, n, d ):
nonlocal tree
- btree = {"bone":n, "depth": d, "children":[], "uid": _uid()}
+ btree = {}
+ btree["bone"] = n
+ btree["uid"] = _uid()
+ btree["children"] = []
+ btree["depth"] = d
+ btree["parent"] = p
+ tree["bones"] += [n.name]
+
for c in n.children:
_extendb( btree, c, d+1 )
+ for c in tree['obj'].pose.bones[n.name].constraints:
+ if c.type == 'IK':
+ btree["target"] = c.subtarget
+ btree["pole"] = c.pole_subtarget
+ tree["ik_count"] += 1
+
+ if n.cv_data.collider:
+ tree['collider_count'] += 1
+
btree['deform'] = n.use_deform
p['children'] += [btree]
- if n.use_deform:
- tree["bones"] += [n.name]
-
for b in n.data.bones:
if not b.parent:
_extendb( tree, b, d+1 )
it = _graph_iter(graph)
- root.children = len(graph['children'])
+ root.parent = 0xffffffff
# Compile
# ==============================================
node.q[3] = quat[0]
if objt == 'BONE':
- node.s[0] = obj.tail[0]
- node.s[1] = obj.tail[2]
- node.s[2] = -obj.tail[1]
+ node.s[0] = obj.tail_local[0] - node.co[0]
+ node.s[1] = obj.tail_local[2] - node.co[1]
+ node.s[2] = -obj.tail_local[1] - node.co[2]
else:
node.s[0] = obj.scale[0]
node.s[1] = obj.scale[2]
node.pstr_name = emplace_string( obj.name )
+ if node_def["parent"]:
+ node.parent = node_def["parent"]["uid"]
+
if objt == 'BONE':
classtype = 'k_classtype_bone'
elif objt == 'ARMATURE':
if mod.type == 'ARMATURE':
classtype = 'k_classtype_skin'
armature_def = graph_lookup[mod.object]
+ POSE_OR_REST_CACHE = armature_def['obj'].data.pose_position
+
+ armature_def['obj'].data.pose_position = 'REST'
if can_use_cache and obj.data.name in mesh_cache:
ref = mesh_cache[obj.data.name]
s001 = F" L {obj.name}"
s002 = s000+s001
s003 = F"{disptype}"
- s004 = ""
+ s004 = F"{node.parent: 3}"
+ s005 = ""
+
if classtype == 'k_classtype_skin':
- s004 = F"-> {armature_def['obj'].cv_data.uid}"
+ armature_def['obj'].data.pose_position = POSE_OR_REST_CACHE
+ s005 = F" [armature -> {armature_def['obj'].cv_data.uid}]"
- scmp = F"{s002:<32} {s003:<16} {s004}"
+ scmp = F"{s002:<32} {s003:<22} {s004} {s005}"
print( scmp )
if classtype == 'k_classtype_INSTANCE' or \
elif classtype == 'k_classtype_skeleton':
node.classtype = 11
entdata_length += sizeof( classtype_skeleton )
-
skeleton = classtype_skeleton()
- skeleton.anim_start = 0
- skeleton.anim_count = 0
+
+ armature_def = graph_lookup[obj]
+ armature = obj
+ bones = armature_def['bones']
+ skeleton.channels = len(bones)
+ skeleton.ik_count = armature_def["ik_count"]
+ skeleton.collider_count = armature_def["collider_count"]
+
+ if armature.animation_data:
+ previous_frame = bpy.context.scene.frame_current
+ previous_action = armature.animation_data.action
+
+ skeleton.anim_start = len(anim_buffer)
+ skeleton.anim_count = 0
+
+ for NLALayer in obj.animation_data.nla_tracks:
+ for NLAStrip in NLALayer.strips:
+ # Use action
+ for a in bpy.data.actions:
+ if a.name == NLAStrip.name:
+ armature.animation_data.action = a
+ break
+
+ anim_start = int(NLAStrip.action_frame_start)
+ anim_end = int(NLAStrip.action_frame_end)
+
+ # export strips
+ anim = mdl_animation()
+ anim.pstr_name = emplace_string( NLAStrip.action.name )
+ anim.rate = 30.0
+ anim.offset = animdata_length
+ anim.length = anim_end-anim_start
+
+ # Export the fucking keyframes
+ for frame in range(anim_start,anim_end):
+ bpy.context.scene.frame_set(frame)
+
+ for bone_name in bones:
+ for pb in armature.pose.bones:
+ if pb.name == bone_name:
+ rb = armature.data.bones[ bone_name ]
+
+ loc, rot, sca = pb.matrix_basis.decompose()
+
+ # local position
+ vp = rb.matrix @ loc
+ final_pos = Vector(( vp[0], vp[2], -vp[1] ))
+
+ # rotation
+ lc_m = pb.matrix_channel.to_3x3()
+ if pb.parent is not None:
+ smtx = pb.parent.matrix_channel.to_3x3()
+ lc_m = smtx.inverted() @ lc_m
+ rq = lc_m.to_quaternion()
+
+ kf = mdl_keyframe()
+ kf.co[0] = final_pos[0]
+ kf.co[1] = final_pos[1]
+ kf.co[2] = final_pos[2]
+
+ kf.q[0] = rq[1]
+ kf.q[1] = rq[3]
+ kf.q[2] = -rq[2]
+ kf.q[3] = rq[0]
+
+ # scale
+ kf.s[0] = sca[0]
+ kf.s[1] = sca[2]
+ kf.s[2] = sca[1]
+
+ animdata_buffer += [kf]
+ animdata_length += sizeof(mdl_keyframe)
+ break
+
+ anim_buffer += [anim]
+ skeleton.anim_count += 1
+
+ s000 = F" [{uid: 3}/{header.node_count-1}]" + " |"*(depth-1)
+ print( F"{s000} | *anim: {NLAStrip.action.name}" )
+
+ bpy.context.scene.frame_set( previous_frame )
+ armature.animation_data.action = previous_action
entdata_buffer += [skeleton]
entdata_length += sizeof( classtype_bone )
bone = classtype_bone()
- bone.use_deform = node_def['deform']
+ bone.deform = node_def['deform']
+
+ if 'target' in node_def:
+ bone.ik_target = armature_def['bones'].index( node_def['target'] )
+ bone.ik_pole = armature_def['bones'].index( node_def['pole'] )
+ else:
+ bone.ik_target = 0
+ bone.ik_pole = 0
+
+ bone.collider = 1 if obj.cv_data.collider else 0
+ if obj.cv_data.collider:
+ bone.hitbox[0][0] = obj.cv_data.v0[0]
+ bone.hitbox[0][1] = obj.cv_data.v0[2]
+ bone.hitbox[0][2] = -obj.cv_data.v1[1]
+ bone.hitbox[1][0] = obj.cv_data.v1[0]
+ bone.hitbox[1][1] = obj.cv_data.v1[2]
+ bone.hitbox[1][2] = -obj.cv_data.v0[1]
+ else:
+ bone.hitbox[0][0] = 0.0
+ bone.hitbox[0][1] = 0.0
+ bone.hitbox[0][2] = 0.0
+ bone.hitbox[1][0] = 0.0
+ bone.hitbox[1][1] = 0.0
+ bone.hitbox[1][2] = 0.0
+
+ bone.deform = node_def['deform']
entdata_buffer += [bone]
elif classtype == 'k_classtype_gate':
# Write data arrays
#
+ header.anim_count = len(anim_buffer)
+
print( "Writing data" )
fpos = sizeof(header)
header.material_offset = fpos
fpos += sizeof(mdl_material)*header.material_count
+ print( F"Animation count: {header.anim_count}" )
+ header.anim_offset = fpos
+ fpos += sizeof(mdl_animation)*header.anim_count
+
print( F"Entdata length: {entdata_length}" )
header.entdata_offset = fpos
fpos += entdata_length
print( F"Indice count: {header.indice_count}" )
header.indice_offset = fpos
fpos += sizeof(c_uint32)*header.indice_count
+
+ print( F"Keyframe count: {animdata_length}" )
+ header.animdata_offset = fpos
+ fpos += animdata_length
print( F"Strings length: {len(strings_buffer)}" )
header.strings_offset = fpos
fp.write( bytearray(sm) )
for mat in material_buffer:
fp.write( bytearray(mat) )
+ for a in anim_buffer:
+ fp.write( bytearray(a) )
for ed in entdata_buffer:
fp.write( bytearray(ed) )
for v in vertex_buffer:
fp.write( bytearray(v) )
for i in indice_buffer:
fp.write( bytearray(i) )
+ for kf in animdata_buffer:
+ fp.write( bytearray(kf) )
+
fp.write( strings_buffer )
fp.close()
colours += [c0,c1]
for obj in bpy.context.collection.objects:
+ if obj.type == 'ARMATURE':
+ for bone in obj.data.bones:
+ if bone.cv_data.collider:
+ c = bone.head_local
+ a = bone.cv_data.v0
+ b = bone.cv_data.v1
+
+ vs = [None]*8
+ vs[0]=obj.matrix_world@Vector((c[0]+a[0],c[1]+a[1],c[2]+a[2]))
+ vs[1]=obj.matrix_world@Vector((c[0]+a[0],c[1]+b[1],c[2]+a[2]))
+ vs[2]=obj.matrix_world@Vector((c[0]+b[0],c[1]+b[1],c[2]+a[2]))
+ vs[3]=obj.matrix_world@Vector((c[0]+b[0],c[1]+a[1],c[2]+a[2]))
+ vs[4]=obj.matrix_world@Vector((c[0]+a[0],c[1]+a[1],c[2]+b[2]))
+ vs[5]=obj.matrix_world@Vector((c[0]+a[0],c[1]+b[1],c[2]+b[2]))
+ vs[6]=obj.matrix_world@Vector((c[0]+b[0],c[1]+b[1],c[2]+b[2]))
+ vs[7]=obj.matrix_world@Vector((c[0]+b[0],c[1]+a[1],c[2]+b[2]))
+
+ indices = [(0,1),(1,2),(2,3),(3,0),(4,5),(5,6),(6,7),(7,4),\
+ (0,4),(1,5),(2,6),(3,7)]
+
+ for l in indices:
+ v0 = vs[l[0]]
+ v1 = vs[l[1]]
+ verts += [(v0[0],v0[1],v0[2])]
+ verts += [(v1[0],v1[1],v1[2])]
+ colours += [(0.5,0.5,0.5,0.5),(0.5,0.5,0.5,0.5)]
+
+ center=obj.matrix_world@c
+
+ def _angle_lim( major, minor, amin, amax, colour ):
+ nonlocal verts, colours
+ f = 0.05
+ ay = major*f
+ ax = minor*f
+
+ for x in range(16):
+ t0 = x/16
+ t1 = (x+1)/16
+ a0 = amin*(1.0-t0)+amax*t0
+ a1 = amin*(1.0-t1)+amax*t1
+
+ p0 = c + major*f*math.cos(a0) + minor*f*math.sin(a0)
+ p1 = c + major*f*math.cos(a1) + minor*f*math.sin(a1)
+
+ p0=obj.matrix_world @ p0
+ p1=obj.matrix_world @ p1
+ verts += [p0,p1]
+ colours += [colour,colour]
+
+ if x == 0:
+ verts += [p0,c]
+ colours += [colour,colour]
+ if x == 15:
+ verts += [p1,c]
+ colours += [colour,colour]
+
+ verts += [c+major*1.2*f,c+major*f*0.8]
+ colours += [colour,colour]
+
+ if bone.cv_data.con0:
+ _angle_lim( Vector((0,1,0)),Vector((0,0,1)), \
+ bone.cv_data.mins[0], bone.cv_data.maxs[0], \
+ (1,0,0,1))
+ _angle_lim( Vector((0,0,1)),Vector((1,0,0)), \
+ bone.cv_data.mins[1], bone.cv_data.maxs[1], \
+ (0,1,0,1))
+ _angle_lim( Vector((1,0,0)),Vector((0,1,0)), \
+ bone.cv_data.mins[2], bone.cv_data.maxs[2], \
+ (0,0,1,1))
+
if obj.cv_data.classtype == 'k_classtype_gate':
if obj.type == 'MESH':
('k_classtype_SKIN',"","",12)
])
+class CV_BONE_SETTINGS(bpy.types.PropertyGroup):
+ collider: bpy.props.BoolProperty(name="Collider",default=False)
+ v0: bpy.props.FloatVectorProperty(name="v0",size=3)
+ v1: bpy.props.FloatVectorProperty(name="v1",size=3)
+
+ mins: bpy.props.FloatVectorProperty(name="mins",size=3)
+ maxs: bpy.props.FloatVectorProperty(name="maxs",size=3)
+
+ con0: bpy.props.BoolProperty(name="Constriant 0",default=False)
+ c0: bpy.props.FloatVectorProperty(name="dir",size=3)
+ s0: bpy.props.FloatVectorProperty(name="limits",size=3)
+
+ con1: bpy.props.BoolProperty(name="Constriant 1",default=False)
+ c1: bpy.props.FloatVectorProperty(name="dir",size=3)
+ s1: bpy.props.FloatVectorProperty(name="limits",size=3)
+
+class CV_BONE_PANEL(bpy.types.Panel):
+ bl_label="Bone Config"
+ bl_idname="SCENE_PT_cv_bone"
+ bl_space_type='PROPERTIES'
+ bl_region_type='WINDOW'
+ bl_context='bone'
+
+ def draw(_,context):
+ active_object = context.active_object
+ if active_object == None: return
+
+ bone = active_object.data.bones.active
+ if bone == None: return
+
+ _.layout.prop( bone.cv_data, "collider" )
+ _.layout.prop( bone.cv_data, "v0" )
+ _.layout.prop( bone.cv_data, "v1" )
+
+ _.layout.label( text="Angle Limits" )
+ _.layout.prop( bone.cv_data, "mins" )
+ _.layout.prop( bone.cv_data, "maxs" )
+
+ _.layout.prop( bone.cv_data, "con0" )
+ _.layout.prop( bone.cv_data, "c0" )
+ _.layout.prop( bone.cv_data, "s0" )
+
+ _.layout.prop( bone.cv_data, "con1" )
+ _.layout.prop( bone.cv_data, "c1" )
+ _.layout.prop( bone.cv_data, "s1" )
+
class CV_SCENE_SETTINGS(bpy.types.PropertyGroup):
use_hidden: bpy.props.BoolProperty( name="use hidden", default=False )
return {'FINISHED'}
classes = [CV_OBJ_SETTINGS,CV_OBJ_PANEL,CV_COMPILE,CV_INTERFACE,\
- CV_MESH_SETTINGS, CV_SCENE_SETTINGS]
+ CV_MESH_SETTINGS, CV_SCENE_SETTINGS, CV_BONE_SETTINGS,\
+ CV_BONE_PANEL]
def register():
global cv_view_draw_handler
bpy.types.Object.cv_data = bpy.props.PointerProperty(type=CV_OBJ_SETTINGS)
bpy.types.Mesh.cv_data = bpy.props.PointerProperty(type=CV_MESH_SETTINGS)
bpy.types.Scene.cv_data = bpy.props.PointerProperty(type=CV_SCENE_SETTINGS)
+ bpy.types.Bone.cv_data = bpy.props.PointerProperty(type=CV_BONE_SETTINGS)
cv_view_draw_handler = bpy.types.SpaceView3D.draw_handler_add(\
cv_draw,(),'WINDOW','POST_VIEW')