#================================
# Blend2XAML v0.1
# Copyright Rakesh Ravuri 2005
# All Rights Reserved
# rakesh@eternalillusions.com
# http://www.eternalillusions.com/blog
#
# IMPORTANT:
# You probably need the FULL python installation (2.3) to run this script.
# If you have problems, get the python installation from
# http://www.python.org
#
# PURPOSE:
# To allow rudimentary XAML export capabilities. Currently the goal is to
# support just the geometry,Solid Materials & lights.
# Texture & Animation will be in the next version.
#
# USAGE:
# Select one or more objects and execute this script (ALT+P)
# Be Sure to select a Camera & Light :)
# Everything that CAN be exported, will be written to the export file.
# You can change the export filename by entering the path and filename
# in the dialog. If you do not enter a path, the export file will
# appear in your blender directory.
#
# LIMITATIONS:
# Your mesh "M U S T" be triangulated before you execute this script.
# If you don't there will be holes in your mesh.
# Only exports triangular meshes,spot/point light,materials & camera currently.
#
# Texture coordinates & Normals are NOT be exported.
#
# NOTES:
# If you would like to contribute, or make modifications, send your mods
# to me and I will incorporate them into the next release for everyone to
# enjoy.
# Please don't distribute modified copies of this file without contacting me
# first!
#
# HISTORY:
# Aug 09,05: Blend2XAML created ( based on the 3DS exporter)
#
#================================
# ===============================
# Setup our runtime constants
# ===============================
DEBUG=1 # Set this to "1" to see extra messages
# ===============================
# Import our libraries
# ===============================
import string
import struct
import Blender
import locale
import math
from Blender import NMesh,Camera,Scene
from Blender.Draw import *
from Blender.BGL import *
from locale import format
# ===============================
# Input Variables
# ===============================
resourceData=""
gFilename=Create("c:\\export.xaml")
uselights=1
exportSelected=1
ambientR=Create(0.6)
ambientG=Create(0.6)
ambientB=Create(0.6)
EVENT_PATHCHANGE= 1
EVENT_IMPORT= 2
EVENT_EXPORT= 3
EVENT_QUIT= 4
EVENT_EXPORTLIGHTS= 5
EVENT_AR= 6
EVENT_AG= 7
EVENT_AB= 8
EVENT_EXPORTSELECTED= 9
#================================
def RRBuildXAML ():
#================================
#
#================================
data=""
sdata=""
sdata=sdata+RRBuildScene()
data=data+'\n'
data=data+''
data=data+RRBuildResources()
data=data+''
data=data+sdata
data=data+'\n'
return data
def RRBuildResources():
global resourceData
tmlist=[]
tmname=""
allmats=Blender.Material.Get()
for m in allmats:
tmname="XM_"+m.name
if tmlist.__contains__(tmname):
if DEBUG: print('Duplicate material \n')
else:
tmlist.append(tmname)
resourceData=resourceData+'\n'
resourceData=resourceData+'\n\n'
resourceData=resourceData+''
resourceData=resourceData+'\n\n'
resourceData=resourceData+'\n\n'
resourceData=resourceData+''
resourceData=resourceData+'\n\n'
resourceData=resourceData+'\n'
data="\n"+resourceData
return data
#================================
def RRBuildScene():
#================================
data=""
data=data+'\n'
data=data+RRBuildSceneGraph()
data=data+'\n'
return data
#================================
def RRBuildCamera(cameraObj):
#================================
camera=cameraObj.data
data=""
if camera.type==0:
data=data+'\n"
data=data+""
return data
def RRBuildLight(lObj):
lamp=lObj.data
data=""
m=lObj.getMatrix()
sf=1
print str(lamp.type)
print m
if lamp.type==0:
data=data+'\n'
return data
#================================
def RRBuildSceneGraph():
#================================
global exportSelected
global uselights
cdata=""
data=""
data=data+"\n\n"
data=data+"\n"
index=0
if exportSelected:
objList=Blender.Object.GetSelected()
else:
objList=Blender.Object.Get()
for curOb in objList:
if DEBUG:
print '\n'
print 'exporting ....'
print type(curOb.data)
if type(curOb.data)==Blender.Types.CameraType:
cdata=RRBuildCamera(curOb)
elif type(curOb.data)==Blender.Types.LampType:
if (uselights==1):
data=data+RRBuildLight(curOb)
else:
data=data+RRBuildMesh ("BdObj"+str(index),curOb)
index=index+1
data=data+''
data=data+"\n\n\n"
return cdata+data
#================================
def RRBuildVertexList(theMesh,theLoc):
#================================
data=""
fdata=""+' TriangleIndices="'
ndata=""+' Normals="'
data=data+' Positions="'
for v in theMesh.verts:
data=data+str(v.co[0])+','+str(v.co[1])+','+str(v.co[2])+' '
for f in theMesh.faces:
ndata=ndata+str(f.no[0])+','+str(f.no[1])+','+str(f.no[2])+' '
fdata=fdata+str(f.v[0].index)+','+str(f.v[1].index)+','+str(f.v[2].index)+' '
fdata=fdata+'"'
data=data+'" '
ndata=ndata+'" '
return data+fdata #+ndata
#================================
def RRBuildMesh(name,currentObject):
#================================
# Determines what type of object we're dealing
# with and export is accordingly.
# If we can't convert the object,
# ignore it completely
#===============================
data=""
# --- Check for NMesh
if type(currentObject.data)==Blender.Types.NMeshType:
data=data+""
data=data+RRBuildTriMesh (currentObject)
# --- Cannot convert
else:
if DEBUG: print ("Found non-convertible object: Discarding")
return data
#================================
def RRBuildTriMesh(theObject):
#================================
# Does an emergency check to make sure
# we've been passed a mesh.
#================================
global resourceData
theMesh=theObject.data
data=""
# Make sure that this is a mesh,
# if not, bail out with a null return value
if type(theMesh)!=Blender.Types.NMeshType:
if DEBUG: print ("Found a non-mesh (Discarding)")
return ""
if DEBUG:
print ("Converting mesh with "+str(len(theMesh.verts))+" verts and "+str(len(theMesh.faces))+" faces")
m=theObject.getMatrix()
resourceData=resourceData+'\n'
resourceData=resourceData+"\n"
resourceData=resourceData+''
resourceData=resourceData+"\n"
data=data+'0:
m=theMesh.materials[0]
data=data+' Material="{StaticResource XM_'+m.name+'}" '
data=data+" >\n\n\n"
data=data+'\n\n'
return data
#data=data+RVBuildTriMappingCoors(theMesh)
#data=data+RVBuildFaceList1(theMesh)
#data=data+RVBuildTriLocal(theObject)
#================================
def ExitGUI ():
#================================
Exit()
#================================
def EventGUI (event):
#================================
global gFilename
global uselights
global exportSelected
if (event==EVENT_EXPORT):
RRExport(gFilename.val)
if (event==EVENT_QUIT):
ExitGUI()
if (event==EVENT_EXPORTLIGHTS):
if (uselights==1):
uselights=0
else:
uselights=1
if (event==EVENT_EXPORTSELECTED):
if (exportSelected==1):
exportSelected=0
else:
exportSelected=1
Redraw(1)
#================================
def DrawGUI():
#================================
glClearColor (0.0,0.0,0.0,0)
glClear (GL_COLOR_BUFFER_BIT)
global gFilename
global gAlert
global uselights
global ambientR
global ambientG
global ambientB
w=Blender.World.GetCurrent()
#AllWorlds=Blender.World.Get()
#if (len(AllWorlds)>0):
# w=AllWorlds[0]
gFilename=String ("Filename: ",EVENT_PATHCHANGE,32,96,320,32,gFilename.val,255,"Full pathname and filename")
Button ("Export",EVENT_EXPORT,32,48,100,32)
Button ("Quit",EVENT_QUIT,252,48,100,32)
ambientR=Number("R",EVENT_AR,32,225,100,22,ambientR.val,0.0,1.0)
ambientG=Number("G",EVENT_AG,32,200,100,22,ambientG.val,0.0,1.0)
ambientB=Number("B",EVENT_AB,32,175,100,22,ambientB.val,0.0,1.0)
Toggle("Export Lights",EVENT_EXPORTLIGHTS,32,144,100,16,uselights,"Use this toggle button to export the selected lights")
Toggle("Export Selected Only",EVENT_EXPORTSELECTED,142,144,100,16,exportSelected,"Use this toggle button to export only the selected objects")
#================================
def RegisterGUI ():
#================================
global ambientR
global ambientG
global ambientB
w=Blender.World.GetCurrent()
ambientR.val=w.amb[0]
ambientG.val=w.amb[1]
ambientB.val=w.amb[2]
Register (DrawGUI,None,EventGUI)
#================================
# MAIN SCRIPT
#================================
# Opens a file, writes data in it
# and closes it up.
#================================
RegisterGUI()
#================================
def RRExport (exportName):
#================================
if DEBUG:print ("Starting...")
theFile=open (exportName,"w")
theFile.write (RRBuildXAML())
theFile.flush()
theFile.close()
if DEBUG:print ("Finished...")