1 |
#!BPY
|
2 |
|
3 |
"""
|
4 |
Name: 'Textport'
|
5 |
Blender: 234
|
6 |
Group: 'Export'
|
7 |
Tooltip: 'Textport exporter for libgender'
|
8 |
"""
|
9 |
|
10 |
# Blender Text Export Module
|
11 |
# Version 0.0
|
12 |
# Colin Peart
|
13 |
# Released under GPL (insert license text here)
|
14 |
|
15 |
import Blender
|
16 |
import sys
|
17 |
|
18 |
|
19 |
# dump -- the object dumper f: File, o: object
|
20 |
def dump(f, o):
|
21 |
if o.block_type == 'NMesh' :
|
22 |
mesh = Blender.NMesh.GetRaw(o.name)
|
23 |
# header
|
24 |
f.write("M " + o.name + "\n")
|
25 |
|
26 |
# Vertex Coordinate header
|
27 |
f.write("V " + `len(mesh.verts)` + "\n")
|
28 |
|
29 |
# Verticies
|
30 |
for vert in mesh.verts:
|
31 |
f.write(`vert.co[0]` + " " + `vert.co[1]` + " " + `vert.co[2]` + "\n")
|
32 |
|
33 |
# Vertex Normal Header
|
34 |
f.write("N " + `len(mesh.verts)` + "\n")
|
35 |
# Vertex Normals
|
36 |
for vert in mesh.verts:
|
37 |
f.write(`vert.no[0]` + " " + `vert.no[1]` + " " + `vert.no[2]` + "\n")
|
38 |
|
39 |
# uv coordinates
|
40 |
if mesh.hasVertexUV:
|
41 |
# UV header
|
42 |
f.write("U " + `len(mesh.verts)` + "\n")
|
43 |
for vert in mesh.verts:
|
44 |
f.write(`vert.uvco[0]` + " " + `vert.uvco[1]` + "\n")
|
45 |
|
46 |
|
47 |
# Faces: Take two passes: First, count the total number of faces,
|
48 |
# second: output them. Tessalate quads to triangles. Discard edges (which
|
49 |
# have only two vertices)
|
50 |
# Modified to use only one pass, by building a variable with the text
|
51 |
faces = len(mesh.faces)
|
52 |
data = ""
|
53 |
colordata = ""
|
54 |
for face in mesh.faces:
|
55 |
if face.v[2] < 0:
|
56 |
# discard
|
57 |
faces = faces - 1
|
58 |
elif face.v[2] < 0:
|
59 |
# Already a triangle, add it to the data, do not change the count
|
60 |
data = data + `face.v[0].index` + " " + `face.v[1].index` + " " + `face.v[2].index` + "\n"
|
61 |
else:
|
62 |
# this one is a quad
|
63 |
# Break it up into two triangles
|
64 |
# Hence one additional face
|
65 |
faces = faces + 1
|
66 |
data = data + `face.v[0].index` + " " + `face.v[1].index` + " " + `face.v[3].index` + "\n"
|
67 |
data = data + `face.v[1].index` + " " + `face.v[2].index` + " " + `face.v[3].index` + "\n"
|
68 |
# Now I can write the header with the correct face count, and then the data
|
69 |
f.write("A " + `faces` + "\n")
|
70 |
f.write(data)
|
71 |
|
72 |
|
73 |
|
74 |
#Stage 1:
|
75 |
# Gather information -- which objects, which frames, what type of export
|
76 |
|
77 |
# Objects to export: Use the set of selected objects
|
78 |
objectsToExport = Blender.Object.GetSelected()
|
79 |
|
80 |
# Which frames:
|
81 |
startFrame = Blender.Get('staframe')
|
82 |
endFrame = Blender.Get('endframe')
|
83 |
frameset = range(startFrame, endFrame + 1)
|
84 |
|
85 |
|
86 |
# Create a file for output
|
87 |
filename = Blender.Get('filename') + '.blasc'
|
88 |
print('Exporting ' + filename + ' frames:')
|
89 |
print(frameset)
|
90 |
|
91 |
fout = open(filename, 'w')
|
92 |
|
93 |
# Go through the objects and export them
|
94 |
|
95 |
# output file header
|
96 |
|
97 |
#for ob in objectsToExport:
|
98 |
ob = objectsToExport[0]
|
99 |
# output object header
|
100 |
print('Exporting Object ' + ob.name)
|
101 |
fout.write("O " + ob.name + "\n")
|
102 |
#for fr in frameset:
|
103 |
# Blender.Set('curframe', fr)
|
104 |
#print('Exporting Frame ' + `fr`)
|
105 |
|
106 |
# output frame header
|
107 |
#fout.write("F " + `fr` + "\n")
|
108 |
|
109 |
# Write out common items
|
110 |
#fout.write("Y " + `ob.data.block_type` + "\n")
|
111 |
#fout.write("L " + `ob.LocX + ob.dLocX` + " " + `ob.LocY + ob.dLocY` + " " + `ob.LocZ + ob.dLocZ` + "\n")
|
112 |
#fout.write("R " + `ob.RotX + ob.dRotX` + " " + `ob.RotY + ob.dRotY` + " " + `ob.RotZ + ob.dRotZ` + "\n")
|
113 |
#fout.write("S " + `ob.SizeX + ob.dSizeX` + " " + `ob.SizeY + ob.dSizeY` + " " + `ob.SizeZ + ob.dSizeZ` + "\n")
|
114 |
|
115 |
# Write out the data
|
116 |
dump(fout, ob.data)
|
117 |
|
118 |
# output Frame footer
|
119 |
#fout.write("/F\n")
|
120 |
#Output object footer
|
121 |
|
122 |
# Close the file
|
123 |
fout.close();
|
124 |
|
125 |
print('Done!')
|