forked from hypodyne/Xaml-Exporter-for-Blender
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio_export_xaml.py
174 lines (135 loc) · 5.31 KB
/
io_export_xaml.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
##########################################
# Copyright AI Robot Media 2017
##########################################
bl_info = {
"name": "XAML format (.xaml)",
"author": "AI Robot Media",
"version": (1, 0, 0),
"blender": (2, 71, 0),
"location": "File > Export > XAML",
"description": "Export scene to XAML",
"warning": "",
"wiki_url": "http://airobotmedia.com",
"category": "Import-Export"}
import bpy
import bmesh
from mathutils import Matrix
from io import StringIO
from math import *
class XamlWriter :
def __init__(self):
self.outfile = StringIO()
def toString(self):
return self.outfile.getvalue()
def writeString(self, str):
self.outfile.write(str)
return
def writeLine(self, str):
self.outfile.write(str + "\n")
return
def compactFloat(number):
str = "%.6f" % number
if len(str) == 0 : return str
backStr = str[-5:]
frontStr = str[:-5]
str = frontStr + backStr.rstrip("0")
return str
##########################################
# Xaml Exporter
##########################################
class XamlExporter :
def __init__(self):
self.writer = XamlWriter()
def writeCamera(self, cameraObj):
self.writer.writeString("Camera")
def writeLamp(self, lampObj):
self.writer.writeString("Lamp")
def writeMesh(self, meshObj):
#transform into xaml coordinate system (Y-UP)
matrix = Matrix.Rotation(radians(-90), 4, 'X')
# get mesh data from obj
mesh = meshObj.data
# copy mesh so it does not transform original
mesh = mesh.copy()
#apply tranform matrix to whole mesh
mesh.transform(matrix)
# Vertices are correct
self.writer.writeString("Positions=" + '"')
for vertice in mesh.vertices:
self.writer.writeString("%f,%f,%f " % (compactFloat(round(vertice.co.x, 6)), compactFloat(round(vertice.co.y, 6)), compactFloat(round(vertice.co.z, 6))))
self.writer.writeLine('"')
# Triangle Indices
self.writer.writeString("TriangleIndices=" + '"')
for polygon in mesh.polygons:
if len(polygon.vertices) == 3:
self.writer.writeString("%d,%d,%d " % (polygon.vertices[0], polygon.vertices[1], polygon.vertices[2],))
elif len(polygon.vertices) == 4:
self.writer.writeString("%d,%d,%d " % (polygon.vertices[0], polygon.vertices[1], polygon.vertices[2],))
self.writer.writeString("%d,%d,%d " % (polygon.vertices[0], polygon.vertices[2], polygon.vertices[3],))
self.writer.writeLine('"')
# Normals
self.writer.writeString("Normals=" + '"')
for polygon in mesh.polygons:
if polygon.use_smooth:
self.writer.writeString("n use smooth (lookup)")
else:
if len(polygon.vertices) == 3:
self.writer.writeString("%d,%d,%d " % (polygon.normal[0], polygon.normal[1], polygon.normal[2],))
elif len(polygon.vertices) == 4:
self.writer.writeString("%d,%d,%d " % (polygon.normal[0], polygon.normal[1], polygon.normal[2],))
self.writer.writeString("%d,%d,%d " % (polygon.normal[0], polygon.normal[1], polygon.normal[2],))
self.writer.writeLine('"')
#self.writer.writeString("\n")
#for loop in mesh.loops:
# self.writer.writeString("l %f" % (loop.vertex_index))
def writeScene(self, file):
for item in bpy.context.scene.objects:
if item.type == 'MESH':
self.writeMesh(item)
elif item.type == 'LAMP':
self.writeLamp(item)
elif item.type == 'CAMERA':
self.writeCamera(item)
file.write(self.writer.toString())
##########################################
# Main
##########################################
def export(filename, context):
exporter = XamlExporter()
file = open(filename, 'w')
try:
exporter.writeScene(file)
finally:
file.flush()
file.close()
return True
##########################################
# The Export Xaml Class
##########################################
from bpy_extras.io_utils import ExportHelper
from bpy.props import StringProperty, BoolProperty
class ExportXaml(bpy.types.Operator, ExportHelper):
"""Export scene to XAML"""
bl_idname = "export.xaml"
bl_label = "Export to XAML"
filename_ext = ".xaml"
#Draw
#Poll
def execute(self, context):
filepath = self.filepath
filepath = bpy.path.ensure_ext(filepath, self.filename_ext)
exported = export(filepath, context)
return {'FINISHED'}
##########################################
# REGISTER
##########################################
def menu_func(self, context):
self.layout.operator(ExportXaml.bl_idname, text="XAML (.xaml)")
def register():
bpy.utils.register_class(ExportXaml)
bpy.types.INFO_MT_file_export.append(menu_func)
def unregister():
bpy.utils.unregister_class(ExportXaml)
bpy.types.INFO_MT_file_export.remove(menu_func)
if __name__ == "__main__":
register()