[Bf-extensions-cvs] SVN commit: /data/svn/bf-extensions [766] trunk/py/scripts/addons/ export_directx_x.py: Added port of direct x exporter to trunk.

Brendon Murphy meta.androcto1 at gmail.com
Fri Jul 9 13:11:35 CEST 2010


Revision: 766
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-extensions&revision=766
Author:   meta-androcto
Date:     2010-07-09 13:11:35 +0200 (Fri, 09 Jul 2010)

Log Message:
-----------
Added port of direct x exporter to trunk.
Thanks to Chris Foster for this well written script.

Added Paths:
-----------
    trunk/py/scripts/addons/export_directx_x.py

Added: trunk/py/scripts/addons/export_directx_x.py
===================================================================
--- trunk/py/scripts/addons/export_directx_x.py	                        (rev 0)
+++ trunk/py/scripts/addons/export_directx_x.py	2010-07-09 11:11:35 UTC (rev 766)
@@ -0,0 +1,870 @@
+ #  ***** GPL LICENSE BLOCK *****
+ #
+ #  This program is free software: you can redistribute it and/or modify
+ #  it under the terms of the GNU General Public License as published by
+ #  the Free Software Foundation, either version 3 of the License, or
+ #  (at your option) any later version.
+ #
+ #  This program is distributed in the hope that it will be useful,
+ #  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ #  GNU General Public License for more details.
+ #
+ #  You should have received a copy of the GNU General Public License
+ #  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ #  All rights reserved.
+ #  ***** GPL LICENSE BLOCK *****
+
+#One line description for early versions of Blender 2.52.
+"Export: DirectX Model Format (.x)"
+
+bl_addon_info = {
+    'name': 'Export: DirectX Model Format (.x)',
+    'author': 'Chris Foster (Kira Vakaan)',
+    'version': '1.1',
+    'blender': (2, 5, 3),
+    'location': 'location":"File > Export',
+    'description': 'Export to the DirectX Model Format',
+    'wiki_url': 'http://wiki.blender.org/index.php/Extensions:2.5/Py/' \
+        'Scripts/File_I-O/DirectX_Exporter',
+    'tracker_url': 'https://projects.blender.org/tracker/index.php?'\
+        'func=detail&aid=22795&group_id=153&atid=467',
+    'category': 'Import/Export'}
+"""
+Name: 'DirectX Exporter'
+Blender: 252
+Group: 'Export'
+Tooltip: 'Exports to the DirectX model file format (.x)'
+"""
+
+__author__="Chris Foster (Kira Vakaan)"
+__url__="www.tobedecided.com"
+__version__="1.1"
+__bpydoc__="""\
+"""
+
+import bpy
+from math import radians
+from mathutils import *
+import os
+
+
+#Container for the exporter settings
+class DirectXExporterSettings:
+    def __init__(self,
+                 context,
+                 FilePath,
+                 CoordinateSystem=1,
+                 RotateX=True,
+                 FlipNormals=False,
+                 ApplyModifiers=False,
+                 IncludeFrameRate=False,
+                 ExportTextures=True,
+                 ExportArmatures=False,
+                 ExportAnimation=0,
+                 ExportMode=1,
+                 Verbose=False):
+        self.context=context
+        self.FilePath=FilePath
+        self.CoordinateSystem=int(CoordinateSystem)
+        self.RotateX=RotateX
+        self.FlipNormals=FlipNormals
+        self.ApplyModifiers=ApplyModifiers
+        self.IncludeFrameRate=IncludeFrameRate
+        self.ExportTextures=ExportTextures
+        self.ExportArmatures=ExportArmatures
+        self.ExportAnimation=int(ExportAnimation)
+        self.ExportMode=int(ExportMode)
+        self.Verbose=Verbose
+
+def LegalName(Name):
+    NewName=Name.replace(".","_")
+    NewName=NewName.replace(" ","_")
+    if NewName[0].isdigit() or NewName in ["ARRAY","DWORD","UCHAR","BINARY","FLOAT","ULONGLONG","BINARY_RESOURCE","SDWORD","UNICODE","CHAR","STRING","WORD","CSTRING","SWORD","DOUBLE","TEMPLATE"]:
+        NewName="_"+NewName
+    return NewName
+
+def ExportDirectX(Config):
+    print("----------\nExporting to {}".format(Config.FilePath))
+    if Config.Verbose: print("Opening File...",end=" ")
+    Config.File=open(Config.FilePath,"w")
+    if Config.Verbose: print("Done")
+    
+    if Config.Verbose: print("Generating Object list for export...",end=" ")
+    if Config.ExportMode==1:
+        Config.ExportList=[Object for Object in Config.context.scene.objects if Object.type in ("ARMATURE","EMPTY","MESH") and Object.parent==None]
+    else:
+        ExportList=[Object for Object in Config.context.selected_objects if Object.type in ("ARMATURE","EMPTY","MESH")]
+        Config.ExportList=[Object for Object in ExportList if Object.parent not in ExportList]
+    if Config.Verbose: print("Done")
+    
+    if Config.Verbose: print("Setting up...",end=" ")
+    Config.SystemMatrix=Matrix()
+    if Config.RotateX:
+        Config.SystemMatrix*=RotationMatrix(radians(-90),4,"X")
+    if Config.CoordinateSystem==1:
+        Config.SystemMatrix*=ScaleMatrix(-1,4,Vector((0,1,0)))
+    Config.InverseSystemMatrix=Config.SystemMatrix.copy().invert()
+    
+    if Config.ExportAnimation:
+        CurrentFrame=bpy.context.scene.frame_current
+        bpy.context.scene.frame_current=bpy.context.scene.frame_current
+    if Config.Verbose: print("Done")
+    
+    if Config.Verbose: print("Writing Header...",end=" ")
+    WriteHeader(Config)
+    if Config.Verbose: print("Done")
+    
+    Config.Whitespace=0
+    Config.ObjectList=[]
+    if Config.Verbose: print("Writing Objects...")
+    WriteObjects(Config,Config.ExportList)
+    if Config.Verbose: print("Done")
+    
+    if Config.ExportAnimation:
+        if Config.IncludeFrameRate:
+            if Config.Verbose: print("Writing Frame Rate...",end=" ")
+            Config.File.write("{}AnimTicksPerSecond {{\n".format("  "*Config.Whitespace))
+            Config.Whitespace+=1
+            Config.File.write("{}{};\n".format("  "*Config.Whitespace,int(bpy.context.scene.render.fps/bpy.context.scene.render.fps_base)))
+            Config.Whitespace-=1
+            Config.File.write("{}}}\n".format("  "*Config.Whitespace))
+            if Config.Verbose: print("Done")
+        if Config.Verbose: print("Writing Animation...")
+        WriteAnimationSet(Config)
+        bpy.context.scene.frame_current=CurrentFrame
+        if Config.Verbose: print("Done")
+    
+    CloseFile(Config)
+    print("Finished")
+
+def GetObjectChildren(Parent):
+    return [Object for Object in Parent.children if Object.type in ("ARMATURE","EMPTY","MESH")]
+
+def GetMeshVertexCount(Mesh):
+    VertexCount=0
+    for Face in Mesh.faces:
+        VertexCount+=len(Face.verts)
+    return VertexCount
+
+def GetMaterialTexture(Material):
+    if Material:
+        ImageTextures=[Material.texture_slots[TextureSlot].texture for TextureSlot in Material.texture_slots.keys() if Material.texture_slots[TextureSlot].texture.type=="IMAGE"]
+        ImageFiles=[os.path.basename(Texture.image.filename) for Texture in ImageTextures if Texture.image.source=="FILE"]
+        if ImageFiles:
+            return ImageFiles[0]
+    return None
+
+def WriteHeader(Config):
+    Config.File.write("xof 0303txt 0032\n\n")
+    if Config.ExportArmatures:
+        Config.File.write("template XSkinMeshHeader {\n\
+  <3cf169ce-ff7c-44ab-93c0-f78f62d172e2>\n\
+  WORD nMaxSkinWeightsPerVertex;\n\
+  WORD nMaxSkinWeightsPerFace;\n\
+  WORD nBones;\n\
+}\n\n\
+template SkinWeights {\n\
+  <6f0d123b-bad2-4167-a0d0-80224f25fabb>\n\
+  STRING transformNodeName;\n\
+  DWORD nWeights;\n\
+  array DWORD vertexIndices[nWeights];\n\
+  array float weights[nWeights];\n\
+  Matrix4x4 matrixOffset;\n\
+}\n\n")
+
+def WriteObjects(Config,ObjectList):
+    Config.ObjectList+=ObjectList
+    
+    for Object in ObjectList:
+        if Config.Verbose: print("  Writing Object: {}...".format(Object.name))
+        Config.File.write("{}Frame {} {{\n".format("  "*Config.Whitespace,LegalName(Object.name)))
+        
+        Config.Whitespace+=1
+        if Config.Verbose: print("    Writing Local Matrix...",end=" ")
+        WriteLocalMatrix(Config,Object)
+        if Config.Verbose: print("Done")
+        
+        if Config.ExportArmatures and Object.type=="ARMATURE":
+            Armature=Object.data
+            ParentList=[Bone for Bone in Armature.bones if Bone.parent==None]
+            if Config.Verbose: print("    Writing Armature Bones...")
+            WriteArmatureBones(Config,Object,ParentList)
+            if Config.Verbose: print("    Done")
+        
+        ChildList=GetObjectChildren(Object)
+        if Config.Verbose: print("    Writing Children...")
+        WriteObjects(Config,ChildList)
+        if Config.Verbose: print("    Done Writing Children")
+        
+        if Object.type=="MESH":
+            if Config.Verbose: print("    Generating Mesh...",end=" ")
+            Mesh=Object.create_mesh(bpy.context.scene,(Config.ApplyModifiers|Config.ExportArmatures),"PREVIEW")
+            if Config.Verbose: print("Done")
+            if Config.Verbose: print("    Writing Mesh...")
+            WriteMesh(Config,Object,Mesh)
+            if Config.Verbose: print("    Done")
+            bpy.data.meshes.remove(Mesh)
+        
+        Config.Whitespace-=1
+        Config.File.write("{}}} //End of {}\n".format("  "*Config.Whitespace,LegalName(Object.name)))
+        if Config.Verbose: print("  Done Writing Object: {}".format(Object.name))
+
+def WriteLocalMatrix(Config,Object):
+    if Object.parent:
+        LocalMatrix=Object.parent.matrix_world.copy().invert()
+    else:
+        LocalMatrix=Matrix()
+    LocalMatrix*=Object.matrix_world
+    LocalMatrix=Config.SystemMatrix*LocalMatrix*Config.InverseSystemMatrix
+    
+    Config.File.write("{}FrameTransformMatrix {{\n".format("  "*Config.Whitespace))
+    Config.Whitespace+=1
+    Config.File.write("{}{:9f},{:9f},{:9f},{:9f},\n".format("  "*Config.Whitespace,LocalMatrix[0][0],LocalMatrix[0][1],LocalMatrix[0][2],LocalMatrix[0][3]))
+    Config.File.write("{}{:9f},{:9f},{:9f},{:9f},\n".format("  "*Config.Whitespace,LocalMatrix[1][0],LocalMatrix[1][1],LocalMatrix[1][2],LocalMatrix[1][3]))
+    Config.File.write("{}{:9f},{:9f},{:9f},{:9f},\n".format("  "*Config.Whitespace,LocalMatrix[2][0],LocalMatrix[2][1],LocalMatrix[2][2],LocalMatrix[2][3]))
+    Config.File.write("{}{:9f},{:9f},{:9f},{:9f};;\n".format("  "*Config.Whitespace,LocalMatrix[3][0],LocalMatrix[3][1],LocalMatrix[3][2],LocalMatrix[3][3]))
+    Config.Whitespace-=1
+    Config.File.write("{}}}\n".format("  "*Config.Whitespace))
+
+def WriteArmatureBones(Config,Object,ChildList):
+    PoseBones=Object.pose.bones
+    for Bone in ChildList:
+        if Config.Verbose: print("      Writing Bone: {}...".format(Bone.name),end=" ")
+        Config.File.write("{}Frame {} {{\n".format("  "*Config.Whitespace,LegalName(Object.name)+"_"+LegalName(Bone.name)))
+        Config.Whitespace+=1
+        
+        PoseBone=PoseBones[Bone.name]
+        
+        if Bone.parent:

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-extensions-cvs mailing list