Gamestudio Links
Zorro Links
Newest Posts
Executing Trades on Next Bar Open
by Zheka. 06/20/24 14:26
Lapsa's very own thread
by rki. 06/19/24 11:27
A simple game ...
by VoroneTZ. 06/18/24 10:50
Face player all the time ...
by bbn1982. 06/18/24 10:25
Zorro Beta 2.61: PyTorch
by jcl. 06/10/24 14:42
New FXCM FIX Plugin
by flink. 06/04/24 07:30
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
1 registered members (AndrewAMD), 692 guests, and 1 spider.
Key: Admin, Global Mod, Mod
Newest Members
Mino, squik, AemStones, LucasJoshua, Baklazhan
19061 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
why do I get this error, when I try to export .mdl with blender? #318018
04/04/10 10:34
04/04/10 10:34
Joined: Nov 2008
Posts: 196
the wrong place
Muhsin Offline OP
Member
Muhsin  Offline OP
Member

Joined: Nov 2008
Posts: 196
the wrong place
Hi!

When im trying to export my model from blender to MED (.mdl) I get this error:



what can i do to prevent this??

thanks!!


- Muhsin Kaymak


Come and play my new browsergame - Valley Of Wolves:

http://www.mafiacreator.com/ValleyOfWolves

Hurry and be the first to take over the different business' in the Valley Of Wolves, before anybody else does it!
And be the most feared MafiaBoss in the World!!
Re: why do I get this error, when I try to export .mdl with blender? [Re: Muhsin] #318020
04/04/10 10:59
04/04/10 10:59
Joined: Feb 2009
Posts: 3,207
Germany, Magdeburg
Rei_Ayanami Offline
Expert
Rei_Ayanami  Offline
Expert

Joined: Feb 2009
Posts: 3,207
Germany, Magdeburg
in the new blender "as" is a keaword an can not anymore be used as a variable...

use an older blender version...

Re: why do I get this error, when I try to export .mdl with blender? [Re: Rei_Ayanami] #318021
04/04/10 11:05
04/04/10 11:05
Joined: Nov 2008
Posts: 196
the wrong place
Muhsin Offline OP
Member
Muhsin  Offline OP
Member

Joined: Nov 2008
Posts: 196
the wrong place
can't I chance the script somehow instead?


Come and play my new browsergame - Valley Of Wolves:

http://www.mafiacreator.com/ValleyOfWolves

Hurry and be the first to take over the different business' in the Valley Of Wolves, before anybody else does it!
And be the most feared MafiaBoss in the World!!
Re: why do I get this error, when I try to export .mdl with blender? [Re: Muhsin] #318030
04/04/10 13:13
04/04/10 13:13
Joined: Sep 2003
Posts: 5,900
Bielefeld, Germany
Pappenheimer Offline
Senior Expert
Pappenheimer  Offline
Senior Expert

Joined: Sep 2003
Posts: 5,900
Bielefeld, Germany
Sure, install Python, open its editor IDLE and change it.
Which version do you use?
I use blender 2.49b, and I don't have such a problem, although others sometimes.
In case you want to export bones animations, you have to use the fbx exporter, anyway.

Re: why do I get this error, when I try to export .mdl with blender? [Re: Pappenheimer] #318243
04/06/10 11:53
04/06/10 11:53
Joined: Jun 2006
Posts: 379
Flevoland, 5 meters under wate...
Roel Offline
Senior Member
Roel  Offline
Senior Member

Joined: Jun 2006
Posts: 379
Flevoland, 5 meters under wate...
I got the same problem some moths ago,
and... I changed some very little things in it, now the script works again. here it is.
I hope it helps!


Click to reveal..

#!BPY
"""
Name: '3DGameStudio mdl7 (.mdl)...'
Blender: 241
Group: 'Export'
Tooltip: 'export selected meshes to 3dgamestudio mdl7 format'
"""

__author__ = "thomas oppl"
__version__ = "6.03"
__bpydoc__ = """\

description:

this script exports all selected meshes to 3dgamestudio's mdl7 format
(3dgamestudio version 6.4 and newer!).

usage:

1. open a script window besides a 3d window (a 3d viewport has to stay
visible during export otherwise animations won't get exported!) and start
the script: scripts -> export -> 3dgamestudio mdl7 (.mdl)...

2. select the mesh objects to be exported.

3. set the scale (in blender units usually get used more like meters and in
3dgamestudio more like inches).

4. select [external textures] or [internal textures] depending on if the
textures should be stored internally or externally (for now only textures
assigned to faces per uv/image editor will be exported, internally stored
textures will only get mipmaps if their width and height are powers of
two).

5. select [world space] or [object space] coordinate export (object space
probably doesn't make much sense if exporting multiple meshes).

6. press [export animation] if animation should get exported.

- animation sequences get defined by an animation sequence name and
a list of frame numbers separated by spaces. with [v] enabled the
sequence will get exported _as vertex animation. with [b] enabled
the sequence will get exported _as bone animation. with the middle
mouse button the gui can be dragged up and down if the list gets too
long.

notes for exporting bone animations:

- the mdl7 format currently doesn't support weighted skinning
so every vertex should only have 1 bone influence. if there
is more than one influence the exporter will take the
strongest one or a random one if all influences have the
same weight.

- bones whose name begin with "ik" (not case sensitive) won't
get exported.

- only the armature of the active mesh object (=last selected
will get exported.

- the armature must have the same origin and transformation _as
the mesh object it is parented to!

- press [add] or [remove] to add or remove animation sequences

7. press [export].

8. set the path and filename and press [export mdl7] to start the export.

9. wait until the "export done!" pop-up appears.

10. with the [save options] and [load options] buttons the export settings
for the current blend file can be saved and loaded.

other notes:

- names shouldn't be longer than 20 characters for bones, 16 characters for
texture file names (without extension), 16 characters for mesh objects.

- exporting internal textures can be slow because of mipmap generation.

- the exporter can also output a xml version of the mdl7 format if the line
model.save(filename[:-4] + ".xml") gets uncommented. this functionality
was originally added for debugging purposes but maybe the xml format can
be helful for someone.
"""

#------------------------------------------------------------
# blender mdl7 exporter (c) 2006 thomas oppl toppl@fh-sbg.ac.at
#------------------------------------------------------------
# ***** 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
from Blender import Draw, BGL, Mesh, Window, Object, Armature, Types
from Blender.Mathutils import *
import os, struct, cPickle

##try:
## import psyco
## psyco.full()
## print "using psyco"
##except ImportError:
## pass

####################################################################################################
#################################################################################################### c_mdl7
####################################################################################################

class c_mdl7:
def __init__(self):
self.bones = []
self.groups = []

def addbone(self, x, y, z, name, parentindex):
"""add a bone to the model"""
self.bones.append(c_bone(x, y, z, name, parentindex))

def addgroup(self, name):
"""add a group to the model"""
group = c_group(name)
self.groups.append(group)
return group

def save(self, filename):
"""save the model"""
extension = filename[-3:].lower()
if extension == "mdl":
file = open(filename, "wb")
file.write(struct.pack("<4sllllllHHHHHHHHHH",
"MDL7", 0, len(self.bones), len(self.groups), self.getsize(), # 0 = subversion
0, 0, # entlumpsize, medlumpsize
36, 28, 16, 68, 8, 16, 26, 26, 68, 24))
# struct sizes:
# bone, skin, color, material, uv, triangle, mainvertex, framevertex, bonematrix, frame
if extension == "xml":
file = open(filename, "w")
file.write('<mdl7 version="6.03">\n')
for bone in self.bones:
bone.write(file)
for group in self.groups:
group.write(file)
if extension == "xml":
file.write('</mdl7>\n')
file.close()

def getsize(self):
"""return mdl7 size in bytes"""
size = 48 + len(self.bones) * 36
for group in self.groups:
size += group.getsize()
return size

#--------------------------------------------------------------------------------------------------- c_bone
class c_bone:
def __init__(self, x=0, y=0, z=0, name="bone", parentindex=65535):
self.x = x
self.y = y
self.z = z
self.name = name
self.parentindex = parentindex

def write(self, file):
"""write bone to file"""
extension = file.name[-3:].lower()
if extension == "mdl":
file.write(struct.pack("<HHfff20s",
self.parentindex, 0,
self.x, self.y, self.z, self.name))
if extension == "xml":
file.write(' <bone name="%s" x="%f" y="%f " z="%f" parent="%d" />\n' %\
(self.name, self.x, self.y, self.z, self.parentindex))

#--------------------------------------------------------------------------------------------------- c_group
class c_group:
def __init__(self, name="group"):
self.name = name
self.skins = []
self.uvs = []
self.triangles = []
self.vertices = []
self.frames = []

def addskin(self, name, filename="", pixels=[], sizex=0, sizey=0):
"""add a skin to the group"""
self.skins.append(c_skin(name, filename, pixels, sizex, sizey))

def adduv(self, u, v):
"""add a uv to the group"""
self.uvs.append(c_uv(u, v))

def addtriangle(self, v1, v2, v3, uv1, uv2, uv3, material):
"""add a triangle to the group"""
self.triangles.append(c_triangle(v1, v2, v3, uv1, uv2, uv3, material))

def addvertex(self, x, y, z, nx, ny, nz, boneindex=65535):
"""add a vertex to the group"""
self.vertices.append(c_mainvertex(x, y, z, nx, ny, nz, boneindex))

def addframe(self, name):
"""add a frame to the group"""
frame = c_frame(name)
self.frames.append(frame)
return frame

def write(self, file):
"""write group to file"""
extension = file.name[-3:].lower()
if extension == "mdl":
file.write(struct.pack("<BBBBl16slllll",
1, 0, 0, 0, # type 1 = triangle mesh
self.getsize(),
self.name,
len(self.skins),
len(self.uvs),
len(self.triangles),
len(self.vertices),
len(self.frames)))
if extension == "xml":
file.write(' <group name="%s" type="1">\n' % self.name)
for skin in self.skins:
skin.write(file)
for uv in self.uvs:
uv.write(file)
for triangle in self.triangles:
triangle.write(file)
for vertex in self.vertices:
vertex.write(file)
for frame in self.frames:
frame.write(file)
if extension == "xml":
file.write(' </group>\n')

def getsize(self):
"""return group size in bytes"""
size = 44 + len(self.uvs) * 8 + len(self.triangles) * 16 + len(self.vertices) * 26
for skin in self.skins:
size += skin.getsize()
for frame in self.frames:
size += frame.getsize()
return size

#--------------------------------------------------------------------------------------------------- c_skin
class c_skin:
def __init__(self, name="", filename="", pixels=[], width=0, height=0):
self.name = name
self.filename = filename
self.pixels = pixels
self.width = width
self.height = height
if self.pixels:
# currently mipmaps can only be created for these sizes
poweroftwo = (16, 32, 64, 128, 256, 512, 1024, 2048, 4096)
if self.width in poweroftwo and self.height in poweroftwo:
self.type = 12 # 12 = 888 with mipmaps
else:
self.type = 4 # 4 = 888 without mipmaps
else:
self.type = 7 # 7 = external texture

def write(self, file):
"""write skin to file"""
extension = file.name[-3:].lower()
if extension == "mdl":
if self.pixels: # internal texture
file.write(struct.pack("<BBBBll16s",
self.type, 0, 0, 0,
self.width, self.height, self.name))
w = self.width
h = self.height
mip0 = []
for y in range(h):
for x in range(w):
b = int(self.pixels[x+y*w][2] * 255)
g = int(self.pixels[x+y*w][1] * 255)
r = int(self.pixels[x+y*w][0] * 255)
mip0.append([r, g, b])
for p in mip0: file.write(struct.pack("<BBB", p[2], p[1], p[0]))
print " " + str(w) + "x" + str(h),

if self.type == 12:
def boxfilter(h, w, m):
nm = []
for y in range(0, h, 2):
for x in range(0, w, 2):
b = (m[x+y*w][2] + m[(x+1)+y*w][2] + m[(x+1)+(y+1)*w][2] + m[x+(y+1)*w][2]) / 4
g = (m[x+y*w][1] + m[(x+1)+y*w][1] + m[(x+1)+(y+1)*w][1] + m[x+(y+1)*w][1]) / 4
r = (m[x+y*w][0] + m[(x+1)+y*w][0] + m[(x+1)+(y+1)*w][0] + m[x+(y+1)*w][0]) / 4
nm.append([r, g, b])
return nm
mip1 = boxfilter(h, w, mip0)
for p in mip1: file.write(struct.pack("<BBB", p[2], p[1], p[0]))
print "(" + str(w/2) + "x" + str(h/2),
mip2 = boxfilter(h/2, w/2, mip1)
for p in mip2: file.write(struct.pack("<BBB", p[2], p[1], p[0]))
print str(w/4) + "x" + str(h/4),
mip3 = boxfilter(h/4, w/4, mip2)
for p in mip3: file.write(struct.pack("<BBB", p[2], p[1], p[0]))
print str(w/8) + "x" + str(h/8) + ") pixels written"

if self.type == 4:
print "pixels written (warning! no mipmaps generated)"

else: # external texture
file.write(struct.pack("<BBBBll16s",
self.type, 0, 0, 0,
len(self.filename) + 1, 1,
self.name) + self.filename + "\x00")

if extension == "xml": ##### set type to external texture?
file.write(' <skin name="%s" type="%s" filename="%s" />\n' % (self.name, self.type, self.filename))

def getsize(self):
"""return skin size in bytes"""
w = self.width
h = self.height
if self.type == 12:
return 28 + (w*h*3) + ((w/2)*(h/2)*3) + ((w/4)*(h/4)*3) + ((w/8)*(h/8)*3)
if self.type == 4:
return 28 + (w*h*3)
if self.type == 7:
return 28 + len(self.filename) + 1

#--------------------------------------------------------------------------------------------------- c_uv
class c_uv:
def __init__(self, u=0, v=0):
self.u = u
self.v = v

def write(self, file):
"""write uv to file"""
extension = file.name[-3:].lower()
if extension == "mdl":
file.write(struct.pack("<ff", self.u, self.v))
if extension == "xml":
file.write(' <uv u="%f" v="%f" />\n' % (self.u, self.v))

#--------------------------------------------------------------------------------------------------- c_triangle
class c_triangle:
def __init__(self, v1=0, v2=0, v3=0, uv1=0, uv2=0, uv3=0, material=0):
self.v1 = v1
self.v2 = v2
self.v3 = v3
self.uv1 = uv1
self.uv2 = uv2
self.uv3 = uv3
self.material = material

def write(self, file):
"""write triangle to file"""
extension = file.name[-3:].lower()
if extension == "mdl":
file.write(struct.pack("<HHHHHHi",
self.v1, self.v2, self.v3,
self.uv1, self.uv2, self.uv3,
self.material))
if extension == "xml":
file.write(' <triangle v1="%d" v2="%d" v3="%d" uv1="%d" uv2="%d" uv3="%d" m="%d" />\n' %\
(self.v1, self.v2, self.v3, self.uv1, self.uv2, self.uv3, self.material))

#--------------------------------------------------------------------------------------------------- c_mainvertex
class c_mainvertex:
def __init__(self, x=0, y=0, z=0, nx=0, ny=0, nz=0, boneindex=65535):
self.x = x
self.y = y
self.z = z
self.nx = nx
self.ny = ny
self.nz = nz
self.boneindex = boneindex

def write(self, file):
"""write mainvertex to file"""
extension = file.name[-3:].lower()
if extension == "mdl":
file.write(struct.pack("<fffHfff",
self.x, self.y, self.z,
self.boneindex,
self.nx, self.ny, self.nz))
if extension == "xml":
file.write(' <vertex x="%f" y="%f" z="%f" nx="%f" ny="%f" nz="%f" b="%d" />\n' %\
(self.x, self.y, self.z, self.nx, self.ny, self.nz, self.boneindex))

#--------------------------------------------------------------------------------------------------- c_frame
class c_frame:
def __init__(self, name="frame"):
self.name = name
self.vertices = []
self.matrices = []

def addvertex(self, x, y, z, nx, ny, nz, vertexindex):
"""add a vertex to the frame"""
self.vertices.append(c_framevertex(x, y, z, nx, ny, nz, vertexindex))

def addmatrix(self, matrix, boneindex):
"""add a matrix to the frame"""
self.matrices.append(c_bonematrix(matrix, boneindex))

def write(self, file):
"""write frame to file"""
extension = file.name[-3:].lower()
if extension == "mdl":
file.write(struct.pack("<16sll", self.name,
len(self.vertices), len(self.matrices)))
if extension == "xml":
file.write(' <frame name="%s" />\n' % self.name)
for vertex in self.vertices:
vertex.write(file)
for matrix in self.matrices:
matrix.write(file)
if extension == "xml":
file.write(' </frame>\n')

def getsize(self):
"""return frame size in bytes"""
return 24 + len(self.vertices) * 26 + len(self.matrices) * 68

#--------------------------------------------------------------------------------------------------- c_framevertex
class c_framevertex:
def __init__(self, x=0, y=0, z=0, nx=0, ny=0, nz=0, vertexindex=0):
self.x = x
self.y = y
self.z = z
self.nx = nx
self.ny = ny
self.nz = nz
self.vertexindex = vertexindex

def write(self, file):
"""write framevertex to file"""
extension = file.name[-3:].lower()
if extension == "mdl":
file.write(struct.pack("<fffHfff", self.x, self.y, self.z,
self.vertexindex,
self.nx, self.ny, self.nz))
if extension == "xml":
file.write(' <vertex i="%d" x="%f" y="%f" z="%f" nx="%f" ny="%f" nz="%f" />\n' %\
(self.vertexindex, self.x, self.y, self.z, self.nx, self.ny, self.nz))

#--------------------------------------------------------------------------------------------------- c_bonematrix
class c_bonematrix:
def __init__(self, matrix=[], boneindex=0):
self.matrix = matrix
self.boneindex = boneindex

def write(self, file):
"""write bonematrix to file"""
extension = file.name[-3:].lower()
if extension == "mdl":
file.write(struct.pack("<16fHH",
self.matrix[0][0], self.matrix[0][1], self.matrix[0][2], self.matrix[0][3],
self.matrix[1][0], self.matrix[1][1], self.matrix[1][2], self.matrix[1][3],
self.matrix[2][0], self.matrix[2][1], self.matrix[2][2], self.matrix[2][3],
self.matrix[3][0], self.matrix[3][1], self.matrix[3][2], self.matrix[3][3],
self.boneindex, 0))
if extension == "xml":
s = ' <matrix i="%d" ' +\
'm00="%f" m01="%f" m02="%f" m03="%f" ' +\
'm10="%f" m11="%f" m12="%f" m13="%f" ' +\
'm20="%f" m21="%f" m22="%f" m23="%f" ' +\
'm30="%f" m31="%f" m32="%f" m33="%f" />\n'
file.write(s % (self.boneindex,
self.matrix[0][0], self.matrix[0][1], self.matrix[0][2], self.matrix[0][3],
self.matrix[1][0], self.matrix[1][1], self.matrix[1][2], self.matrix[1][3],
self.matrix[2][0], self.matrix[2][1], self.matrix[2][2], self.matrix[2][3],
self.matrix[3][0], self.matrix[3][1], self.matrix[3][2], self.matrix[3][3]))

####################################################################################################
#################################################################################################### c_gui
####################################################################################################

class c_gui:
def __init__(self, x, y, defaultscale=1.0, defaultfilename=""):
self.guipos = [x, y]
self.defaultscale = defaultscale
self.defaultfilename = defaultfilename # for development
self.guiid = 100
def newid():
self.guiid += 1
return self.guiid
self.B_SCALE = newid()
self.B_SAVEOPTIONS = newid()
self.B_LOADOPTIONS = newid()
self.B_ETEX = newid()
self.B_ITEX = newid()
self.B_WSPACE = newid()
self.B_OSPACE = newid()
self.B_ANIMATION = newid()
self.B_EXPORT = newid()
self.B_REMOVE = newid()
self.B_ADD = newid()
self.B_SEQUENCES = 1000
self.B_SEQUENCES_VB = 10000
self.num_scale = Draw.Create(self.defaultscale)
self.tog_etex = Draw.Create(1)
self.tog_itex = Draw.Create(0)
self.tog_wspace = Draw.Create(1)
self.tog_ospace = Draw.Create(0)
self.tog_animation = Draw.Create(0)
self.animationsequences = [[Draw.Create(""), Draw.Create(""), Draw.Create(1), Draw.Create(0)]]
self.mousedrag = 0
self.mouseold = [0, 0]

#----------------------------------------------------------------------------------------------- register
def register(self):
Draw.Register(self.draw, self.keyboardmouseevent, self.buttonevent)

#----------------------------------------------------------------------------------------------- startexport
def export(self, filename):
exporter = c_exporter(self.num_scale.val,
self.tog_etex.val, self.tog_wspace.val,
self.tog_animation,
[[_as[0].val, _as[1].val, _as[2].val, _as[3].val] for _as in self.animationsequences])
exporter.write(filename)

#----------------------------------------------------------------------------------------------- saveoptions
def saveoptions(self):
datadir = Blender.Get("datadir")
if datadir:
configfilename = Blender.sys.join(datadir, "export_3dgs_mdl7_6.03.cfg")
if Blender.sys.exists(configfilename):
d = cPickle.load(open(configfilename))
else:
d = {}
fn = Blender.Get("filename")
d[fn] = (self.num_scale.val, self.tog_etex.val, self.tog_itex.val,
self.tog_wspace.val, self.tog_ospace.val, self.tog_animation.val,
[[_as[0].val, _as[1].val, _as[2].val, _as[3].val] for _as in self.animationsequences])
cPickle.dump(d, open(configfilename, "w"))
#print "export options saved!"

#----------------------------------------------------------------------------------------------- loadoptions
def loadoptions(self):
datadir = Blender.Get("datadir")
if datadir:
configfilename = Blender.sys.join(datadir, "export_3dgs_mdl7_6.03.cfg")
if Blender.sys.exists(configfilename):
d = cPickle.load(open(configfilename))
fn = Blender.Get("filename")
if fn in d:
self.num_scale.val = d[fn][0]
self.tog_etex.val, self.tog_itex.val = d[fn][1], d[fn][2]
self.tog_wspace.val, self.tog_ospace.val = d[fn][3], d[fn][4]
self.tog_animation.val = d[fn][5]
self.animationsequences = []
for _as in d[fn][6]:
self.animationsequences.append([
Draw.Create(_as[0]), Draw.Create(_as[1]),
Draw.Create(_as[2]), Draw.Create(_as[3])])
#print "export options loaded!"
else:
Draw.PupMenu("no options found for this blend file!%t|OK")
else:
Draw.PupMenu("no options file found!%t|OK")

#----------------------------------------------------------------------------------------------- keyboardmouseevent
def keyboardmouseevent(self, evt, val):
##### handle mouse going outside of the window while dragging?
if evt == Draw.MIDDLEMOUSE:
if val == 1:
self.mousedrag = 1
self.mouseold = Window.GetMouseCoords()
if val == 0:
self.mousedrag = 0
if self.mousedrag:
self.mousecurrent = Window.GetMouseCoords()
self.guipos[1] += self.mousecurrent[1] - self.mouseold[1]
self.mouseold = Window.GetMouseCoords()
Draw.Redraw(1)

#----------------------------------------------------------------------------------------------- buttonevent
def buttonevent(self, evt):
if evt == self.B_SAVEOPTIONS:
self.saveoptions()
if evt == self.B_LOADOPTIONS:
self.loadoptions()
if evt == self.B_ETEX:
self.tog_etex.val = 1
self.tog_itex.val = 0
if evt == self.B_ITEX:
self.tog_itex.val = 1
self.tog_etex.val = 0
if evt == self.B_WSPACE:
self.tog_wspace.val = 1
self.tog_ospace.val = 0
if evt == self.B_OSPACE:
self.tog_ospace.val = 1
self.tog_wspace.val = 0

if evt >= self.B_SEQUENCES_VB:
if evt % 2 == 0: # v button
index = (evt - 10000) / 2
self.animationsequences[index][2].val = 1
self.animationsequences[index][3].val = 0
else: # b button
index = (evt - 10001) / 2
self.animationsequences[index][3].val = 1
self.animationsequences[index][2].val = 0
if evt == self.B_ADD:
self.animationsequences.append([Draw.Create(""), Draw.Create(""), Draw.Create(1), Draw.Create(0)])
if evt == self.B_REMOVE:
if(len(self.animationsequences) > 1):
self.animationsequences.pop()

if evt == self.B_EXPORT:
if not self.defaultfilename:
Window.FileSelector(self.export, "export mdl7", Blender.sys.makename(ext=".mdl"))
else:
self.export(self.defaultfilename)

Draw.Redraw(1)

#----------------------------------------------------------------------------------------------- draw
def draw(self):
BGL.glClearColor(0.6, 0.6, 0.6, 1)
BGL.glClear(BGL.GL_COLOR_BUFFER_BIT)
BGL.glColor3f(0, 0, 0)

x, y = self.guipos[0], self.guipos[1]

BGL.glRasterPos2i(x, y)
Draw.Text("3dgamestudio mdl7 exporter", "normal")
y -= 40
BGL.glRasterPos2i(x, y)
Draw.Text("export options", "normal")

y -= 30
self.num_scale = Draw.Number("scale: ", self.B_SCALE, x, y, 100, 20, self.num_scale.val, 0.01, 1024, "scale factor")
Draw.PushButton("save options", self.B_SAVEOPTIONS, x + 210, y, 100, 20, "save the export options for this blender file")
Draw.PushButton("load options", self.B_LOADOPTIONS, x + 315, y, 100, 20, "load the export options for this blender file")
y -= 30
self.tog_etex = Draw.Toggle("external textures", self.B_ETEX, x, y, 100, 20, self.tog_etex.val, "use external textures and copy them to the target folder")
self.tog_itex = Draw.Toggle("internal textures", self.B_ITEX, x + 105, y, 100, 20, self.tog_itex.val, "store textures within the model file")
y -= 30
self.tog_wspace = Draw.Toggle("world space", self.B_WSPACE, x, y, 100, 20, self.tog_wspace.val, "export world space coordinates")
self.tog_ospace = Draw.Toggle("object space", self.B_OSPACE, x + 105, y, 100, 20, self.tog_ospace.val, "export object space coordinates (only useful for exporting a single object)")
y -= 30
self.tog_animation = Draw.Toggle("export animation", self.B_ANIMATION, x, y, 100, 20, self.tog_animation.val, "export animation")

if self.tog_animation.val == 1:
y -= 30
BGL.glRasterPos2i(x, y)
Draw.Text("animation sequences", "normal")
i = 0
j = 0
for _as in self.animationsequences:
y -= 30
_as[0] = Draw.String("name: ", self.B_SEQUENCES + i, x, y, 100, 20, _as[0].val, 14, "name of the animation sequence")
i += 1
_as[1] = Draw.String("frames: ", self.B_SEQUENCES + i, x + 105, y, 270, 20, _as[1].val, 128, "list of frames separated by spaces")
i += 1
_as[2] = Draw.Toggle("v", self.B_SEQUENCES_VB + j, x + 380, y, 15, 20, _as[2].val, "export sequence _as vertex animation")
j += 1
_as[3] = Draw.Toggle("b", self.B_SEQUENCES_VB + j, x + 400, y, 15, 20, _as[3].val, "export sequence _as bone animation")
j += 1
y -= 30
Draw.PushButton("add", self.B_ADD, x, y, 100, 20, "add an animation sequence")
Draw.PushButton("remove", self.B_REMOVE, x + 105, y, 100, 20, "remove an animation sequence")

y -= 60
Draw.PushButton("export", self.B_EXPORT, x, y, 415, 20, "export the selected mesh")

#--------------------------------------------------------------------------------------------------- main
if __name__ == '__main__':
gui = c_gui(10, 320, 32.0, "")
gui.register()

####################################################################################################
#################################################################################################### c_exporter
####################################################################################################

class c_exporter:
def __init__(self, scale=1.0, etex=True, wspace=True, animation=False, animationsequences=[]):
self.meshobjects = []
self.armatureobject = None
self.bonenames = []
self.textures = []
self.SCALE = scale
self.ETEX = etex
self.WSPACE = wspace
self.ANIMATION = animation
self.ANIMATIONSEQUENCES = animationsequences
self.framelist = []
for _as in self.ANIMATIONSEQUENCES:
frames = _as[1].split()
i = 0
for f in frames:
self.framelist.append([_as[0] + str(i), int(f), _as[2]]) # name, number, type
i += 1

#----------------------------------------------------------------------------------------------- write
def write(self, filename):
starttime = Blender.sys.time()
print
print "------------------------------------------------------------ mdl7 export"
print "filename: " + filename
print

model = c_mdl7()
self.getobjects()
if not self.meshobjects:
print "no mesh objects selected! aborting..."
Draw.PupMenu("no mesh objects selected! aborting...%t|OK")
return
if self.armatureobject:
self.exportarmature(model)
self.gettextures()
for texture in self.textures:
width, height = texture[0].getSize()
print " texture \"%s\": %dx%d" % (texture[1], width, height)
print

for obj in self.meshobjects: # export each object _as separate group
if len(obj.data.name) > 16:
print "warning! mesh object name \""+ obj.data.name +"\" too long! continuing..."
Draw.PupMenu("warning! mesh object name \""+ obj.data.name +"\" too long! continuing...%t|OK")
print " object \"%s\":" % obj.data.name
group = model.addgroup(obj.data.name)
if group == model.groups[0]: # add all textures to the first group (mdl7 format quirk)
self.exporttextures(group)
uvs = self.exportuvs(obj, group)
self.exportmainvertices(obj, group)
self.exporttriangles(obj, uvs, group)

group.addframe("bindpose") # empty frame is needed for med (=3dgs model editor)

if self.ANIMATION: # export animations
print " frames:"
for frame in self.framelist:
framename, framenumber, vertexanimation = frame[0], frame[1], frame[2]
Blender.Set("curframe", framenumber)
Window.Redraw() # necessary to update the mesh data
if vertexanimation:
self.exportvertexanimationframe(obj, framename, group)
print " name: %s frame: %d type: %d" % (framename, framenumber, vertexanimation)
else:
if self.armatureobject:
if group == model.groups[0]: # bone animation frames can be in the first group only (mdl7 format quirk)
self.exportboneanimationframe(framename, group)
print " name: %s frame: %d type: %d" % (framename, framenumber, vertexanimation)
print

model.save(filename)
#model.save(filename[:-4] + ".xml")
if self.ETEX: # copy textures to target folder
for texture in self.textures:
os.system("copy \"" + texture[3] + "\" \"" + os.path.dirname(filename) + "\"")
print
print "export done in", Blender.sys.time() - starttime, "seconds!"
Draw.PupMenu("export done!%t|OK")

#----------------------------------------------------------------------------------------------- getobjects
def getobjects(self):
self.meshobjects = [o for o in Object.GetSelected() if type(o.getData()) == Types.NMeshType]
if self.meshobjects:
if self.meshobjects[0].parent: # first in the list is the last selected (=active object)
if type(self.meshobjects[0].parent.getData()) == Types.ArmatureType:
self.armatureobject = self.meshobjects[0].parent

#----------------------------------------------------------------------------------------------- gettextures
def gettextures(self):
images = []
for obj in self.meshobjects: # interate through all faces of each selected object to get the image list
mesh = Mesh.New()
mesh.getFromObject(obj.name, 1) # 1 = undeformed
if mesh.faceUV: # mesh has uvs
for f in mesh.faces:
if f.image != None:
if f.image not in images:
images.append(f.image)
for image in images:
imagename = os.path.basename(image.getFilename())[:-4]
imagefilename = os.path.basename(image.getFilename())
imagefilenamewithpath = image.getFilename()
self.textures.append([image, imagename, imagefilename, imagefilenamewithpath])
#print self.textures

#----------------------------------------------------------------------------------------------- exportarmature
def exportarmature(self, model):
def bindposerelativetranslation(bone):
am = Matrix([1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1])
if self.WSPACE:
am = self.armatureobject.matrix
scalematrix = ScaleMatrix(self.SCALE, 4)
position = Matrix(Matrix(bone.matrix["ARMATURESPACE"]) * am * scalematrix).translationPart()
if bone.hasParent():
position -= Matrix(Matrix(bone.parent.matrix["ARMATURESPACE"]) * am * scalematrix).translationPart()
return position

armature = self.armatureobject.getData()
# find all root bones
rootbones = []
for bone in armature.bones.values():
if not bone.hasParent():
rootbones.append(bone)

# sort (+export) bones -> parents have to be listed before their children
print " armature \"%s\":" % armature.name
def iteratebones(bones, level):
for name in [bone.name for bone in bones]:
if len(name) > 20:
print "warning! bone name \""+ name +"\" too long! continuing..."
Draw.PupMenu("warning! bone name \""+ name +"\" too long! continuing...%t|OK")
if name[0:2].lower() != "ik": # ignore ik bones
print " %s%s" % (level * " ", name)
self.bonenames.append(name)
bone = armature.bones[name]
position = bindposerelativetranslation(bone)
if bone.hasParent():
parentindex = self.bonenames.index(bone.parent.name)
else:
parentindex = 65536
model.addbone(position.x, position.y, position.z, name, parentindex)
if bone.hasChildren():
iteratebones(bone.children, level + 2)
iteratebones(rootbones, 0)
print

#----------------------------------------------------------------------------------------------- exporttextures
def exporttextures(self, group):
for texture in self.textures:
image, name, filename, filenamewithpath = texture[0], texture[1], texture[2], texture[3]
if len(name) > 16:
print "warning! texture name \""+ name +"\" too long! continuing..."
Draw.PupMenu("warning! texture name \""+ name +"\" too long! continuing...%t|OK")
if self.ETEX: # external textures
group.addskin(name, filename)
else: # internal texture
width, height = image.getSize()
pixels = []
for y in range(height - 1, -1, -1): # flip vertically
for x in range(width):
pixels.append(image.getPixelF(x, y))
group.addskin(name, "", pixels, width, height)
print " %d %s textures" % (len(self.textures), ["internal", "external"][int(self.ETEX)])

#----------------------------------------------------------------------------------------------- exportuvs
def exportuvs(self, obj, group):
mesh = obj.getData(0, 1) # (0 = return obj, 1 = mesh instead of nmesh)
# every uv pair should be unique
uvs = [[0.0, 0.0]]
if mesh.faceUV: # mesh has uvs
for f in mesh.faces:
if f.uv: # triangle has uvs
for uv in f.uv:
if uv not in uvs: # can be slow!
uvs.append(uv)
# add uvs to group
for uv in uvs:
group.adduv(uv[0], 1 - uv[1]) # flip vertically
print " %d uvs" % len(uvs)
return uvs

#----------------------------------------------------------------------------------------------- exportmainvertices
def exportmainvertices(self, obj, group):
linkedmesh = obj.getData(0, 1) # undeformed mesh (0 = obj, 1 = mesh instead of nmesh)
def vertexgroupnames():
return linkedmesh.getVertGroupNames()
def vertexinfluences(v):
return linkedmesh.getVertexInfluences(v)
def getboneindex(v):
weights = [vi[1] for vi in vertexinfluences(v)]
biggest_weight = max(weights)
bonename = vertexinfluences(v)[weights.index(biggest_weight)][0]
if bonename in self.bonenames:
return self.bonenames.index(bonename)
else:
return 65536

# export vertices
mesh = Mesh.New() # get new unlinked mesh so transformation does only happen temporary
mesh.getFromObject(obj.name, 1) # 1 = undeformed
if self.WSPACE:
mesh.transform(obj.matrix)
scalematrix = ScaleMatrix(self.SCALE, 4)
mesh.transform(scalematrix)
for v in mesh.verts:
boneindex = 65536
if vertexgroupnames():
if vertexinfluences(v.index):
boneindex = getboneindex(v.index)
group.addvertex(v.co[0], v.co[1], v.co[2],
-v.no[0], -v.no[1], -v.no[2], # negative -> because of reverse winding
boneindex)
print " %d vertices" % len(mesh.verts)

#----------------------------------------------------------------------------------------------- exporttriangles
def exporttriangles(self, obj, uvs, group):
linkedmesh = obj.getData(0, 1) # undeformed mesh (0 = obj, 1 = mesh instead of nmesh)
for f in linkedmesh.faces:
textureindex = -1 # empty face
if linkedmesh.faceUV:
if f.image != None:
textureindex = [t[0] for t in self.textures].index(f.image)

fuv = [[0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]
if linkedmesh.faceUV:
if f.uv: # triangle has uvs
fuv = f.uv
if len(f.v) == 3:
group.addtriangle(
f.v[2].index, f.v[1].index, f.v[0].index, # reverse winding
uvs.index(fuv[2]), uvs.index(fuv[1]), uvs.index(fuv[0]), # can be slow!
textureindex)
if len(f.v) == 4: # triangulate
group.addtriangle(
f.v[2].index, f.v[1].index, f.v[0].index,
uvs.index(fuv[2]), uvs.index(fuv[1]), uvs.index(fuv[0]),
textureindex)
group.addtriangle(
f.v[3].index, f.v[2].index, f.v[0].index,
uvs.index(fuv[3]), uvs.index(fuv[2]), uvs.index(fuv[0]),
textureindex)
print " %d faces" % len(linkedmesh.faces)

#----------------------------------------------------------------------------------------------- exportanimationframe
def exportvertexanimationframe(self, obj, framename, group):
def samepositionasmainvertex(v):
def cf(f1, f2): # compare floats
if abs(f1 - f2) < 0.00001:
return True
else:
return False
i = v.index
if cf(v.co[0], group.vertices[i].x) and \
cf(v.co[1], group.vertices[i].y) and \
cf(v.co[2], group.vertices[i].z):
return True
else:
return False
mesh = Mesh.New()
mesh.getFromObject(obj.name, 0) # 0 = deformed
if self.WSPACE:
mesh.transform(obj.matrix)
scalematrix = ScaleMatrix(self.SCALE, 4)
mesh.transform(scalematrix)
frame = group.addframe(framename)
for v in mesh.verts:
if not samepositionasmainvertex(v): # only export vertices which really moved
frame.addvertex(v.co[0], v.co[1], v.co[2], -v.no[0], -v.no[1], -v.no[2], v.index)

#----------------------------------------------------------------------------------------------- exportboneanimationframe
def exportboneanimationframe(self, framename, group):
def isidentity(m):
def cf(f1, f2): # compare floats
if abs(f1 - f2) < 0.00001:
return True
else:
return False
if cf(m[0][0], 1) and cf(m[0][1], 0) and cf(m[0][2], 0) and cf(m[0][3], 0) and \
cf(m[1][0], 0) and cf(m[1][1], 1) and cf(m[1][2], 0) and cf(m[1][3], 0) and \
cf(m[2][0], 0) and cf(m[2][1], 0) and cf(m[2][2], 1) and cf(m[2][3], 0) and \
cf(m[3][0], 0) and cf(m[3][1], 0) and cf(m[3][2], 0) and cf(m[3][3], 1):
return True
else:
return False
def posematrix(bone):
pose = self.armatureobject.getPose()
m = Matrix(pose.bones[bone.name].poseMatrix)
if bone.hasParent():
m *= Matrix(pose.bones[bone.parent.name].poseMatrix).invert()
return m
def bindposematrix(bone):
m = Matrix(bone.matrix["ARMATURESPACE"])
if bone.hasParent():
m *= Matrix(bone.parent.matrix["ARMATURESPACE"]).invert()
return m
armature = self.armatureobject.getData()
scalematrix = ScaleMatrix(self.SCALE, 4)
frame = group.addframe(framename)
i = 0
for bonename in self.bonenames:
bone = armature.bones[bonename]
if self.WSPACE:
o1 = Matrix(bone.matrix["ARMATURESPACE"] * self.armatureobject.matrix * scalematrix).invert()
o2 = Matrix(bone.matrix["ARMATURESPACE"] * self.armatureobject.matrix * scalematrix)
else:
o1 = Matrix(bone.matrix["ARMATURESPACE"] * scalematrix).invert()
o2 = Matrix(bone.matrix["ARMATURESPACE"] * scalematrix)
m1 = bindposematrix(bone).invert()
m2 = posematrix(bone)
fm = o1 * (m2 * m1) * o2
#if not isidentity(fm): # only export non-identity matrices -> have to think about this again. can cause problems...
# frame.addmatrix(fm, i)
frame.addmatrix(fm, i)
i += 1

####################################################################################################
#################################################################################################### api for external use
####################################################################################################

__FORMAT_COMPATIBILITY__ = 'MESH', 'MESH_NAME', 'MESH_VERT', 'MESH_FACE', 'MESH_TEXFACE'

def write(filename):
exporter = c_exporter()
exporter.write(filename) # exports selected mesh objects



Check out the throwing game here: The throwing game
Re: why do I get this error, when I try to export .mdl with blender? [Re: Roel] #318576
04/08/10 15:22
04/08/10 15:22
Joined: Nov 2008
Posts: 196
the wrong place
Muhsin Offline OP
Member
Muhsin  Offline OP
Member

Joined: Nov 2008
Posts: 196
the wrong place
Thank you very much for the code. it works! thanks!!!

thanks!


- Muhsin Kaymak


Come and play my new browsergame - Valley Of Wolves:

http://www.mafiacreator.com/ValleyOfWolves

Hurry and be the first to take over the different business' in the Valley Of Wolves, before anybody else does it!
And be the most feared MafiaBoss in the World!!

Moderated by  aztec, Blink, HeelX 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1