1 |
root |
1.1 |
#!BPY
|
2 |
|
|
|
3 |
|
|
"""
|
4 |
root |
1.2 |
Name: 'Textport save'
|
5 |
root |
1.1 |
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 |
|
|
f.write("M " + o.name + "\n")
|
24 |
root |
1.2 |
f.write("T Material\n");
|
25 |
|
|
|
26 |
|
|
if len (mesh.materials) > 0:
|
27 |
|
|
material = mesh.materials[0]
|
28 |
|
|
c0 = material.rgbCol[0]
|
29 |
|
|
c1 = material.rgbCol[1]
|
30 |
|
|
c2 = material.rgbCol[2]
|
31 |
|
|
f.write(`c0` + " " + `c1` + " " + `c2` + " " + `material.alpha` + "\n");
|
32 |
|
|
s0 = material.specCol[0]
|
33 |
|
|
s1 = material.specCol[1]
|
34 |
|
|
s2 = material.specCol[2]
|
35 |
|
|
f.write(`s0` + " " + `s1` + " " + `s2` + " " + `material.specTransp` + "\n");
|
36 |
|
|
s0 *= material.emit
|
37 |
|
|
s1 *= material.emit
|
38 |
|
|
s2 *= material.emit
|
39 |
|
|
f.write(`s0` + " " + `s1` + " " + `s2` + " 1\n");
|
40 |
root |
1.3 |
f.write(`material.spec` + "\n");
|
41 |
root |
1.2 |
else:
|
42 |
|
|
f.write("1 1 1 1\n");
|
43 |
|
|
f.write("0 0 0 1\n");
|
44 |
|
|
f.write("0 0 0 1\n");
|
45 |
root |
1.3 |
f.write("0\n");
|
46 |
root |
1.2 |
|
47 |
root |
1.1 |
|
48 |
|
|
# Vertex Coordinate header
|
49 |
|
|
f.write("V " + `len(mesh.verts)` + "\n")
|
50 |
|
|
|
51 |
|
|
# Verticies
|
52 |
|
|
for vert in mesh.verts:
|
53 |
root |
1.3 |
f.write(`vert.co[0]` + " " + `vert.co[2]` + " " + `vert.co[1]` + "\n")
|
54 |
root |
1.1 |
|
55 |
|
|
# Vertex Normal Header
|
56 |
|
|
f.write("N " + `len(mesh.verts)` + "\n")
|
57 |
|
|
# Vertex Normals
|
58 |
|
|
for vert in mesh.verts:
|
59 |
root |
1.3 |
f.write(`vert.no[0]` + " " + `vert.no[2]` + " " + `vert.no[1]` + "\n")
|
60 |
root |
1.1 |
|
61 |
|
|
# uv coordinates
|
62 |
|
|
if mesh.hasVertexUV:
|
63 |
|
|
# UV header
|
64 |
|
|
f.write("U " + `len(mesh.verts)` + "\n")
|
65 |
|
|
for vert in mesh.verts:
|
66 |
|
|
f.write(`vert.uvco[0]` + " " + `vert.uvco[1]` + "\n")
|
67 |
|
|
|
68 |
|
|
|
69 |
|
|
# Faces: Take two passes: First, count the total number of faces,
|
70 |
|
|
# second: output them. Tessalate quads to triangles. Discard edges (which
|
71 |
|
|
# have only two vertices)
|
72 |
|
|
# Modified to use only one pass, by building a variable with the text
|
73 |
|
|
faces = len(mesh.faces)
|
74 |
|
|
data = ""
|
75 |
|
|
colordata = ""
|
76 |
|
|
for face in mesh.faces:
|
77 |
root |
1.2 |
if len (face.v) < 4:
|
78 |
|
|
# triangle
|
79 |
root |
1.3 |
data = data + `face.v[0].index` + " " + `face.v[2].index` + " " + `face.v[1].index` + "\n"
|
80 |
root |
1.1 |
else:
|
81 |
|
|
# this one is a quad
|
82 |
|
|
# Break it up into two triangles
|
83 |
|
|
# Hence one additional face
|
84 |
|
|
faces = faces + 1
|
85 |
root |
1.2 |
|
86 |
root |
1.3 |
data = data + `face.v[0].index` + " " + `face.v[3].index` + " " + `face.v[1].index` + "\n"
|
87 |
|
|
data = data + `face.v[1].index` + " " + `face.v[3].index` + " " + `face.v[2].index` + "\n"
|
88 |
root |
1.1 |
# Now I can write the header with the correct face count, and then the data
|
89 |
|
|
f.write("A " + `faces` + "\n")
|
90 |
|
|
f.write(data)
|
91 |
|
|
|
92 |
|
|
|
93 |
|
|
|
94 |
|
|
#Stage 1:
|
95 |
|
|
# Gather information -- which objects, which frames, what type of export
|
96 |
|
|
|
97 |
|
|
# Objects to export: Use the set of selected objects
|
98 |
|
|
objectsToExport = Blender.Object.GetSelected()
|
99 |
|
|
|
100 |
|
|
# Which frames:
|
101 |
|
|
startFrame = Blender.Get('staframe')
|
102 |
|
|
endFrame = Blender.Get('endframe')
|
103 |
|
|
frameset = range(startFrame, endFrame + 1)
|
104 |
|
|
|
105 |
|
|
|
106 |
|
|
# Create a file for output
|
107 |
|
|
filename = Blender.Get('filename') + '.blasc'
|
108 |
|
|
print('Exporting ' + filename + ' frames:')
|
109 |
|
|
print(frameset)
|
110 |
|
|
|
111 |
|
|
fout = open(filename, 'w')
|
112 |
|
|
|
113 |
|
|
# Go through the objects and export them
|
114 |
|
|
|
115 |
|
|
# output file header
|
116 |
|
|
|
117 |
|
|
#for ob in objectsToExport:
|
118 |
|
|
ob = objectsToExport[0]
|
119 |
|
|
# output object header
|
120 |
|
|
print('Exporting Object ' + ob.name)
|
121 |
|
|
fout.write("O " + ob.name + "\n")
|
122 |
|
|
#for fr in frameset:
|
123 |
|
|
# Blender.Set('curframe', fr)
|
124 |
|
|
#print('Exporting Frame ' + `fr`)
|
125 |
|
|
|
126 |
|
|
# output frame header
|
127 |
|
|
#fout.write("F " + `fr` + "\n")
|
128 |
|
|
|
129 |
|
|
# Write out common items
|
130 |
|
|
#fout.write("Y " + `ob.data.block_type` + "\n")
|
131 |
|
|
#fout.write("L " + `ob.LocX + ob.dLocX` + " " + `ob.LocY + ob.dLocY` + " " + `ob.LocZ + ob.dLocZ` + "\n")
|
132 |
|
|
#fout.write("R " + `ob.RotX + ob.dRotX` + " " + `ob.RotY + ob.dRotY` + " " + `ob.RotZ + ob.dRotZ` + "\n")
|
133 |
|
|
#fout.write("S " + `ob.SizeX + ob.dSizeX` + " " + `ob.SizeY + ob.dSizeY` + " " + `ob.SizeZ + ob.dSizeZ` + "\n")
|
134 |
|
|
|
135 |
|
|
# Write out the data
|
136 |
|
|
dump(fout, ob.data)
|
137 |
|
|
|
138 |
|
|
# output Frame footer
|
139 |
|
|
#fout.write("/F\n")
|
140 |
|
|
#Output object footer
|
141 |
|
|
|
142 |
|
|
# Close the file
|
143 |
|
|
fout.close();
|
144 |
|
|
|
145 |
|
|
print('Done!')
|