Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ac3d_import.py « scripts « release - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 07e8540798ac742261b4c370fb0ba5ffc89959e9 (plain)
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
#!BPY

""" Registration info for Blender menus:
Name: 'AC3D (.ac)...'
Blender: 232
Group: 'Import'
Tip: 'Import an AC3D (.ac) file.'
"""

__author__ = "Willian P. Germano"
__url__ = ("blender", "elysiun", "AC3D's homepage, http://www.ac3d.org",
	"PLib 3d gaming lib, http://plib.sf.net")
__version__ = "2.34 07/26/04"

__bpydoc__ = """\
This script imports AC3D models into Blender.

AC3D is a simple and affordable commercial 3d modeller also built with OpenGL.
The .ac file format is an easy to parse text format well supported,
for example, by the PLib 3d gaming library (AC3D v3.x).

Supported:<br>
    UV-textured meshes with hierarchy (grouping) information.

Missing:<br>
    Support for AC3D 4's crease tag (simple, will be added soon).

Known issues:<br>
    None.

Notes:<br>
    Check script's source code for options that can be tweaked (using Blender
empties to emulate AC3D's grouping info and setting a default folder for
textures, for the case of wrong paths in the .ac file(s).
"""

# $Id$
#
# --------------------------------------------------------------------------
# AC3DImport version 2.34 Jul 26, 2004
# Program versions: Blender 2.32+ and AC3Db files (means version 0xb)
# small update to allow a default path for textures, see TEXDIR below.
# --------------------------------------------------------------------------
# ***** BEGIN GPL LICENSE BLOCK *****
#
# Copyright (C) 2004: Willian P. Germano, wgermano _at_ ig.com.br
#
# 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 *****
# --------------------------------------------------------------------------

# Note:
# Blender doesn't handle n-gons (polygons with more than 4 vertices):
#   The script triangulates them, but concave polygons come out wrong and need
#   fixing. Avoiding or triangulating concave n-gons in AC3D is a simple way to
#   avoid problems.

# Default folder for AC3D textures, to override wrong paths, change to your
# liking or leave as "":
TEXDIR = ""

# Set 'GROUP' to 1 to make Blender group imported objects using Empties,
# to reproduce the object hierarchy in the .ac file
GROUP = 0

import Blender

if TEXDIR:
    TEXDIR = TEXDIR.replace('\\','/')
    if TEXDIR[-1] != '/': TEXDIR += '/'

errmsg = ""

class Obj:
    
    def __init__(self, type):
        self.type = type
        self.dad = None
        self.name = ''
        self.data = ''
        self.tex = ''
        self.texrep = [1,1]
        self.texoff = None
        self.loc = [0, 0, 0]
        self.rot = []
        self.vlist = []
        self.flist = []
        self.matlist = []
        self.kids = 0

class AC3DImport:

    def __init__(self, filename):

        global errmsg

        print "Trying to import AC3D model(s) from %s ..." % filename

        self.i = 0
        errmsg = ''
        self.importdir = Blender.sys.dirname(filename)
        try:
            file = open(filename, 'r')
        except IOError, (errno, strerror):
            errmsg = "IOError #%s: %s" % (errno, strerror)
            print errmsg
            return None
        header = file.read(5)
        header, version = header[:4], header[-1]
        if header != 'AC3D':
            file.close()
            errmsg = 'Invalid file -- AC3D header not found.'
            print errmsg
            return None
        elif version != 'b':
            print 'AC3D file version 0x%s.' % version
            print 'This importer is for version 0xb, so it may fail.' 

        self.token = {'OBJECT':     self.parse_obj,
                      'numvert':    self.parse_vert,
                      'numsurf':    self.parse_surf,
                      'name':       self.parse_name,
                      'data':       self.parse_data,
                      'kids':       self.parse_kids,
                      'loc':        self.parse_loc,
                      'rot':        self.parse_rot,
                      'MATERIAL':   self.parse_mat,
                      'texture':    self.parse_tex,
                      'texrep':     self.parse_texrep,
                      'texoff':     self.parse_texoff}

        self.objlist = []
        self.mlist = []
        self.dads = []
        self.kids = []
        self.dad = None

        self.lines = file.readlines()
        self.lines.append('')
        self.parse_file()
        file.close()
        
        self.testAC3DImport()
                
    def parse_obj(self, value):
        if self.kids:
            while not self.kids[-1]:
                self.kids.pop()
                self.dad = self.dad.dad
            self.kids[-1] -= 1
        new = Obj(value)
        new.dad = self.dad
        new.name = value
        self.objlist.append(new)

    def parse_kids(self, value):
        kids = int(value)
        if kids:
            self.kids.append(kids)
            self.dad = self.objlist[-1]
        self.objlist[-1].kids = kids

    def parse_name(self, value):
        name = value.split('"')[1]
        self.objlist[-1].name = name

    def parse_data(self, value):
        data = self.lines[self.i].strip()
        self.objlist[-1].data = data

    def parse_tex(self, value):
        texture = value.split('"')[1]
        self.objlist[-1].tex = texture

    def parse_texrep(self, trash):
        trep = self.lines[self.i - 1]
        trep = trep.split()
        trep = [float(trep[1]), float(trep[2])]
        self.objlist[-1].texrep = trep
        self.objlist[-1].texoff = [0, 0]

    def parse_texoff(self, trash):
        toff = self.lines[self.i - 1]
        toff = toff.split()
        toff = [float(toff[1]), float(toff[2])]
        self.objlist[-1].texoff = toff
        
    def parse_mat(self, value):
        i = self.i - 1
        lines = self.lines
        line = lines[i].split()
        mat_name = ''
        mat_col = mat_spec_col = [0,0,0]
        mat_alpha = 1

        while line[0] == 'MATERIAL':
            mat_name = line[1].split('"')[1]
            mat_col = map(float,[line[3],line[4],line[5]])
            mat_spec_col = map(float,[line[15],line[16],line[17]])
            mat_alpha = float(line[-1])
            mat_alpha = 1 - mat_alpha
            self.mlist.append([mat_name, mat_col, mat_spec_col, mat_alpha])
            i += 1
            line = lines[i].split()

        self.i = i

    def parse_rot(self, trash):
        i = self.i - 1
        rot = self.lines[i].split(' ', 1)[1]
        rot = map(float, rot.split())
        self.objlist[-1].rot = rot

    def parse_loc(self, trash):
        i = self.i - 1
        loc = self.lines[i].split(' ', 1)[1]
        loc = map(float, loc.split())
        self.objlist[-1].loc = loc

    def parse_vert(self, value):
        i = self.i
        lines = self.lines
        obj = self.objlist[-1]
        vlist = obj.vlist
        n = int(value)

        while n:
            line = lines[i].split()
            line = map(float, line)
            vlist.append(line)
            n -= 1
            i += 1

        self.i = i

        rot = obj.rot
        if rot:
            nv = len(vlist)
            for j in range(nv):
                v = vlist[j]
                t = [0,0,0]
                t[0] = rot[0]*v[0] + rot[3]*v[1] + rot[6]*v[2]
                t[1] = rot[1]*v[0] + rot[4]*v[1] + rot[7]*v[2]
                t[2] = rot[2]*v[0] + rot[5]*v[1] + rot[8]*v[2]
                vlist[j] = t

        loc = obj.loc
        dad = obj.dad
        while dad:
            for j in [0, 1, 2]:
                loc[j] += dad.loc[j]
            dad = dad.dad

        for v in vlist:
            for j in [0, 1, 2]:
                v[j] += loc[j]

    def parse_surf(self, value):
        i = self.i
        is_smooth = 0
        double_sided = 0
        lines = self.lines
        obj = self.objlist[-1]
        matlist = obj.matlist
        numsurf = int(value)

        while numsurf:
            flags = lines[i].split()
            flaglow = 0
            if len(flags[1]) > 3: flaglow = int(flags[1][3])
            flaghigh = int(flags[1][2])
            is_smooth = flaghigh & 1
            twoside = flaghigh & 2
            mat = lines[i+1].split()
            mat = int(mat[1])
            if not mat in matlist: matlist.append(mat)
            refs = lines[i+2].split()
            refs = int(refs[1])
            i += 3
            face = []
            faces = []
            fuv = []
            rfs = refs

            while rfs:
                line = lines[i].split()
                v = int(line[0])
                uv = [float(line[1]), float(line[2])]
                face.append([v, uv])
                rfs -= 1
                i += 1
                
            if flaglow:
                while len(face) >= 2:
                    cut = face[:2]
                    faces.append(cut)
                    face = face[1:]

                if flaglow == 1:
                    face = [faces[-1][-1], faces[0][0]]
                    faces.append(face)

            else:
                while len(face) > 4:
                    cut = face[:4]
                    face = face[3:]
                    face.insert(0, cut[0])
                    faces.append(cut)        

                faces.append(face)

            for f in faces:
                f.append(mat)
                f.append(is_smooth)
                f.append(twoside)
                self.objlist[-1].flist.append(f)

            numsurf -= 1      

                            
        self.i = i

    def parse_file(self):
        i = 1
        lines = self.lines
        line = lines[i].split()

        while line:
            kw = ''
            for k in self.token.keys():
                if line[0] == k:
                    kw = k
                    break
            i += 1
            if kw:
                self.i = i
                self.token[kw](line[1])
                i = self.i
            line = lines[i].split()

    def testAC3DImport(self):
        global GROUP
        scene = Blender.Scene.GetCurrent()

        bmat = []
        for mat in self.mlist:
            name = mat[0]
            m = Blender.Material.New(name)
            m.rgbCol = (mat[1][0], mat[1][1], mat[1][2])
            m.specCol = (mat[2][0], mat[2][1], mat[2][2])
            m.alpha = mat[3]
            bmat.append(m)

        for obj in self.objlist:
            if obj.type == 'world':
                continue
            elif obj.type == 'group':
                if not GROUP: continue
                empty = Blender.Object.New('Empty')
                empty.name = obj.name
                scene.link(empty)
                if self.dads:
                    dadobj = Blender.Object.get(self.dads.pop())
                    dadobj.makeParent([empty])
                while obj.kids:
                    self.dads.append(empty.name)
                    obj.kids -= 1
                continue
            mesh = Blender.NMesh.New()
            if (obj.data): mesh.name = obj.data
            mesh.hasFaceUV(1)

            tex = None
            if obj.tex != '':
                try:
                    tex = Blender.Image.Load(obj.tex)
                    # Commented because it's unnecessary:
                    #tex.xrep = int(obj.texrep[0])
                    #tex.yrep = int(obj.texrep[1])
                except:
                    basetexname = Blender.sys.basename(obj.tex)
                    try:
                        obj.tex = self.importdir + '/' + basetexname
                        tex = Blender.Image.Load(obj.tex)
                    except:
                        try:
                            obj.tex = TEXDIR + basetexname
                            tex = Blender.Image.Load(obj.tex)
                        except:
                            print "Couldn't load texture: %s" % basetexname

            for v in obj.vlist:
                bvert = Blender.NMesh.Vert(v[0],v[1],v[2])
                mesh.verts.append(bvert)

            objmat_indices = []
            for mat in bmat:
                if bmat.index(mat) in obj.matlist:
                    objmat_indices.append(bmat.index(mat))
                    mesh.materials.append(mat)
            for f in obj.flist:
                twoside = f[-1]
                is_smooth = f[-2]
                fmat = f[-3]
                f=f[:-3]
                bface = Blender.NMesh.Face()
                bface.smooth = is_smooth
                if twoside: bface.mode |= Blender.NMesh.FaceModes['TWOSIDE']
                if tex:
                    bface.mode |= Blender.NMesh.FaceModes['TEX']
                    bface.image = tex
                bface.materialIndex = objmat_indices.index(fmat)
                if obj.texoff:
                    uoff = obj.texoff[0]
                    voff = obj.texoff[1]
                    urep = obj.texrep[0]
                    vrep = obj.texrep[1]
                    for vi in range(len(f)):
                        f[vi][1][0] *= urep
                        f[vi][1][1] *= vrep
                        f[vi][1][0] += uoff
                        f[vi][1][1] += voff

                for vi in range(len(f)):
                    bface.v.append(mesh.verts[f[vi][0]])
                    bface.uv.append((f[vi][1][0], f[vi][1][1]))
                mesh.faces.append(bface)

            mesh.mode = 0
            object = Blender.NMesh.PutRaw(mesh)
            object.setName(obj.name)
            object.setEuler([1.5707963,0,0]) # align ac3d w/ Blender
            if self.dads:
                dadobj = Blender.Object.get(self.dads.pop())
                dadobj.makeParent([object])

        print '...done!'

# End of class AC3DImport

def filesel_callback(filename):
  test = AC3DImport(filename)

Blender.Window.FileSelector(filesel_callback, "Import AC3D", "*.ac")