~siretart/ubuntu/utopic/blender/libav10

« back to all changes in this revision

Viewing changes to release/scripts/addons/io_scene_3ds/import_3ds.py

  • Committer: Package Import Robot
  • Author(s): Matteo F. Vescovi
  • Date: 2012-07-23 08:54:18 UTC
  • mfrom: (14.2.16 sid)
  • mto: (14.2.19 sid)
  • mto: This revision was merged to the branch mainline in revision 42.
  • Revision ID: package-import@ubuntu.com-20120723085418-9foz30v6afaf5ffs
Tags: 2.63a-2
* debian/: Cycles support added (Closes: #658075)
  For now, this top feature has been enabled only
  on [any-amd64 any-i386] architectures because
  of OpenImageIO failing on all others
* debian/: scripts installation path changed
  from /usr/lib to /usr/share:
  + debian/patches/: patchset re-worked for path changing
  + debian/control: "Breaks" field added on yafaray-exporter

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# ##### BEGIN GPL LICENSE BLOCK #####
 
2
#
 
3
#  This program is free software; you can redistribute it and/or
 
4
#  modify it under the terms of the GNU General Public License
 
5
#  as published by the Free Software Foundation; either version 2
 
6
#  of the License, or (at your option) any later version.
 
7
#
 
8
#  This program is distributed in the hope that it will be useful,
 
9
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
#  GNU General Public License for more details.
 
12
#
 
13
#  You should have received a copy of the GNU General Public License
 
14
#  along with this program; if not, write to the Free Software Foundation,
 
15
#  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 
16
#
 
17
# ##### END GPL LICENSE BLOCK #####
 
18
 
 
19
# <pep8 compliant>
 
20
 
 
21
# Script copyright (C) Bob Holcomb
 
22
# Contributors: Bob Holcomb, Richard L?rk?ng, Damien McGinnes, Campbell Barton, Mario Lapin, Dominique Lorre
 
23
 
 
24
import os
 
25
import time
 
26
import struct
 
27
 
 
28
import bpy
 
29
import mathutils
 
30
 
 
31
BOUNDS_3DS = []
 
32
 
 
33
 
 
34
######################################################
 
35
# Data Structures
 
36
######################################################
 
37
 
 
38
#Some of the chunks that we will see
 
39
#----- Primary Chunk, at the beginning of each file
 
40
PRIMARY = 0x4D4D
 
41
 
 
42
#------ Main Chunks
 
43
OBJECTINFO = 0x3D3D  # This gives the version of the mesh and is found right before the material and object information
 
44
VERSION = 0x0002  # This gives the version of the .3ds file
 
45
EDITKEYFRAME = 0xB000  # This is the header for all of the key frame info
 
46
 
 
47
#------ sub defines of OBJECTINFO
 
48
MATERIAL = 0xAFFF  # This stored the texture info
 
49
OBJECT = 0x4000  # This stores the faces, vertices, etc...
 
50
 
 
51
#>------ sub defines of MATERIAL
 
52
#------ sub defines of MATERIAL_BLOCK
 
53
MAT_NAME = 0xA000  # This holds the material name
 
54
MAT_AMBIENT = 0xA010  # Ambient color of the object/material
 
55
MAT_DIFFUSE = 0xA020  # This holds the color of the object/material
 
56
MAT_SPECULAR = 0xA030  # SPecular color of the object/material
 
57
MAT_SHINESS = 0xA040  # ??
 
58
MAT_TRANSPARENCY = 0xA050  # Transparency value of material
 
59
MAT_SELF_ILLUM = 0xA080  # Self Illumination value of material
 
60
MAT_WIRE = 0xA085  # Only render's wireframe
 
61
 
 
62
MAT_TEXTURE_MAP = 0xA200  # This is a header for a new texture map
 
63
MAT_SPECULAR_MAP = 0xA204  # This is a header for a new specular map
 
64
MAT_OPACITY_MAP = 0xA210  # This is a header for a new opacity map
 
65
MAT_REFLECTION_MAP = 0xA220  # This is a header for a new reflection map
 
66
MAT_BUMP_MAP = 0xA230  # This is a header for a new bump map
 
67
MAT_MAP_FILEPATH = 0xA300  # This holds the file name of the texture
 
68
 
 
69
MAT_MAP_TILING = 0xa351   # 2nd bit (from LSB) is mirror UV flag
 
70
MAT_MAP_USCALE = 0xA354   # U axis scaling
 
71
MAT_MAP_VSCALE = 0xA356   # V axis scaling
 
72
MAT_MAP_UOFFSET = 0xA358  # U axis offset
 
73
MAT_MAP_VOFFSET = 0xA35A  # V axis offset
 
74
MAT_MAP_ANG = 0xA35C      # UV rotation around the z-axis in rad
 
75
 
 
76
MAT_FLOAT_COLOR = 0x0010  # color defined as 3 floats
 
77
MAT_24BIT_COLOR = 0x0011  # color defined as 3 bytes
 
78
 
 
79
#>------ sub defines of OBJECT
 
80
OBJECT_MESH = 0x4100  # This lets us know that we are reading a new object
 
81
OBJECT_LAMP = 0x4600  # This lets un know we are reading a light object
 
82
OBJECT_LAMP_SPOT = 0x4610  # The light is a spotloght.
 
83
OBJECT_LAMP_OFF = 0x4620  # The light off.
 
84
OBJECT_LAMP_ATTENUATE = 0x4625
 
85
OBJECT_LAMP_RAYSHADE = 0x4627
 
86
OBJECT_LAMP_SHADOWED = 0x4630
 
87
OBJECT_LAMP_LOCAL_SHADOW = 0x4640
 
88
OBJECT_LAMP_LOCAL_SHADOW2 = 0x4641
 
89
OBJECT_LAMP_SEE_CONE = 0x4650
 
90
OBJECT_LAMP_SPOT_RECTANGULAR = 0x4651
 
91
OBJECT_LAMP_SPOT_OVERSHOOT = 0x4652
 
92
OBJECT_LAMP_SPOT_PROJECTOR = 0x4653
 
93
OBJECT_LAMP_EXCLUDE = 0x4654
 
94
OBJECT_LAMP_RANGE = 0x4655
 
95
OBJECT_LAMP_ROLL = 0x4656
 
96
OBJECT_LAMP_SPOT_ASPECT = 0x4657
 
97
OBJECT_LAMP_RAY_BIAS = 0x4658
 
98
OBJECT_LAMP_INNER_RANGE = 0x4659
 
99
OBJECT_LAMP_OUTER_RANGE = 0x465A
 
100
OBJECT_LAMP_MULTIPLIER = 0x465B
 
101
OBJECT_LAMP_AMBIENT_LIGHT = 0x4680
 
102
 
 
103
OBJECT_CAMERA = 0x4700  # This lets un know we are reading a camera object
 
104
 
 
105
#>------ sub defines of CAMERA
 
106
OBJECT_CAM_RANGES = 0x4720  # The camera range values
 
107
 
 
108
#>------ sub defines of OBJECT_MESH
 
109
OBJECT_VERTICES = 0x4110  # The objects vertices
 
110
OBJECT_FACES = 0x4120  # The objects faces
 
111
OBJECT_MATERIAL = 0x4130  # This is found if the object has a material, either texture map or color
 
112
OBJECT_UV = 0x4140  # The UV texture coordinates
 
113
OBJECT_TRANS_MATRIX = 0x4160  # The Object Matrix
 
114
 
 
115
#>------ sub defines of EDITKEYFRAME
 
116
ED_KEY_AMBIENT_NODE = 0xB001
 
117
ED_KEY_OBJECT_NODE = 0xB002
 
118
ED_KEY_CAMERA_NODE = 0xB003
 
119
ED_KEY_TARGET_NODE = 0xB004
 
120
ED_KEY_LIGHT_NODE = 0xB005
 
121
ED_KEY_L_TARGET_NODE = 0xB006
 
122
ED_KEY_SPOTLIGHT_NODE = 0xB007
 
123
#>------ sub defines of ED_KEY_OBJECT_NODE
 
124
# EK_OB_KEYFRAME_SEG = 0xB008
 
125
# EK_OB_KEYFRAME_CURTIME = 0xB009
 
126
# EK_OB_KEYFRAME_HEADER = 0xB00A
 
127
EK_OB_NODE_HEADER = 0xB010
 
128
EK_OB_INSTANCE_NAME = 0xB011
 
129
# EK_OB_PRESCALE = 0xB012
 
130
EK_OB_PIVOT = 0xB013
 
131
# EK_OB_BOUNDBOX =   0xB014
 
132
# EK_OB_MORPH_SMOOTH = 0xB015
 
133
EK_OB_POSITION_TRACK = 0xB020
 
134
EK_OB_ROTATION_TRACK = 0xB021
 
135
EK_OB_SCALE_TRACK = 0xB022
 
136
# EK_OB_CAMERA_FOV_TRACK = 0xB023
 
137
# EK_OB_CAMERA_ROLL_TRACK = 0xB024
 
138
# EK_OB_COLOR_TRACK = 0xB025
 
139
# EK_OB_MORPH_TRACK = 0xB026
 
140
# EK_OB_HOTSPOT_TRACK = 0xB027
 
141
# EK_OB_FALLOF_TRACK = 0xB028
 
142
# EK_OB_HIDE_TRACK = 0xB029
 
143
# EK_OB_NODE_ID = 0xB030
 
144
 
 
145
ROOT_OBJECT = 0xFFFF
 
146
 
 
147
global scn
 
148
scn = None
 
149
 
 
150
object_dictionary = {}
 
151
object_matrix = {}
 
152
 
 
153
 
 
154
#the chunk class
 
155
class chunk:
 
156
    ID = 0
 
157
    length = 0
 
158
    bytes_read = 0
 
159
 
 
160
    #we don't read in the bytes_read, we compute that
 
161
    binary_format = "<HI"
 
162
 
 
163
    def __init__(self):
 
164
        self.ID = 0
 
165
        self.length = 0
 
166
        self.bytes_read = 0
 
167
 
 
168
    def dump(self):
 
169
        print('ID: ', self.ID)
 
170
        print('ID in hex: ', hex(self.ID))
 
171
        print('length: ', self.length)
 
172
        print('bytes_read: ', self.bytes_read)
 
173
 
 
174
 
 
175
def read_chunk(file, chunk):
 
176
    temp_data = file.read(struct.calcsize(chunk.binary_format))
 
177
    data = struct.unpack(chunk.binary_format, temp_data)
 
178
    chunk.ID = data[0]
 
179
    chunk.length = data[1]
 
180
    #update the bytes read function
 
181
    chunk.bytes_read = 6
 
182
 
 
183
    #if debugging
 
184
    #chunk.dump()
 
185
 
 
186
 
 
187
def read_string(file):
 
188
    #read in the characters till we get a null character
 
189
    s = b''
 
190
    while True:
 
191
        c = struct.unpack('<c', file.read(1))[0]
 
192
        if c == b'\x00':
 
193
            break
 
194
        s += c
 
195
        #print 'string: ',s
 
196
 
 
197
    #remove the null character from the string
 
198
#       print("read string", s)
 
199
    return str(s, "utf-8", "replace"), len(s) + 1
 
200
 
 
201
######################################################
 
202
# IMPORT
 
203
######################################################
 
204
 
 
205
 
 
206
def process_next_object_chunk(file, previous_chunk):
 
207
    new_chunk = chunk()
 
208
 
 
209
    while (previous_chunk.bytes_read < previous_chunk.length):
 
210
        #read the next chunk
 
211
        read_chunk(file, new_chunk)
 
212
 
 
213
 
 
214
def skip_to_end(file, skip_chunk):
 
215
    buffer_size = skip_chunk.length - skip_chunk.bytes_read
 
216
    binary_format = "%ic" % buffer_size
 
217
    file.read(struct.calcsize(binary_format))
 
218
    skip_chunk.bytes_read += buffer_size
 
219
 
 
220
 
 
221
def add_texture_to_material(image, texture, scale, offset, extension, material, mapto):
 
222
    #print('assigning %s to %s' % (texture, material))
 
223
 
 
224
    if mapto not in {'COLOR', 'SPECULARITY', 'ALPHA', 'NORMAL'}:
 
225
        print('/tError:  Cannot map to "%s"\n\tassuming diffuse color. modify material "%s" later.' % (mapto, material.name))
 
226
        mapto = "COLOR"
 
227
 
 
228
    if image:
 
229
        texture.image = image
 
230
 
 
231
    mtex = material.texture_slots.add()
 
232
    mtex.texture = texture
 
233
    mtex.texture_coords = 'UV'
 
234
    mtex.use_map_color_diffuse = False
 
235
 
 
236
    mtex.scale = (scale[0], scale[1], 1.0)
 
237
    mtex.offset = (offset[0], offset[1], 0.0)
 
238
 
 
239
    texture.extension = 'REPEAT'
 
240
    if extension == 'mirror':
 
241
        # 3DS mirror flag can be emulated by these settings (at least so it seems)
 
242
        texture.repeat_x = texture.repeat_y = 2
 
243
        texture.use_mirror_x = texture.use_mirror_y = True
 
244
    elif extension == 'decal':
 
245
        # 3DS' decal mode maps best to Blenders CLIP
 
246
        texture.extension = 'CLIP'
 
247
 
 
248
    if mapto == 'COLOR':
 
249
        mtex.use_map_color_diffuse = True
 
250
    elif mapto == 'SPECULARITY':
 
251
        mtex.use_map_specular = True
 
252
    elif mapto == 'ALPHA':
 
253
        mtex.use_map_alpha = True
 
254
    elif mapto == 'NORMAL':
 
255
        mtex.use_map_normal = True
 
256
 
 
257
 
 
258
def process_next_chunk(file, previous_chunk, importedObjects, IMAGE_SEARCH):
 
259
    from bpy_extras.image_utils import load_image
 
260
 
 
261
    #print previous_chunk.bytes_read, 'BYTES READ'
 
262
    contextObName = None
 
263
    contextLamp = [None, None]  # object, Data
 
264
    contextMaterial = None
 
265
    contextMatrix_rot = None  # Blender.mathutils.Matrix(); contextMatrix.identity()
 
266
    #contextMatrix_tx = None # Blender.mathutils.Matrix(); contextMatrix.identity()
 
267
    contextMesh_vertls = None  # flat array: (verts * 3)
 
268
    contextMesh_facels = None
 
269
    contextMeshMaterials = []  # (matname, [face_idxs])
 
270
    contextMeshUV = None  # flat array (verts * 2)
 
271
 
 
272
    TEXTURE_DICT = {}
 
273
    MATDICT = {}
 
274
#       TEXMODE = Mesh.FaceModes['TEX']
 
275
 
 
276
    # Localspace variable names, faster.
 
277
    STRUCT_SIZE_FLOAT = struct.calcsize('f')
 
278
    STRUCT_SIZE_2FLOAT = struct.calcsize('2f')
 
279
    STRUCT_SIZE_3FLOAT = struct.calcsize('3f')
 
280
    STRUCT_SIZE_4FLOAT = struct.calcsize('4f')
 
281
    STRUCT_SIZE_UNSIGNED_SHORT = struct.calcsize('H')
 
282
    STRUCT_SIZE_4UNSIGNED_SHORT = struct.calcsize('4H')
 
283
    STRUCT_SIZE_4x3MAT = struct.calcsize('ffffffffffff')
 
284
    # STRUCT_SIZE_4x3MAT = calcsize('ffffffffffff')
 
285
    # print STRUCT_SIZE_4x3MAT, ' STRUCT_SIZE_4x3MAT'
 
286
    # only init once
 
287
    object_list = []  # for hierarchy
 
288
    object_parent = []  # index of parent in hierarchy, 0xFFFF = no parent
 
289
    pivot_list = []  # pivots with hierarchy handling
 
290
 
 
291
    def putContextMesh(myContextMesh_vertls, myContextMesh_facels, myContextMeshMaterials):
 
292
        bmesh = bpy.data.meshes.new(contextObName)
 
293
 
 
294
        if myContextMesh_facels is None:
 
295
            myContextMesh_facels = []
 
296
 
 
297
        if myContextMesh_vertls:
 
298
 
 
299
            bmesh.vertices.add(len(myContextMesh_vertls) // 3)
 
300
            bmesh.vertices.foreach_set("co", myContextMesh_vertls)
 
301
 
 
302
            nbr_faces = len(myContextMesh_facels)
 
303
            bmesh.polygons.add(nbr_faces)
 
304
            bmesh.loops.add(nbr_faces * 3)
 
305
            eekadoodle_faces = []
 
306
            for v1, v2, v3 in myContextMesh_facels:
 
307
                eekadoodle_faces.extend((v3, v1, v2) if v3 == 0 else (v1, v2, v3))
 
308
            bmesh.polygons.foreach_set("loop_start", range(0, nbr_faces * 3, 3))
 
309
            bmesh.polygons.foreach_set("loop_total", (3,) * nbr_faces)
 
310
            bmesh.loops.foreach_set("vertex_index", eekadoodle_faces)
 
311
 
 
312
            if bmesh.polygons and contextMeshUV:
 
313
                bmesh.uv_textures.new()
 
314
                uv_faces = bmesh.uv_textures.active.data[:]
 
315
            else:
 
316
                uv_faces = None
 
317
 
 
318
            for mat_idx, (matName, faces) in enumerate(myContextMeshMaterials):
 
319
                if matName is None:
 
320
                    bmat = None
 
321
                else:
 
322
                    bmat = MATDICT.get(matName)
 
323
                    # in rare cases no materials defined.
 
324
                    if bmat:
 
325
                        img = TEXTURE_DICT.get(bmat.name)
 
326
                    else:
 
327
                        print("    warning: material %r not defined!" % matName)
 
328
                        bmat = MATDICT[matName] = bpy.data.materials.new(matName)
 
329
                        img = None
 
330
 
 
331
                bmesh.materials.append(bmat)  # can be None
 
332
 
 
333
                if uv_faces  and img:
 
334
                    for fidx in faces:
 
335
                        bmesh.polygons[fidx].material_index = mat_idx
 
336
                        uv_faces[fidx].image = img
 
337
                else:
 
338
                    for fidx in faces:
 
339
                        bmesh.polygons[fidx].material_index = mat_idx
 
340
 
 
341
            if uv_faces:
 
342
                uvl = bmesh.uv_layers.active.data[:]
 
343
                for fidx, pl in enumerate(bmesh.polygons):
 
344
                    face = myContextMesh_facels[fidx]
 
345
                    v1, v2, v3 = face
 
346
 
 
347
                    # eekadoodle
 
348
                    if v3 == 0:
 
349
                        v1, v2, v3 = v3, v1, v2
 
350
 
 
351
                    uvl[pl.loop_start].uv = contextMeshUV[v1 * 2: (v1 * 2) + 2]
 
352
                    uvl[pl.loop_start + 1].uv = contextMeshUV[v2 * 2: (v2 * 2) + 2]
 
353
                    uvl[pl.loop_start + 2].uv = contextMeshUV[v3 * 2: (v3 * 2) + 2]
 
354
                    # always a tri
 
355
 
 
356
        bmesh.validate()
 
357
        bmesh.update()
 
358
 
 
359
        ob = bpy.data.objects.new(contextObName, bmesh)
 
360
        object_dictionary[contextObName] = ob
 
361
        SCN.objects.link(ob)
 
362
        importedObjects.append(ob)
 
363
 
 
364
        if contextMatrix_rot:
 
365
            ob.matrix_local = contextMatrix_rot
 
366
            object_matrix[ob] = contextMatrix_rot.copy()
 
367
 
 
368
    #a spare chunk
 
369
    new_chunk = chunk()
 
370
    temp_chunk = chunk()
 
371
 
 
372
    CreateBlenderObject = False
 
373
 
 
374
    def read_float_color(temp_chunk):
 
375
        temp_data = file.read(STRUCT_SIZE_3FLOAT)
 
376
        temp_chunk.bytes_read += STRUCT_SIZE_3FLOAT
 
377
        return [float(col) for col in struct.unpack('<3f', temp_data)]
 
378
 
 
379
    def read_float(temp_chunk):
 
380
        temp_data = file.read(STRUCT_SIZE_FLOAT)
 
381
        temp_chunk.bytes_read += STRUCT_SIZE_FLOAT
 
382
        return struct.unpack('<f', temp_data)[0]
 
383
 
 
384
    def read_short(temp_chunk):
 
385
        temp_data = file.read(STRUCT_SIZE_UNSIGNED_SHORT)
 
386
        temp_chunk.bytes_read += STRUCT_SIZE_UNSIGNED_SHORT
 
387
        return struct.unpack('<H', temp_data)[0]
 
388
 
 
389
    def read_byte_color(temp_chunk):
 
390
        temp_data = file.read(struct.calcsize('3B'))
 
391
        temp_chunk.bytes_read += 3
 
392
        return [float(col) / 255 for col in struct.unpack('<3B', temp_data)]  # data [0,1,2] == rgb
 
393
 
 
394
    def read_texture(new_chunk, temp_chunk, name, mapto):
 
395
        new_texture = bpy.data.textures.new(name, type='IMAGE')
 
396
 
 
397
        u_scale, v_scale, u_offset, v_offset = 1.0, 1.0, 0.0, 0.0
 
398
        mirror = False
 
399
        extension = 'wrap'
 
400
        while (new_chunk.bytes_read < new_chunk.length):
 
401
            #print 'MAT_TEXTURE_MAP..while', new_chunk.bytes_read, new_chunk.length
 
402
            read_chunk(file, temp_chunk)
 
403
 
 
404
            if temp_chunk.ID == MAT_MAP_FILEPATH:
 
405
                texture_name, read_str_len = read_string(file)
 
406
 
 
407
                img = TEXTURE_DICT[contextMaterial.name] = load_image(texture_name, dirname)
 
408
                temp_chunk.bytes_read += read_str_len  # plus one for the null character that gets removed
 
409
 
 
410
            elif temp_chunk.ID == MAT_MAP_USCALE:
 
411
                u_scale = read_float(temp_chunk)
 
412
            elif temp_chunk.ID == MAT_MAP_VSCALE:
 
413
                v_scale = read_float(temp_chunk)
 
414
 
 
415
            elif temp_chunk.ID == MAT_MAP_UOFFSET:
 
416
                u_offset = read_float(temp_chunk)
 
417
            elif temp_chunk.ID == MAT_MAP_VOFFSET:
 
418
                v_offset = read_float(temp_chunk)
 
419
 
 
420
            elif temp_chunk.ID == MAT_MAP_TILING:
 
421
                tiling = read_short(temp_chunk)
 
422
                if tiling & 0x2:
 
423
                    extension = 'mirror'
 
424
                elif tiling & 0x10:
 
425
                    extension = 'decal'
 
426
 
 
427
            elif temp_chunk.ID == MAT_MAP_ANG:
 
428
                print("\nwarning: ignoring UV rotation")
 
429
 
 
430
            skip_to_end(file, temp_chunk)
 
431
            new_chunk.bytes_read += temp_chunk.bytes_read
 
432
 
 
433
        # add the map to the material in the right channel
 
434
        if img:
 
435
            add_texture_to_material(img, new_texture, (u_scale, v_scale),
 
436
                                    (u_offset, v_offset), extension, contextMaterial, mapto)
 
437
 
 
438
    dirname = os.path.dirname(file.name)
 
439
 
 
440
    #loop through all the data for this chunk (previous chunk) and see what it is
 
441
    while (previous_chunk.bytes_read < previous_chunk.length):
 
442
        #print '\t', previous_chunk.bytes_read, 'keep going'
 
443
        #read the next chunk
 
444
        #print 'reading a chunk'
 
445
        read_chunk(file, new_chunk)
 
446
 
 
447
        #is it a Version chunk?
 
448
        if new_chunk.ID == VERSION:
 
449
            #print 'if new_chunk.ID == VERSION:'
 
450
            #print 'found a VERSION chunk'
 
451
            #read in the version of the file
 
452
            #it's an unsigned short (H)
 
453
            temp_data = file.read(struct.calcsize('I'))
 
454
            version = struct.unpack('<I', temp_data)[0]
 
455
            new_chunk.bytes_read += 4  # read the 4 bytes for the version number
 
456
            #this loader works with version 3 and below, but may not with 4 and above
 
457
            if version > 3:
 
458
                print('\tNon-Fatal Error:  Version greater than 3, may not load correctly: ', version)
 
459
 
 
460
        #is it an object info chunk?
 
461
        elif new_chunk.ID == OBJECTINFO:
 
462
            #print 'elif new_chunk.ID == OBJECTINFO:'
 
463
            # print 'found an OBJECTINFO chunk'
 
464
            process_next_chunk(file, new_chunk, importedObjects, IMAGE_SEARCH)
 
465
 
 
466
            #keep track of how much we read in the main chunk
 
467
            new_chunk.bytes_read += temp_chunk.bytes_read
 
468
 
 
469
        #is it an object chunk?
 
470
        elif new_chunk.ID == OBJECT:
 
471
 
 
472
            if CreateBlenderObject:
 
473
                putContextMesh(contextMesh_vertls, contextMesh_facels, contextMeshMaterials)
 
474
                contextMesh_vertls = []
 
475
                contextMesh_facels = []
 
476
 
 
477
                ## preparando para receber o proximo objeto
 
478
                contextMeshMaterials = []  # matname:[face_idxs]
 
479
                contextMeshUV = None
 
480
                #contextMesh.vertexUV = 1 # Make sticky coords.
 
481
                # Reset matrix
 
482
                contextMatrix_rot = None
 
483
                #contextMatrix_tx = None
 
484
 
 
485
            CreateBlenderObject = True
 
486
            contextObName, read_str_len = read_string(file)
 
487
            new_chunk.bytes_read += read_str_len
 
488
 
 
489
        #is it a material chunk?
 
490
        elif new_chunk.ID == MATERIAL:
 
491
 
 
492
#                       print("read material")
 
493
 
 
494
            #print 'elif new_chunk.ID == MATERIAL:'
 
495
            contextMaterial = bpy.data.materials.new('Material')
 
496
 
 
497
        elif new_chunk.ID == MAT_NAME:
 
498
            #print 'elif new_chunk.ID == MAT_NAME:'
 
499
            material_name, read_str_len = read_string(file)
 
500
 
 
501
#                       print("material name", material_name)
 
502
 
 
503
            #plus one for the null character that ended the string
 
504
            new_chunk.bytes_read += read_str_len
 
505
 
 
506
            contextMaterial.name = material_name.rstrip()  # remove trailing  whitespace
 
507
            MATDICT[material_name] = contextMaterial
 
508
 
 
509
        elif new_chunk.ID == MAT_AMBIENT:
 
510
            #print 'elif new_chunk.ID == MAT_AMBIENT:'
 
511
            read_chunk(file, temp_chunk)
 
512
            if temp_chunk.ID == MAT_FLOAT_COLOR:
 
513
                contextMaterial.mirror_color = read_float_color(temp_chunk)
 
514
#                               temp_data = file.read(struct.calcsize('3f'))
 
515
#                               temp_chunk.bytes_read += 12
 
516
#                               contextMaterial.mirCol = [float(col) for col in struct.unpack('<3f', temp_data)]
 
517
            elif temp_chunk.ID == MAT_24BIT_COLOR:
 
518
                contextMaterial.mirror_color = read_byte_color(temp_chunk)
 
519
#                               temp_data = file.read(struct.calcsize('3B'))
 
520
#                               temp_chunk.bytes_read += 3
 
521
#                               contextMaterial.mirCol = [float(col)/255 for col in struct.unpack('<3B', temp_data)] # data [0,1,2] == rgb
 
522
            else:
 
523
                skip_to_end(file, temp_chunk)
 
524
            new_chunk.bytes_read += temp_chunk.bytes_read
 
525
 
 
526
        elif new_chunk.ID == MAT_DIFFUSE:
 
527
            #print 'elif new_chunk.ID == MAT_DIFFUSE:'
 
528
            read_chunk(file, temp_chunk)
 
529
            if temp_chunk.ID == MAT_FLOAT_COLOR:
 
530
                contextMaterial.diffuse_color = read_float_color(temp_chunk)
 
531
#                               temp_data = file.read(struct.calcsize('3f'))
 
532
#                               temp_chunk.bytes_read += 12
 
533
#                               contextMaterial.rgbCol = [float(col) for col in struct.unpack('<3f', temp_data)]
 
534
            elif temp_chunk.ID == MAT_24BIT_COLOR:
 
535
                contextMaterial.diffuse_color = read_byte_color(temp_chunk)
 
536
#                               temp_data = file.read(struct.calcsize('3B'))
 
537
#                               temp_chunk.bytes_read += 3
 
538
#                               contextMaterial.rgbCol = [float(col)/255 for col in struct.unpack('<3B', temp_data)] # data [0,1,2] == rgb
 
539
            else:
 
540
                skip_to_end(file, temp_chunk)
 
541
 
 
542
#                       print("read material diffuse color", contextMaterial.diffuse_color)
 
543
 
 
544
            new_chunk.bytes_read += temp_chunk.bytes_read
 
545
 
 
546
        elif new_chunk.ID == MAT_SPECULAR:
 
547
            #print 'elif new_chunk.ID == MAT_SPECULAR:'
 
548
            read_chunk(file, temp_chunk)
 
549
            if temp_chunk.ID == MAT_FLOAT_COLOR:
 
550
                contextMaterial.specular_color = read_float_color(temp_chunk)
 
551
#                               temp_data = file.read(struct.calcsize('3f'))
 
552
#                               temp_chunk.bytes_read += 12
 
553
#                               contextMaterial.mirCol = [float(col) for col in struct.unpack('<3f', temp_data)]
 
554
            elif temp_chunk.ID == MAT_24BIT_COLOR:
 
555
                contextMaterial.specular_color = read_byte_color(temp_chunk)
 
556
#                               temp_data = file.read(struct.calcsize('3B'))
 
557
#                               temp_chunk.bytes_read += 3
 
558
#                               contextMaterial.mirCol = [float(col)/255 for col in struct.unpack('<3B', temp_data)] # data [0,1,2] == rgb
 
559
            else:
 
560
                skip_to_end(file, temp_chunk)
 
561
            new_chunk.bytes_read += temp_chunk.bytes_read
 
562
 
 
563
        elif new_chunk.ID == MAT_TEXTURE_MAP:
 
564
            read_texture(new_chunk, temp_chunk, "Diffuse", "COLOR")
 
565
 
 
566
        elif new_chunk.ID == MAT_SPECULAR_MAP:
 
567
            read_texture(new_chunk, temp_chunk, "Specular", "SPECULARITY")
 
568
 
 
569
        elif new_chunk.ID == MAT_OPACITY_MAP:
 
570
            read_texture(new_chunk, temp_chunk, "Opacity", "ALPHA")
 
571
 
 
572
        elif new_chunk.ID == MAT_BUMP_MAP:
 
573
            read_texture(new_chunk, temp_chunk, "Bump", "NORMAL")
 
574
 
 
575
        elif new_chunk.ID == MAT_TRANSPARENCY:
 
576
            #print 'elif new_chunk.ID == MAT_TRANSPARENCY:'
 
577
            read_chunk(file, temp_chunk)
 
578
            temp_data = file.read(STRUCT_SIZE_UNSIGNED_SHORT)
 
579
 
 
580
            temp_chunk.bytes_read += 2
 
581
            contextMaterial.alpha = 1 - (float(struct.unpack('<H', temp_data)[0]) / 100)
 
582
            new_chunk.bytes_read += temp_chunk.bytes_read
 
583
 
 
584
        elif new_chunk.ID == OBJECT_LAMP:  # Basic lamp support.
 
585
 
 
586
            temp_data = file.read(STRUCT_SIZE_3FLOAT)
 
587
 
 
588
            x, y, z = struct.unpack('<3f', temp_data)
 
589
            new_chunk.bytes_read += STRUCT_SIZE_3FLOAT
 
590
 
 
591
            # no lamp in dict that would be confusing
 
592
            contextLamp[1] = bpy.data.lamps.new("Lamp", 'POINT')
 
593
            contextLamp[0] = ob = bpy.data.objects.new("Lamp", contextLamp[1])
 
594
 
 
595
            SCN.objects.link(ob)
 
596
            importedObjects.append(contextLamp[0])
 
597
 
 
598
            #print 'number of faces: ', num_faces
 
599
            #print x,y,z
 
600
            contextLamp[0].location = x, y, z
 
601
 
 
602
            # Reset matrix
 
603
            contextMatrix_rot = None
 
604
            #contextMatrix_tx = None
 
605
            #print contextLamp.name,
 
606
 
 
607
        elif new_chunk.ID == OBJECT_MESH:
 
608
            # print 'Found an OBJECT_MESH chunk'
 
609
            pass
 
610
        elif new_chunk.ID == OBJECT_VERTICES:
 
611
            '''
 
612
            Worldspace vertex locations
 
613
            '''
 
614
            # print 'elif new_chunk.ID == OBJECT_VERTICES:'
 
615
            temp_data = file.read(STRUCT_SIZE_UNSIGNED_SHORT)
 
616
            num_verts = struct.unpack('<H', temp_data)[0]
 
617
            new_chunk.bytes_read += 2
 
618
 
 
619
            # print 'number of verts: ', num_verts
 
620
            contextMesh_vertls = struct.unpack('<%df' % (num_verts * 3), file.read(STRUCT_SIZE_3FLOAT * num_verts))
 
621
            new_chunk.bytes_read += STRUCT_SIZE_3FLOAT * num_verts
 
622
            # dummyvert is not used atm!
 
623
 
 
624
            #print 'object verts: bytes read: ', new_chunk.bytes_read
 
625
 
 
626
        elif new_chunk.ID == OBJECT_FACES:
 
627
            # print 'elif new_chunk.ID == OBJECT_FACES:'
 
628
            temp_data = file.read(STRUCT_SIZE_UNSIGNED_SHORT)
 
629
            num_faces = struct.unpack('<H', temp_data)[0]
 
630
            new_chunk.bytes_read += 2
 
631
            #print 'number of faces: ', num_faces
 
632
 
 
633
            # print '\ngetting a face'
 
634
            temp_data = file.read(STRUCT_SIZE_4UNSIGNED_SHORT * num_faces)
 
635
            new_chunk.bytes_read += STRUCT_SIZE_4UNSIGNED_SHORT * num_faces  # 4 short ints x 2 bytes each
 
636
            contextMesh_facels = struct.unpack('<%dH' % (num_faces * 4), temp_data)
 
637
            contextMesh_facels = [contextMesh_facels[i - 3:i] for i in range(3, (num_faces * 4) + 3, 4)]
 
638
 
 
639
        elif new_chunk.ID == OBJECT_MATERIAL:
 
640
            # print 'elif new_chunk.ID == OBJECT_MATERIAL:'
 
641
            material_name, read_str_len = read_string(file)
 
642
            new_chunk.bytes_read += read_str_len  # remove 1 null character.
 
643
 
 
644
            temp_data = file.read(STRUCT_SIZE_UNSIGNED_SHORT)
 
645
            num_faces_using_mat = struct.unpack('<H', temp_data)[0]
 
646
            new_chunk.bytes_read += STRUCT_SIZE_UNSIGNED_SHORT
 
647
 
 
648
            temp_data = file.read(STRUCT_SIZE_UNSIGNED_SHORT * num_faces_using_mat)
 
649
            new_chunk.bytes_read += STRUCT_SIZE_UNSIGNED_SHORT * num_faces_using_mat
 
650
 
 
651
            temp_data = struct.unpack("<%dH" % (num_faces_using_mat), temp_data)
 
652
 
 
653
            contextMeshMaterials.append((material_name, temp_data))
 
654
 
 
655
            #look up the material in all the materials
 
656
 
 
657
        elif new_chunk.ID == OBJECT_UV:
 
658
            temp_data = file.read(STRUCT_SIZE_UNSIGNED_SHORT)
 
659
            num_uv = struct.unpack('<H', temp_data)[0]
 
660
            new_chunk.bytes_read += 2
 
661
 
 
662
            temp_data = file.read(STRUCT_SIZE_2FLOAT * num_uv)
 
663
            new_chunk.bytes_read += STRUCT_SIZE_2FLOAT * num_uv
 
664
            contextMeshUV = struct.unpack('<%df' % (num_uv * 2), temp_data)
 
665
 
 
666
        elif new_chunk.ID == OBJECT_TRANS_MATRIX:
 
667
            # How do we know the matrix size? 54 == 4x4 48 == 4x3
 
668
            temp_data = file.read(STRUCT_SIZE_4x3MAT)
 
669
            data = list(struct.unpack('<ffffffffffff', temp_data))
 
670
            new_chunk.bytes_read += STRUCT_SIZE_4x3MAT
 
671
 
 
672
            contextMatrix_rot = mathutils.Matrix((data[:3] + [0],
 
673
                                                  data[3:6] + [0],
 
674
                                                  data[6:9] + [0],
 
675
                                                  data[9:] + [1],
 
676
                                                  )).transposed()
 
677
 
 
678
        elif  (new_chunk.ID == MAT_MAP_FILEPATH):
 
679
            texture_name, read_str_len = read_string(file)
 
680
            if contextMaterial.name not in TEXTURE_DICT:
 
681
                TEXTURE_DICT[contextMaterial.name] = load_image(texture_name, dirname, place_holder=False, recursive=IMAGE_SEARCH)
 
682
 
 
683
            new_chunk.bytes_read += read_str_len  # plus one for the null character that gets removed
 
684
        elif new_chunk.ID == EDITKEYFRAME:
 
685
            pass
 
686
 
 
687
        # including these here means their EK_OB_NODE_HEADER are scanned
 
688
        elif new_chunk.ID in {ED_KEY_AMBIENT_NODE,
 
689
                               ED_KEY_OBJECT_NODE,
 
690
                               ED_KEY_CAMERA_NODE,
 
691
                               ED_KEY_TARGET_NODE,
 
692
                               ED_KEY_LIGHT_NODE,
 
693
                               ED_KEY_L_TARGET_NODE,
 
694
                               ED_KEY_SPOTLIGHT_NODE}:  # another object is being processed
 
695
            child = None
 
696
 
 
697
        elif new_chunk.ID == EK_OB_NODE_HEADER:
 
698
            object_name, read_str_len = read_string(file)
 
699
            new_chunk.bytes_read += read_str_len
 
700
            temp_data = file.read(STRUCT_SIZE_UNSIGNED_SHORT * 2)
 
701
            new_chunk.bytes_read += 4
 
702
            temp_data = file.read(STRUCT_SIZE_UNSIGNED_SHORT)
 
703
            hierarchy = struct.unpack('<H', temp_data)[0]
 
704
            new_chunk.bytes_read += 2
 
705
 
 
706
            child = object_dictionary.get(object_name)
 
707
 
 
708
            if child is None:
 
709
                child = bpy.data.objects.new(object_name, None)  # create an empty object
 
710
                SCN.objects.link(child)
 
711
                importedObjects.append(child)
 
712
 
 
713
            object_list.append(child)
 
714
            object_parent.append(hierarchy)
 
715
            pivot_list.append(mathutils.Vector((0.0, 0.0, 0.0)))
 
716
 
 
717
        elif new_chunk.ID == EK_OB_INSTANCE_NAME:
 
718
            object_name, read_str_len = read_string(file)
 
719
            # child.name = object_name
 
720
            child.name += "." + object_name
 
721
            object_dictionary[object_name] = child
 
722
            new_chunk.bytes_read += read_str_len
 
723
            # print("new instance object:", object_name)
 
724
 
 
725
        elif new_chunk.ID == EK_OB_PIVOT:  # translation
 
726
                temp_data = file.read(STRUCT_SIZE_3FLOAT)
 
727
                pivot = struct.unpack('<3f', temp_data)
 
728
                new_chunk.bytes_read += STRUCT_SIZE_3FLOAT
 
729
                pivot_list[len(pivot_list) - 1] = mathutils.Vector(pivot)
 
730
 
 
731
        elif new_chunk.ID == EK_OB_POSITION_TRACK:  # translation
 
732
            new_chunk.bytes_read += STRUCT_SIZE_UNSIGNED_SHORT * 5
 
733
            temp_data = file.read(STRUCT_SIZE_UNSIGNED_SHORT * 5)
 
734
            temp_data = file.read(STRUCT_SIZE_UNSIGNED_SHORT)
 
735
            nkeys = struct.unpack('<H', temp_data)[0]
 
736
            temp_data = file.read(STRUCT_SIZE_UNSIGNED_SHORT)
 
737
            new_chunk.bytes_read += STRUCT_SIZE_UNSIGNED_SHORT * 2
 
738
            for i in range(nkeys):
 
739
                temp_data = file.read(STRUCT_SIZE_UNSIGNED_SHORT)
 
740
                nframe = struct.unpack('<H', temp_data)[0]
 
741
                new_chunk.bytes_read += STRUCT_SIZE_UNSIGNED_SHORT
 
742
                temp_data = file.read(STRUCT_SIZE_UNSIGNED_SHORT * 2)
 
743
                new_chunk.bytes_read += STRUCT_SIZE_UNSIGNED_SHORT * 2
 
744
                temp_data = file.read(STRUCT_SIZE_3FLOAT)
 
745
                loc = struct.unpack('<3f', temp_data)
 
746
                new_chunk.bytes_read += STRUCT_SIZE_3FLOAT
 
747
                if nframe == 0:
 
748
                    child.location = loc
 
749
 
 
750
        elif new_chunk.ID == EK_OB_ROTATION_TRACK:  # rotation
 
751
            new_chunk.bytes_read += STRUCT_SIZE_UNSIGNED_SHORT * 5
 
752
            temp_data = file.read(STRUCT_SIZE_UNSIGNED_SHORT * 5)
 
753
            temp_data = file.read(STRUCT_SIZE_UNSIGNED_SHORT)
 
754
            nkeys = struct.unpack('<H', temp_data)[0]
 
755
            temp_data = file.read(STRUCT_SIZE_UNSIGNED_SHORT)
 
756
            new_chunk.bytes_read += STRUCT_SIZE_UNSIGNED_SHORT * 2
 
757
            for i in range(nkeys):
 
758
                temp_data = file.read(STRUCT_SIZE_UNSIGNED_SHORT)
 
759
                nframe = struct.unpack('<H', temp_data)[0]
 
760
                new_chunk.bytes_read += STRUCT_SIZE_UNSIGNED_SHORT
 
761
                temp_data = file.read(STRUCT_SIZE_UNSIGNED_SHORT * 2)
 
762
                new_chunk.bytes_read += STRUCT_SIZE_UNSIGNED_SHORT * 2
 
763
                temp_data = file.read(STRUCT_SIZE_4FLOAT)
 
764
                rad, axis_x, axis_y, axis_z = struct.unpack("<4f", temp_data)
 
765
                new_chunk.bytes_read += STRUCT_SIZE_4FLOAT
 
766
                if nframe == 0:
 
767
                    child.rotation_euler = mathutils.Quaternion((axis_x, axis_y, axis_z), -rad).to_euler()   # why negative?
 
768
 
 
769
        elif new_chunk.ID == EK_OB_SCALE_TRACK:  # translation
 
770
            new_chunk.bytes_read += STRUCT_SIZE_UNSIGNED_SHORT * 5
 
771
            temp_data = file.read(STRUCT_SIZE_UNSIGNED_SHORT * 5)
 
772
            temp_data = file.read(STRUCT_SIZE_UNSIGNED_SHORT)
 
773
            nkeys = struct.unpack('<H', temp_data)[0]
 
774
            temp_data = file.read(STRUCT_SIZE_UNSIGNED_SHORT)
 
775
            new_chunk.bytes_read += STRUCT_SIZE_UNSIGNED_SHORT * 2
 
776
            for i in range(nkeys):
 
777
                temp_data = file.read(STRUCT_SIZE_UNSIGNED_SHORT)
 
778
                nframe = struct.unpack('<H', temp_data)[0]
 
779
                new_chunk.bytes_read += STRUCT_SIZE_UNSIGNED_SHORT
 
780
                temp_data = file.read(STRUCT_SIZE_UNSIGNED_SHORT * 2)
 
781
                new_chunk.bytes_read += STRUCT_SIZE_UNSIGNED_SHORT * 2
 
782
                temp_data = file.read(STRUCT_SIZE_3FLOAT)
 
783
                sca = struct.unpack('<3f', temp_data)
 
784
                new_chunk.bytes_read += STRUCT_SIZE_3FLOAT
 
785
                if nframe == 0:
 
786
                    child.scale = sca
 
787
 
 
788
        else:  # (new_chunk.ID!=VERSION or new_chunk.ID!=OBJECTINFO or new_chunk.ID!=OBJECT or new_chunk.ID!=MATERIAL):
 
789
            # print 'skipping to end of this chunk'
 
790
            #print("unknown chunk: "+hex(new_chunk.ID))
 
791
            buffer_size = new_chunk.length - new_chunk.bytes_read
 
792
            binary_format = "%ic" % buffer_size
 
793
            temp_data = file.read(struct.calcsize(binary_format))
 
794
            new_chunk.bytes_read += buffer_size
 
795
 
 
796
        #update the previous chunk bytes read
 
797
        # print 'previous_chunk.bytes_read += new_chunk.bytes_read'
 
798
        # print previous_chunk.bytes_read, new_chunk.bytes_read
 
799
        previous_chunk.bytes_read += new_chunk.bytes_read
 
800
        ## print 'Bytes left in this chunk: ', previous_chunk.length - previous_chunk.bytes_read
 
801
 
 
802
    # FINISHED LOOP
 
803
    # There will be a number of objects still not added
 
804
    if CreateBlenderObject:
 
805
        putContextMesh(contextMesh_vertls, contextMesh_facels, contextMeshMaterials)
 
806
 
 
807
    # Assign parents to objects
 
808
    # check _if_ we need to assign first because doing so recalcs the depsgraph
 
809
    for ind, ob in enumerate(object_list):
 
810
        parent = object_parent[ind]
 
811
        if parent == ROOT_OBJECT:
 
812
            if ob.parent is not None:
 
813
                ob.parent = None
 
814
        else:
 
815
            if ob.parent != object_list[parent]:
 
816
                ob.parent = object_list[parent]
 
817
 
 
818
            # pivot_list[ind] += pivot_list[parent]  # XXX, not sure this is correct, should parent space matrix be applied before combining?
 
819
    # fix pivots
 
820
    for ind, ob in enumerate(object_list):
 
821
        if ob.type == 'MESH':
 
822
            pivot = pivot_list[ind]
 
823
            pivot_matrix = object_matrix.get(ob, mathutils.Matrix())  # unlikely to fail
 
824
            pivot_matrix = mathutils.Matrix.Translation(pivot_matrix.to_3x3() * -pivot)
 
825
            ob.data.transform(pivot_matrix)
 
826
 
 
827
 
 
828
def load_3ds(filepath,
 
829
             context,
 
830
             IMPORT_CONSTRAIN_BOUNDS=10.0,
 
831
             IMAGE_SEARCH=True,
 
832
             APPLY_MATRIX=True,
 
833
             global_matrix=None):
 
834
    global SCN
 
835
 
 
836
    # XXX
 
837
#       if BPyMessages.Error_NoFile(filepath):
 
838
#               return
 
839
 
 
840
    print("importing 3DS: %r..." % (filepath), end="")
 
841
 
 
842
    if bpy.ops.object.select_all.poll():
 
843
        bpy.ops.object.select_all(action='DESELECT')
 
844
 
 
845
    time1 = time.clock()
 
846
#       time1 = Blender.sys.time()
 
847
 
 
848
    current_chunk = chunk()
 
849
 
 
850
    file = open(filepath, 'rb')
 
851
 
 
852
    #here we go!
 
853
    # print 'reading the first chunk'
 
854
    read_chunk(file, current_chunk)
 
855
    if current_chunk.ID != PRIMARY:
 
856
        print('\tFatal Error:  Not a valid 3ds file: %r' % filepath)
 
857
        file.close()
 
858
        return
 
859
 
 
860
    if IMPORT_CONSTRAIN_BOUNDS:
 
861
        BOUNDS_3DS[:] = [1 << 30, 1 << 30, 1 << 30, -1 << 30, -1 << 30, -1 << 30]
 
862
    else:
 
863
        BOUNDS_3DS[:] = []
 
864
 
 
865
    ##IMAGE_SEARCH
 
866
 
 
867
    # fixme, make unglobal, clear in case
 
868
    object_dictionary.clear()
 
869
    object_matrix.clear()
 
870
 
 
871
    scn = context.scene
 
872
#       scn = bpy.data.scenes.active
 
873
    SCN = scn
 
874
#       SCN_OBJECTS = scn.objects
 
875
#       SCN_OBJECTS.selected = [] # de select all
 
876
 
 
877
    importedObjects = []  # Fill this list with objects
 
878
    process_next_chunk(file, current_chunk, importedObjects, IMAGE_SEARCH)
 
879
 
 
880
    # fixme, make unglobal
 
881
    object_dictionary.clear()
 
882
    object_matrix.clear()
 
883
 
 
884
    # Link the objects into this scene.
 
885
    # Layers = scn.Layers
 
886
 
 
887
    # REMOVE DUMMYVERT, - remove this in the next release when blenders internal are fixed.
 
888
 
 
889
    if APPLY_MATRIX:
 
890
        for ob in importedObjects:
 
891
            if ob.type == 'MESH':
 
892
                me = ob.data
 
893
                me.transform(ob.matrix_local.inverted())
 
894
 
 
895
    # print(importedObjects)
 
896
    if global_matrix:
 
897
        for ob in importedObjects:
 
898
            if ob.parent is None:
 
899
                ob.matrix_world = ob.matrix_world * global_matrix
 
900
 
 
901
    for ob in importedObjects:
 
902
        ob.select = True
 
903
 
 
904
    # Done DUMMYVERT
 
905
    """
 
906
    if IMPORT_AS_INSTANCE:
 
907
        name = filepath.split('\\')[-1].split('/')[-1]
 
908
        # Create a group for this import.
 
909
        group_scn = Scene.New(name)
 
910
        for ob in importedObjects:
 
911
            group_scn.link(ob) # dont worry about the layers
 
912
 
 
913
        grp = Blender.Group.New(name)
 
914
        grp.objects = importedObjects
 
915
 
 
916
        grp_ob = Object.New('Empty', name)
 
917
        grp_ob.enableDupGroup = True
 
918
        grp_ob.DupGroup = grp
 
919
        scn.link(grp_ob)
 
920
        grp_ob.Layers = Layers
 
921
        grp_ob.sel = 1
 
922
    else:
 
923
        # Select all imported objects.
 
924
        for ob in importedObjects:
 
925
            scn.link(ob)
 
926
            ob.Layers = Layers
 
927
            ob.sel = 1
 
928
    """
 
929
 
 
930
    if 0:
 
931
#       if IMPORT_CONSTRAIN_BOUNDS!=0.0:
 
932
        # Set bounds from objecyt bounding box
 
933
        for ob in importedObjects:
 
934
            if ob.type == 'MESH':
 
935
#                       if ob.type=='Mesh':
 
936
                ob.makeDisplayList()  # Why dosnt this update the bounds?
 
937
                for v in ob.getBoundBox():
 
938
                    for i in (0, 1, 2):
 
939
                        if v[i] < BOUNDS_3DS[i]:
 
940
                            BOUNDS_3DS[i] = v[i]  # min
 
941
 
 
942
                        if v[i] > BOUNDS_3DS[i + 3]:
 
943
                            BOUNDS_3DS[i + 3] = v[i]  # min
 
944
 
 
945
        # Get the max axis x/y/z
 
946
        max_axis = max(BOUNDS_3DS[3] - BOUNDS_3DS[0], BOUNDS_3DS[4] - BOUNDS_3DS[1], BOUNDS_3DS[5] - BOUNDS_3DS[2])
 
947
        # print max_axis
 
948
        if max_axis < 1 << 30:  # Should never be false but just make sure.
 
949
 
 
950
            # Get a new scale factor if set as an option
 
951
            SCALE = 1.0
 
952
            while (max_axis * SCALE) > IMPORT_CONSTRAIN_BOUNDS:
 
953
                SCALE /= 10.0
 
954
 
 
955
            # SCALE Matrix
 
956
            SCALE_MAT = mathutils.Matrix.Scale(SCALE, 4)
 
957
 
 
958
            for ob in importedObjects:
 
959
                if ob.parent is None:
 
960
                    ob.matrix_world = ob.matrix_world * SCALE_MAT
 
961
 
 
962
        # Done constraining to bounds.
 
963
 
 
964
    # Select all new objects.
 
965
    print(" done in %.4f sec." % (time.clock() - time1))
 
966
    file.close()
 
967
 
 
968
 
 
969
def load(operator,
 
970
         context,
 
971
         filepath="",
 
972
         constrain_size=0.0,
 
973
         use_image_search=True,
 
974
         use_apply_transform=True,
 
975
         global_matrix=None,
 
976
         ):
 
977
 
 
978
    load_3ds(filepath,
 
979
             context,
 
980
             IMPORT_CONSTRAIN_BOUNDS=constrain_size,
 
981
             IMAGE_SEARCH=use_image_search,
 
982
             APPLY_MATRIX=use_apply_transform,
 
983
             global_matrix=global_matrix,
 
984
             )
 
985
 
 
986
    return {'FINISHED'}