|
@@ -3,6 +3,17 @@ from common import *
|
|
|
reload( sys.modules["common"] )
|
|
reload( sys.modules["common"] )
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+#===================================================================================================
|
|
|
|
|
+# mesh_init_t =
|
|
|
|
|
+#===================================================================================================
|
|
|
|
|
+class mesh_init_t:
|
|
|
|
|
+ mesh = NULL
|
|
|
|
|
+ skeleton = NULL
|
|
|
|
|
+ material_filename = "*put material filename*"
|
|
|
|
|
+ write_comments = false
|
|
|
|
|
+ save_path = ""
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
#===================================================================================================
|
|
#===================================================================================================
|
|
|
# GetMesh =
|
|
# GetMesh =
|
|
|
#===================================================================================================
|
|
#===================================================================================================
|
|
@@ -18,11 +29,99 @@ def GetMesh( obj ):
|
|
|
mesh = obj.getData( 0, 1 )
|
|
mesh = obj.getData( 0, 1 )
|
|
|
return mesh
|
|
return mesh
|
|
|
|
|
|
|
|
|
|
+
|
|
|
|
|
+#===================================================================================================
|
|
|
|
|
+# ScriptVWeights =
|
|
|
|
|
+#===================================================================================================
|
|
|
|
|
+def ScriptVWeights( mesh_init ):
|
|
|
|
|
+ mesh = mesh_init.mesh
|
|
|
|
|
+ skeleton = mesh_init.skeleton
|
|
|
|
|
+ b_cmnts = mesh_init.write_comments
|
|
|
|
|
+
|
|
|
|
|
+ #check if mesh is the correct class
|
|
|
|
|
+ if( mesh.__class__.__name__ != "Blender Mesh" ):
|
|
|
|
|
+ ERROR( "The given func param is not a \"Blender Mesh\" class but a \"" + mesh.__class__.__name__ + "\"" )
|
|
|
|
|
+ return "error"
|
|
|
|
|
+
|
|
|
|
|
+ #check if skeleton is the correct class
|
|
|
|
|
+ if( skeleton.__class__.__name__ != "Armature" ):
|
|
|
|
|
+ ERROR( "The given func param is not a \"Armature\" class but a \"" + skeleton.__class__.__name__ + "\"" )
|
|
|
|
|
+ return "error"
|
|
|
|
|
+
|
|
|
|
|
+ bone_names = skeleton.bones.keys()
|
|
|
|
|
+ bone_names.sort()
|
|
|
|
|
+
|
|
|
|
|
+ # init text
|
|
|
|
|
+ ftxt = ""
|
|
|
|
|
+
|
|
|
|
|
+ # link the vert groups to the bone ids
|
|
|
|
|
+ vgroup2bone_id = {} # we give the vgroup name and we get the bone's id in the skeleton
|
|
|
|
|
+ vgroup_names = mesh.getVertGroupNames()
|
|
|
|
|
+
|
|
|
|
|
+ for vgroup_name in vgroup_names:
|
|
|
|
|
+ bone_id = -1
|
|
|
|
|
+ for bone_name in bone_names:
|
|
|
|
|
+ if bone_name == vgroup_name:
|
|
|
|
|
+ bone_id = bone_names.index( bone_name )
|
|
|
|
|
+ break
|
|
|
|
|
+
|
|
|
|
|
+ if bone_id == -1:
|
|
|
|
|
+ WARNING( "Vert group \"" + vgroup_name + "\" cant link to a bone" )
|
|
|
|
|
+
|
|
|
|
|
+ vgroup2bone_id[ vgroup_name ] = bone_id
|
|
|
|
|
+
|
|
|
|
|
+ if( b_cmnts ):ftxt += "/*VERT_WEIGHTS*/ "
|
|
|
|
|
+ ftxt += str( len( mesh.verts ) ) + "\n"
|
|
|
|
|
+
|
|
|
|
|
+ # for every vert do some shit
|
|
|
|
|
+ for vert in mesh.verts:
|
|
|
|
|
+ influences = mesh.getVertexInfluences( vert.index )
|
|
|
|
|
+
|
|
|
|
|
+ influences_num = 0
|
|
|
|
|
+ sumw = 0.0
|
|
|
|
|
+ # calc the influences num and the total weight (NOTE:we may have...
|
|
|
|
|
+ # ...a vert group that doesnt connect to a bone)
|
|
|
|
|
+ for influence in influences:
|
|
|
|
|
+ vgroup = influence[0]
|
|
|
|
|
+ weight = influence[1]
|
|
|
|
|
+
|
|
|
|
|
+ if vgroup2bone_id[ vgroup ] != -1:
|
|
|
|
|
+ influences_num = influences_num + 1
|
|
|
|
|
+ sumw = sumw + weight
|
|
|
|
|
+
|
|
|
|
|
+ # a check
|
|
|
|
|
+ if( influences_num > 4 ):
|
|
|
|
|
+ ERROR( "Cannot have more than 4 bones per vert" );
|
|
|
|
|
+ return "error"
|
|
|
|
|
+
|
|
|
|
|
+ # write to file
|
|
|
|
|
+ if( b_cmnts ): ftxt += "\t/*VERT_ID " + str( vert.index ) + "*/\n"
|
|
|
|
|
+
|
|
|
|
|
+ if( b_cmnts ): ftxt += "\t/*BONE_CONNECTIONS*/ "
|
|
|
|
|
+ ftxt += str( influences_num ) + "\n"
|
|
|
|
|
+
|
|
|
|
|
+ for influence in influences:
|
|
|
|
|
+ vgroup = influence[0]
|
|
|
|
|
+ weight = influence[1]
|
|
|
|
|
+
|
|
|
|
|
+ if vgroup2bone_id[ vgroup ] != -1:
|
|
|
|
|
+ if( b_cmnts ): ftxt += "\t\t/*BONE_ID*/ "
|
|
|
|
|
+ ftxt += str( vgroup2bone_id[ vgroup ] ) + " "
|
|
|
|
|
+ if( b_cmnts ): ftxt += "/*WEIGHT*/ "
|
|
|
|
|
+ ftxt += str( weight/sumw ) + "\n"
|
|
|
|
|
+ # end for all verts
|
|
|
|
|
+
|
|
|
|
|
+ return ftxt
|
|
|
|
|
+
|
|
|
|
|
|
|
|
#===================================================================================================
|
|
#===================================================================================================
|
|
|
# ScriptMesh =
|
|
# ScriptMesh =
|
|
|
#===================================================================================================
|
|
#===================================================================================================
|
|
|
-def ScriptMesh( mesh, mtl_fname, b_cmnts ):
|
|
|
|
|
|
|
+def ScriptMesh( mesh_init ):
|
|
|
|
|
+ mesh = mesh_init.mesh
|
|
|
|
|
+ b_cmnts = mesh_init.write_comments
|
|
|
|
|
+ skeleton = mesh_init.skeleton
|
|
|
|
|
+
|
|
|
#check if mesh is the correct class
|
|
#check if mesh is the correct class
|
|
|
if( mesh.__class__.__name__ != "Blender Mesh" ):
|
|
if( mesh.__class__.__name__ != "Blender Mesh" ):
|
|
|
ERROR( "The given func param is not a \"Blender Mesh\" class but a \"" + mesh.__class__.__name__ + "\"" )
|
|
ERROR( "The given func param is not a \"Blender Mesh\" class but a \"" + mesh.__class__.__name__ + "\"" )
|
|
@@ -43,7 +142,7 @@ def ScriptMesh( mesh, mtl_fname, b_cmnts ):
|
|
|
|
|
|
|
|
# material
|
|
# material
|
|
|
if(b_cmnts): ftxt += "/*MATERIAL*/ "
|
|
if(b_cmnts): ftxt += "/*MATERIAL*/ "
|
|
|
- ftxt += "\"" + mtl_fname + "\"\n"
|
|
|
|
|
|
|
+ ftxt += "\"" + mesh_init.material_filename + "\"\n"
|
|
|
|
|
|
|
|
# the verts
|
|
# the verts
|
|
|
if(b_cmnts): ftxt += "/*VERTS*/ "
|
|
if(b_cmnts): ftxt += "/*VERTS*/ "
|
|
@@ -157,87 +256,15 @@ def ScriptMesh( mesh, mtl_fname, b_cmnts ):
|
|
|
if(b_cmnts): ftxt += "\t/*VERT_ID " + str(i) + " UV_COORDS*/ "
|
|
if(b_cmnts): ftxt += "\t/*VERT_ID " + str(i) + " UV_COORDS*/ "
|
|
|
|
|
|
|
|
ftxt += str( vertuvs[i][0] ) + " " + str( vertuvs[i][1] ) + "\n"
|
|
ftxt += str( vertuvs[i][0] ) + " " + str( vertuvs[i][1] ) + "\n"
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
- return ftxt
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-#===================================================================================================
|
|
|
|
|
-# ScriptVWeights =
|
|
|
|
|
-#===================================================================================================
|
|
|
|
|
-def ScriptVWeights( mesh, skeleton, b_cmnts ):
|
|
|
|
|
- #check if mesh is the correct class
|
|
|
|
|
- if( mesh.__class__.__name__ != "Blender Mesh" ):
|
|
|
|
|
- ERROR( "The given func param is not a \"Blender Mesh\" class but a \"" + mesh.__class__.__name__ + "\"" )
|
|
|
|
|
- return "error"
|
|
|
|
|
-
|
|
|
|
|
- #check if skeleton is the correct class
|
|
|
|
|
- if( skeleton.__class__.__name__ != "Armature" ):
|
|
|
|
|
- ERROR( "The given func param is not a \"Armature\" class but a \"" + skeleton.__class__.__name__ + "\"" )
|
|
|
|
|
- return "error"
|
|
|
|
|
-
|
|
|
|
|
- bone_names = skeleton.bones.keys()
|
|
|
|
|
- bone_names.sort()
|
|
|
|
|
-
|
|
|
|
|
- # init text
|
|
|
|
|
- ftxt = ""
|
|
|
|
|
-
|
|
|
|
|
- # link the vert groups to the bone ids
|
|
|
|
|
- vgroup2bone_id = {} # we give the vgroup name and we get the bone's id in the skeleton
|
|
|
|
|
- vgroup_names = mesh.getVertGroupNames()
|
|
|
|
|
-
|
|
|
|
|
- for vgroup_name in vgroup_names:
|
|
|
|
|
- bone_id = -1
|
|
|
|
|
- for bone_name in bone_names:
|
|
|
|
|
- if bone_name == vgroup_name:
|
|
|
|
|
- bone_id = bone_names.index( bone_name )
|
|
|
|
|
- break
|
|
|
|
|
-
|
|
|
|
|
- if bone_id == -1:
|
|
|
|
|
- WARNING( "Vert group \"" + vgroup_name + "\" cant link to a bone" )
|
|
|
|
|
|
|
|
|
|
- vgroup2bone_id[ vgroup_name ] = bone_id
|
|
|
|
|
-
|
|
|
|
|
- if( b_cmnts ):ftxt += "/*VERT_WEIGHTS*/ "
|
|
|
|
|
- ftxt += str( len( mesh.verts ) ) + "\n"
|
|
|
|
|
-
|
|
|
|
|
- # for every vert do some shit
|
|
|
|
|
- for vert in mesh.verts:
|
|
|
|
|
- influences = mesh.getVertexInfluences( vert.index )
|
|
|
|
|
-
|
|
|
|
|
- influences_num = 0
|
|
|
|
|
- sumw = 0.0
|
|
|
|
|
- # calc the influences num and the total weight (NOTE:we may have...
|
|
|
|
|
- # ...a vert group that doesnt connect to a bone)
|
|
|
|
|
- for influence in influences:
|
|
|
|
|
- vgroup = influence[0]
|
|
|
|
|
- weight = influence[1]
|
|
|
|
|
-
|
|
|
|
|
- if vgroup2bone_id[ vgroup ] != -1:
|
|
|
|
|
- influences_num = influences_num + 1
|
|
|
|
|
- sumw = sumw + weight
|
|
|
|
|
-
|
|
|
|
|
- # a check
|
|
|
|
|
- if( influences_num > 4 ):
|
|
|
|
|
- ERROR( "Cannot have more than 4 bones per vert" );
|
|
|
|
|
- return "error"
|
|
|
|
|
|
|
+ # and now the vertex weights
|
|
|
|
|
+ if skeleton != NULL:
|
|
|
|
|
+ ftxt += ScriptVWeights( mesh_init )
|
|
|
|
|
+ else:
|
|
|
|
|
+ if b_cmnts:
|
|
|
|
|
+ ftxt += "/*VERT_WEIGHTS*/ "
|
|
|
|
|
+ ftxt += "0"
|
|
|
|
|
|
|
|
- # write to file
|
|
|
|
|
- if( b_cmnts ): ftxt += "\t/*VERT_ID " + str( vert.index ) + "*/\n"
|
|
|
|
|
-
|
|
|
|
|
- if( b_cmnts ): ftxt += "\t/*BONE_CONNECTIONS*/ "
|
|
|
|
|
- ftxt += str( influences_num ) + "\n"
|
|
|
|
|
-
|
|
|
|
|
- for influence in influences:
|
|
|
|
|
- vgroup = influence[0]
|
|
|
|
|
- weight = influence[1]
|
|
|
|
|
-
|
|
|
|
|
- if vgroup2bone_id[ vgroup ] != -1:
|
|
|
|
|
- if( b_cmnts ): ftxt += "\t\t/*BONE_ID*/ "
|
|
|
|
|
- ftxt += str( vgroup2bone_id[ vgroup ] ) + " "
|
|
|
|
|
- if( b_cmnts ): ftxt += "/*WEIGHT*/ "
|
|
|
|
|
- ftxt += str( weight/sumw ) + "\n"
|
|
|
|
|
- # end for all verts
|
|
|
|
|
|
|
|
|
|
return ftxt
|
|
return ftxt
|
|
|
|
|
|
|
@@ -245,7 +272,10 @@ def ScriptVWeights( mesh, skeleton, b_cmnts ):
|
|
|
#===================================================================================================
|
|
#===================================================================================================
|
|
|
# ExportMesh =
|
|
# ExportMesh =
|
|
|
#===================================================================================================
|
|
#===================================================================================================
|
|
|
-def ExportMesh( path, mesh, skeleton, mtl_fname, b_cmnts ):
|
|
|
|
|
|
|
+def ExportMesh( mesh_init ):
|
|
|
|
|
+ mesh = mesh_init.mesh
|
|
|
|
|
+ skeleton = mesh_init.skeleton
|
|
|
|
|
+
|
|
|
#check if mesh is the correct class
|
|
#check if mesh is the correct class
|
|
|
if mesh.__class__.__name__ != "Blender Mesh":
|
|
if mesh.__class__.__name__ != "Blender Mesh":
|
|
|
ERROR( "The given func param is not a \"Blender Mesh\" class but a \"" + mesh.__class__.__name__ + "\"" )
|
|
ERROR( "The given func param is not a \"Blender Mesh\" class but a \"" + mesh.__class__.__name__ + "\"" )
|
|
@@ -258,17 +288,8 @@ def ExportMesh( path, mesh, skeleton, mtl_fname, b_cmnts ):
|
|
|
return false
|
|
return false
|
|
|
|
|
|
|
|
INFO( "Trying to export mesh \"" + mesh.name + "\"" )
|
|
INFO( "Trying to export mesh \"" + mesh.name + "\"" )
|
|
|
-
|
|
|
|
|
- filename = path + mesh.name + ".mesh"
|
|
|
|
|
-
|
|
|
|
|
- str = ScriptMesh( mesh, mtl_fname, b_cmnts );
|
|
|
|
|
- if skeleton != NULL:
|
|
|
|
|
- str += ScriptVWeights( mesh, skeleton, b_cmnts )
|
|
|
|
|
- else:
|
|
|
|
|
- if b_cmnts:
|
|
|
|
|
- str += "/*VERT_WEIGHTS*/ "
|
|
|
|
|
- str += "0"
|
|
|
|
|
-
|
|
|
|
|
- WriteFile( filename, str )
|
|
|
|
|
-
|
|
|
|
|
|
|
+ filename = mesh_init.save_path + mesh.name + ".mesh"
|
|
|
|
|
+ WriteFile( filename, ScriptMesh( mesh_init ) )
|
|
|
INFO( "Mesh exported!! \"" + filename + "\"" )
|
|
INFO( "Mesh exported!! \"" + filename + "\"" )
|
|
|
|
|
+
|
|
|
|
|
+
|