#!BPY
"""
Name: 'TrueSpace (.cob)...'
Blender: 232
Group: 'Export'
Tooltip: 'Export selected meshes to TrueSpace File Format (.cob)'
"""
__author__ = "Anthony D'Agostino (Scorpius)"
__url__ = ("blender", "elysiun",
"Author's homepage, http://www.redrival.com/scorpius")
__version__ = "Part of IOSuite 0.5"
__bpydoc__ = """\
This script exports meshes to TrueSpace file format.
TrueSpace is a commercial modeling and rendering application. The .cob
file format is composed of 'chunks,' is well defined, and easy to read and
write. It's very similar to LightWave's lwo format.
Usage:
Select meshes to be exported and run this script from "File->Export" menu.
Supported:
Vertex colors will be exported, if they are present.
Known issues:
Before exporting to .cob format, the mesh must have real-time UV
coordinates. Press the FKEY to assign them.
Notes:
There are a few differences between how Blender & TrueSpace represent
their objects' transformation matrices. Blender simply uses a 4x4 matrix,
and trueSpace splits it into the following two fields.
For the 'Local Axes' values: The x, y, and z-axis represent a simple
rotation matrix. This is equivalent to Blender's object matrix before
it was combined with the object's scaling matrix. Dividing each value by
the appropriate scaling factor (and transposing at the same time)
produces the original rotation matrix.
For the 'Current Position' values: This is equivalent to Blender's
object matrix except that the last row is omitted and the xyz location
is used in the last column. Binary format uses a 4x3 matrix, ascii
format uses a 4x4 matrix.
For Cameras: The matrix here gets a little confusing, and I'm not sure of
how to handle it.
"""
# $Id$
#
# +---------------------------------------------------------+
# | Copyright (c) 2001 Anthony D'Agostino |
# | http://www.redrival.com/scorpius |
# | scorpius@netzero.com |
# | June 12, 2001 |
# | Read and write Caligari trueSpace File Format (*.cob) |
# +---------------------------------------------------------+
# ***** BEGIN GPL LICENSE BLOCK *****
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# ***** END GPL LICENCE BLOCK *****
import Blender, meshtools
import struct, os, cStringIO, time
# ==============================
# === Write trueSpace Format ===
# ==============================
def write(filename):
start = time.clock()
file = open(filename, "wb")
objects = Blender.Object.GetSelected()
write_header(file)
G,P,V,U,M = 1000,2000,3000,4000,5000
for object in objects:
objname = object.name
meshname = object.data.name
mesh = Blender.NMesh.GetRaw(meshname)
obj = Blender.Object.Get(objname)
if not mesh: continue
grou = generate_grou('Group ' + `objects.index(object)+1`)
polh = generate_polh(objname, obj, mesh)
if meshtools.has_vertex_colors(mesh): vcol = generate_vcol(mesh)
unit = generate_unit()
mat1 = generate_mat1(mesh)
if objects.index(object) == 0: X = 0
write_chunk(file, "Grou", 0, 1, G, X, grou)
write_chunk(file, "PolH", 0, 4, P, G, polh)
if meshtools.has_vertex_colors(mesh) and vcol:
write_chunk(file, "VCol", 1, 0, V, P, vcol)
write_chunk(file, "Unit", 0, 1, U, P, unit)
write_chunk(file, "Mat1", 0, 5, M, P, mat1)
X = G
G,P,V,U,M = map(lambda x: x+1, [G,P,V,U,M])
write_chunk(file, "END ", 1, 0, 0, 0, '') # End Of File Chunk
Blender.Window.DrawProgressBar(1.0, '') # clear progressbar
file.close()
end = time.clock()
seconds = " in %.2f %s" % (end-start, "seconds")
message = "Successfully exported " + os.path.basename(filename) + seconds
meshtools.print_boxed(message)
# =============================
# === Write COB File Header ===
# =============================
def write_header(file):
file.write("Caligari V00.01BLH"+" "*13+"\n")
# ===================
# === Write Chunk ===
# ===================
def write_chunk(file, name, major, minor, chunk_id, parent_id, data):
file.write(name)
file.write(struct.pack("<2h", major, minor))
file.write(struct.pack("<2l", chunk_id, parent_id))
file.write(struct.pack("<1l", len(data)))
file.write(data)
# ============================================
# === Generate PolH (Polygonal Data) Chunk ===
# ============================================
def generate_polh(objname, obj, mesh):
data = cStringIO.StringIO()
write_ObjectName(data, objname)
write_LocalAxes(data, obj)
write_CurrentPosition(data, obj)
write_VertexList(data, mesh)
uvcoords = write_UVCoordsList(data, mesh)
write_FaceList(data, mesh, uvcoords)
return data.getvalue()
# === Write Object Name ===
def write_ObjectName(data, objname):
data.write(struct.pack("