1 |
#!BPY
|
2 |
|
3 |
"""
|
4 |
Name: 'Textport save'
|
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 |
from Blender import Types, Object, NMesh, Camera, Lamp, Mathutils
|
17 |
from Blender.Mathutils import *
|
18 |
|
19 |
import sys
|
20 |
|
21 |
# dump -- the object dumper f: File, o: object
|
22 |
def dump(f, o, od):
|
23 |
if o.block_type == 'NMesh' :
|
24 |
mesh = Blender.NMesh.GetRaw(o.name)
|
25 |
f.write("M " + o.name + "\n")
|
26 |
loc = od.getLocation ()
|
27 |
f.write("L L\n" + `loc[0]` + " " + `loc[1]` + " " + `loc[2]` + "\n")
|
28 |
f.write("T Material\n");
|
29 |
|
30 |
if len (mesh.materials) > 0:
|
31 |
material = mesh.materials[0]
|
32 |
c0 = material.rgbCol[0]
|
33 |
c1 = material.rgbCol[1]
|
34 |
c2 = material.rgbCol[2]
|
35 |
f.write(`c0` + " " + `c1` + " " + `c2` + " " + `material.alpha` + "\n");
|
36 |
s0 = material.specCol[0]
|
37 |
s1 = material.specCol[1]
|
38 |
s2 = material.specCol[2]
|
39 |
f.write(`s0` + " " + `s1` + " " + `s2` + " " + `material.specTransp` + "\n");
|
40 |
s0 *= material.emit
|
41 |
s1 *= material.emit
|
42 |
s2 *= material.emit
|
43 |
f.write(`s0` + " " + `s1` + " " + `s2` + " 1\n");
|
44 |
f.write(`material.spec` + "\n");
|
45 |
else:
|
46 |
f.write("1 1 1 1\n");
|
47 |
f.write("0 0 0 1\n");
|
48 |
f.write("0 0 0 1\n");
|
49 |
f.write("0\n");
|
50 |
|
51 |
|
52 |
# Vertex Coordinate header
|
53 |
f.write("V " + `len(mesh.verts)` + "\n")
|
54 |
|
55 |
# Verticies
|
56 |
for vert in mesh.verts:
|
57 |
f.write(`vert.co[0]` + " " + `vert.co[1]` + " " + `vert.co[2]` + "\n")
|
58 |
|
59 |
# Vertex Normal Header
|
60 |
f.write("N " + `len(mesh.verts)` + "\n")
|
61 |
# Vertex Normals
|
62 |
for vert in mesh.verts:
|
63 |
f.write(`vert.no[0]` + " " + `vert.no[1]` + " " + `vert.no[2]` + "\n")
|
64 |
|
65 |
# Faces: Take two passes: First, count the total number of faces,
|
66 |
# second: output them. Tessalate quads to triangles. Discard edges (which
|
67 |
# have only two vertices)
|
68 |
# Modified to use only one pass, by building a variable with the text
|
69 |
faces = len(mesh.faces)
|
70 |
data = ""
|
71 |
colordata = ""
|
72 |
for face in mesh.faces:
|
73 |
if len (face.v) < 3:
|
74 |
faces = faces - 1
|
75 |
elif len (face.v) == 3:
|
76 |
# triangle
|
77 |
data = data + `face.v[0].index` + " " + `face.v[1].index` + " " + `face.v[2].index` + "\n"
|
78 |
data = data + `face.smooth` + "\n"
|
79 |
data = data + `face.uv[0][0]` + " " + `face.uv[0][1]` + "\n"
|
80 |
data = data + `face.uv[1][0]` + " " + `face.uv[1][1]` + "\n"
|
81 |
data = data + `face.uv[2][0]` + " " + `face.uv[2][1]` + "\n"
|
82 |
else:
|
83 |
# this one is a quad
|
84 |
# Break it up into two triangles
|
85 |
# Hence one additional face
|
86 |
faces = faces + 1
|
87 |
|
88 |
data = data + `face.v[0].index` + " " + `face.v[1].index` + " " + `face.v[3].index` + "\n"
|
89 |
data = data + `face.smooth` + "\n"
|
90 |
data = data + `face.uv[0][0]` + " " + `face.uv[0][1]` + "\n"
|
91 |
data = data + `face.uv[1][0]` + " " + `face.uv[1][1]` + "\n"
|
92 |
data = data + `face.uv[3][0]` + " " + `face.uv[3][1]` + "\n"
|
93 |
|
94 |
data = data + `face.v[1].index` + " " + `face.v[2].index` + " " + `face.v[3].index` + "\n"
|
95 |
data = data + `face.smooth` + "\n"
|
96 |
data = data + `face.uv[1][0]` + " " + `face.uv[1][1]` + "\n"
|
97 |
data = data + `face.uv[2][0]` + " " + `face.uv[2][1]` + "\n"
|
98 |
data = data + `face.uv[3][0]` + " " + `face.uv[3][1]` + "\n"
|
99 |
# Now I can write the header with the correct face count, and then the data
|
100 |
f.write("A " + `faces` + "\n")
|
101 |
f.write(data)
|
102 |
|
103 |
################################################################################
|
104 |
|
105 |
|
106 |
objectsToExport = Blender.Object.GetSelected()
|
107 |
|
108 |
startFrame = Blender.Get('staframe')
|
109 |
endFrame = Blender.Get('endframe')
|
110 |
frameset = range(startFrame, endFrame + 1)
|
111 |
|
112 |
|
113 |
filename = Blender.Get('filename') + '.blasc'
|
114 |
print('Exporting ' + filename + ' frames:')
|
115 |
print(frameset)
|
116 |
|
117 |
fout = open(filename, 'w')
|
118 |
|
119 |
ob = objectsToExport[0]
|
120 |
objs = len (objectsToExport)
|
121 |
|
122 |
print('Exporting Object ' + ob.name)
|
123 |
fout.write("O " + `objs` + "\n")
|
124 |
|
125 |
for i in objectsToExport:
|
126 |
dump (fout, i.data, i)
|
127 |
|
128 |
fout.close();
|
129 |
|
130 |
print('Done!')
|
131 |
|
132 |
|