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