forked from archpollux/warblender
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wbconv.py
138 lines (124 loc) · 4.45 KB
/
wbconv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import sys
import os
import Blender
from mdx import mdx
TEAMCOLOUR = [0, 0, 1]
class Converter:
def __init__(self):
pass
def toBlender(self, filename, model):
print "Converting model %s to Blender data structures" % (repr(model.MODL.Name))
bObjects = []
# textures
(filepath, dummy) = os.path.split(filename)
TEXTURES = []
for texnr in range(model.TEXS.ntexs):
tex = Blender.Texture.New(model.MODL.Name + "_tex" + str(texnr))
tex.setType('Image')
tex.setImageFlags('InterPol', 'UseAlpha', 'MipMap')
texturefile = model.TEXS.textures[texnr].TexturePath;
if texturefile!='':
if os.name!='nt':
texturefile = texturefile.replace("\\", "/")
(texturefile, dummy) = os.path.splitext(texturefile)
texturefile = os.path.join(filepath, texturefile + ".png")
sys.stderr.write("texture %d: PNG texture file (%s)\n" % (texnr, texturefile))
try:
tex.setImage(Blender.Image.Load(texturefile))
except IOError:
pass
TEXTURES.append(tex)
else:
sys.stderr.write("texture %d: None\n" % texnr)
TEXTURES.append(None)
# materials
MATERIALS = []
for matnr in range(model.MTLS.nmtls):
mat = Blender.Material.New(model.MODL.Name + "_mat" + str(matnr))
mat.setRGBCol(TEAMCOLOUR)
mat.setMode('Shadeless', 'ZTransp')
setTexFace = 1
for laynr in range(model.MTLS.materials[matnr].LAYS.nlays):
mapto = 0
lay = model.MTLS.materials[matnr].LAYS.layers[laynr]
if lay.FilterMode == 1:
mapto = mapto | Blender.Texture.MapTo.ALPHA | Blender.Texture.MapTo.COL
elif lay.FilterMode == 2:
mapto = mapto | Blender.Texture.MapTo.COL
setTexFace = 0
elif lay.FilterMode == 3:
# set 'texture blending mode' to 'add'
pass
elif lay.FilterMode == 4:
# set 'texture blending mode' to 'add'
mapto = mapto | Blender.Texture.MapTo.ALPHA
elif lay.FilterMode == 5:
# modulate ??
pass
tex = TEXTURES[model.MTLS.materials[matnr].LAYS.layers[laynr].TextureID]
if tex!=None:
mat.setTexture(laynr, tex, Blender.Texture.TexCo.UV, mapto)
# FIXME we also need a way to specify the other kind of ALPHA setting
# FIXME as well as the DVar option in the MapTo tab of Shading->Material Buttons
if setTexFace==1:
mat.setMode('TexFace')
MATERIALS.append(mat)
# geosets
for j in range(len(model.GEOS.geosets)):
obj = model.GEOS.geosets[j]
mymesh = Blender.NMesh.New(model.MODL.Name + str(j))
sys.stderr.write("mesh %d: %d NRMS\n" % (j, obj.NRMS.nvrts))
mymesh.setMaterials([MATERIALS[obj.MaterialID]]);
mymesh.hasFaceUV(1);
# vertices
nrverts = 0
for k in range(len(obj.VRTX.vertices)):
vert = obj.VRTX.vertices[k]
x = vert.x
y = vert.y
z = vert.z
v = Blender.NMesh.Vert(x, y, z)
#sys.stderr.write("initial normal %d = %s\n" % (k, repr(v.no)))
v.no[0] = obj.NRMS.vertices[k].x
v.no[1] = obj.NRMS.vertices[k].y
v.no[2] = obj.NRMS.vertices[k].z
#sys.stderr.write("normal %d = %s\n" % (k, repr(v.no)))
#if HAS_TEX==1:
# v.uvco[0] = obj.UVAS.UVBS[0].vertices[k].x;
# v.uvco[1] = obj.UVAS.UVBS[0].vertices[k].y;
mymesh.verts.append(v)
nrverts += 1
sys.stderr.write("mesh %d: %d verts\n" % (j, nrverts))
Blender.NMesh.PutRaw(mymesh)
# vertex groups
GROUPS = []
for k in range(len(obj.GNDX.vertexGroups)):
if obj.GNDX.vertexGroups[k] > len(GROUPS)-1:
GROUPS.insert(obj.GNDX.vertexGroups[k], [k])
else:
GROUPS[obj.GNDX.vertexGroups[k]].append(k)
for k in range(len(GROUPS)):
mymesh.addVertGroup('grp' + str(k))
mymesh.assignVertsToGroup('grp'+str(k), GROUPS[k], 1.0, 'replace')
# faces
nrfaces = 0
for i in range(obj.PVTX.nvrts / 3):
v1 = obj.PVTX.vertices[3 * i]
v2 = obj.PVTX.vertices[3 * i + 1]
v3 = obj.PVTX.vertices[3 * i + 2]
face = Blender.NMesh.Face([mymesh.verts[v1], mymesh.verts[v2], mymesh.verts[v3]])
uv_v1 = (obj.UVAS.UVBS[0].vertices[v1].x, 1-obj.UVAS.UVBS[0].vertices[v1].y);
uv_v2 = (obj.UVAS.UVBS[0].vertices[v2].x, 1-obj.UVAS.UVBS[0].vertices[v2].y);
uv_v3 = (obj.UVAS.UVBS[0].vertices[v3].x, 1-obj.UVAS.UVBS[0].vertices[v3].y);
face.uv = [uv_v1, uv_v2, uv_v3];
face.mode = face.mode | Blender.NMesh.FaceModes['TEX'];
#face.transp = Blender.NMesh.FaceTranspModes['ALPHA'];
#face.image = teximg;
face.mat = 0;
mymesh.faces.append(face)
nrfaces += 1
sys.stderr.write("mesh %d: %d faces\n" % (j, nrfaces))
mymesh.update()
bObjects.append(mymesh)
# bones
return bObjects