Index: trunk/src/formats/nmd_loader.cc
===================================================================
--- trunk/src/formats/nmd_loader.cc	(revision 480)
+++ trunk/src/formats/nmd_loader.cc	(revision 482)
@@ -37,13 +37,16 @@
 {
 	data_channel_set* mesh = data_channel_set_creator::create_set( e.children );
-	load_channel_set( source, mesh, e );
+	data_node_info info;
+	load_channel_set( source, mesh, info, e );
 //	m_mesh_names.push_back( e.name );
+	m_infos.push_back( info );
 	m_meshes.push_back( mesh );
 	return true;
 }
 
-data_channel_set* nv::nmd_loader::release_mesh_data( size_t index )
+data_channel_set* nv::nmd_loader::release_mesh_data( size_t index, data_node_info& info )
 {
 	data_channel_set* result = m_meshes[ index ];
+	info = m_infos[ index ];
 	m_meshes[ index ] = nullptr;
 	return result;
@@ -54,7 +57,9 @@
 	for ( auto mesh : m_meshes ) if ( mesh ) delete mesh;
 	if ( m_node_data ) delete m_node_data;
+	if ( m_bone_data ) delete m_bone_data;
 	m_meshes.clear();
 
-	m_node_data  = nullptr;
+	m_node_data = nullptr;
+	m_bone_data = nullptr;
 }
 
@@ -85,5 +90,6 @@
 	nmd_animation_header animation_header;
 	source.read( &animation_header, sizeof( animation_header ), 1 );
-	m_node_data = new mesh_nodes_data( e.name, animation_header.frame_rate, animation_header.frame_count, animation_header.flat );
+	m_node_data = new mesh_nodes_data( e.name, animation_header.frame_rate, animation_header.frame_count );
+	m_bone_data = new data_node_list( e.name );
 	for ( uint32 i = 0; i < e.children; ++i )
 	{
@@ -93,6 +99,8 @@
 		NV_ASSERT( element_header.type == nmd_type::NODE, "NODE expected!" );
 		data_channel_set* set = data_channel_set_creator::create_set( element_header.children );
-		load_channel_set( source, set, element_header );
-		m_node_data->append( set );
+		data_node_info info;
+		load_channel_set( source, set, info, element_header );
+		m_bone_data->append( info );
+		m_node_data->append( set, info );
 	}
 	m_node_data->initialize();
@@ -110,5 +118,5 @@
 }
 
-bool nv::nmd_loader::load_channel_set( stream& source, data_channel_set* channel_set, const nmd_element_header& e )
+bool nv::nmd_loader::load_channel_set( stream& source, data_channel_set* channel_set, data_node_info& info, const nmd_element_header& e )
 {
 	data_channel_set_creator kaccess( channel_set );
@@ -117,8 +125,7 @@
 		load_channel( source, channel_set );
 	}
-	data_channel_set_creator access( channel_set );
-	access.set_name( e.name );
-	access.set_parent_id( e.parent_id );
-	access.set_transform( e.transform );
+	info.name = e.name;
+	info.parent_id = e.parent_id;
+	info.transform = e.transform;
 	return true;
 }
@@ -126,11 +133,20 @@
 mesh_nodes_data* nv::nmd_loader::release_mesh_nodes_data( size_t )
 {
-	if ( m_node_data )
-	{
-		mesh_nodes_data* result = m_node_data;
-		m_node_data = nullptr;
-		return result;
-	}
-	return nullptr;
+	mesh_nodes_data* result = m_node_data;
+	m_node_data = nullptr;
+	return result;
+}
+
+data_node_list* nv::nmd_loader::release_data_node_list( size_t )
+{
+	data_node_list* result = m_bone_data;
+	m_bone_data = nullptr;
+	return result;
+}
+
+bool nv::nmd_loader::is_animated( size_t /*= 0 */ )
+{
+	if ( !m_node_data ) return false;
+	return m_node_data->is_animated();
 }
 
@@ -150,5 +166,5 @@
 }
 
-void nv::nmd_dump_element( stream& stream_out, const data_channel_set& data, nmd_type type )
+void nv::nmd_dump_element( stream& stream_out, const data_channel_set& data, const data_node_info& info, nmd_type type )
 {
 	uint32 size = 0;
@@ -163,7 +179,7 @@
 	eheader.children   = static_cast<uint16>( data.size() );
 	eheader.size       = size;
-	eheader.name       = data.get_name();
-	eheader.transform  = data.get_transform();
-	eheader.parent_id  = data.get_parent_id();
+	eheader.name       = info.name;
+	eheader.transform  = info.transform;
+	eheader.parent_id  = info.parent_id;
 	eheader.attributes = 0;
 	stream_out.write( &eheader, sizeof( eheader ), 1 );
@@ -205,10 +221,49 @@
 	aheader.frame_rate  = nodes.get_fps();
 	aheader.frame_count = nodes.get_frame_count();
-	aheader.flat = nodes.is_flat();
+	aheader.unused      = false;
 	stream_out.write( &aheader, sizeof( aheader ), 1 );
 
+	for ( uint32 i = 0; i < nodes.size(); ++i )
+	{
+		nmd_dump_element( stream_out, *nodes[i], nodes.get_info(i), nv::nmd_type::NODE );
+	}
+}
+
+void nv::nmd_dump_bones( stream& stream_out, const data_node_list& nodes )
+{
+	uint32 total = sizeof( nmd_animation_header );
 	for ( auto node : nodes )
 	{
-		nmd_dump_element( stream_out, *node, nv::nmd_type::NODE );
+		total += sizeof( nmd_element_header );
+	}
+
+	nmd_element_header header;
+	header.type = nmd_type::ANIMATION;
+	header.children = static_cast<uint16>( nodes.size() );
+	header.size = total;
+	header.name = nodes.get_name();
+	header.transform = mat4();
+	header.parent_id = -1;
+	header.attributes = 0;
+
+	stream_out.write( &header, sizeof( header ), 1 );
+
+	nmd_animation_header aheader;
+	aheader.frame_rate = 0;
+	aheader.frame_count = 0;
+	aheader.unused = false;
+	stream_out.write( &aheader, sizeof( aheader ), 1 );
+
+	for ( auto node : nodes )
+	{
+		nmd_element_header eheader;
+		eheader.type = nv::nmd_type::NODE;
+		eheader.children = 0;
+		eheader.size = 0;
+		eheader.name = node.name;
+		eheader.transform = node.transform;
+		eheader.parent_id = node.parent_id;
+		eheader.attributes = 0;
+		stream_out.write( &eheader, sizeof( eheader ), 1 );
 	}
 }
@@ -227,5 +282,5 @@
 }
 
-void nv::nmd_dump( stream& stream_out, array_view< data_channel_set* > meshes, const mesh_nodes_data* nodes, const string_table* strings /*= nullptr*/, uint64 name /*= 0 */ )
+void nv::nmd_dump( stream& stream_out, array_view< data_channel_set* > meshes, array_view< data_node_info > infos, const mesh_nodes_data* nodes, const string_table* strings /*= nullptr*/, uint64 name /*= 0 */ )
 {
 	uint32 elements = ( strings ? 1 : 0 ) // +1 string array
@@ -237,5 +292,5 @@
 	{
 		NV_ASSERT( meshes[i], "mesh is null!" );
-		nmd_dump_element( stream_out, *meshes[i], nv::nmd_type::MESH );
+		nmd_dump_element( stream_out, *meshes[i], infos[i], nv::nmd_type::MESH );
 	}
 
@@ -243,4 +298,28 @@
 	{
 		nmd_dump_nodes( stream_out, *nodes );
+	}
+
+	if ( strings )
+	{
+		nmd_dump_strings( stream_out, *strings );
+	}
+}
+
+void nv::nmd_dump( stream& stream_out, array_view< data_channel_set* > meshes, array_view< data_node_info > infos, const nv::data_node_list* nodes, const string_table* strings /*= nullptr*/, uint64 name /*= 0 */ )
+{
+	uint32 elements = ( strings ? 1 : 0 ) // +1 string array
+		+ meshes.size() // meshes
+		+ ( nodes && nodes->size() > 0 ? 1 : 0 ); // nodes
+	nmd_dump_header( stream_out, elements, name );
+
+	for ( uint32 i = 0; i < meshes.size(); ++i )
+	{
+		NV_ASSERT( meshes[i], "mesh is null!" );
+		nmd_dump_element( stream_out, *meshes[i], infos[i], nv::nmd_type::MESH );
+	}
+
+	if ( nodes && nodes->size() > 0 )
+	{
+		nmd_dump_bones( stream_out, *nodes );
 	}
 
