1
# ##### BEGIN GPL LICENSE BLOCK #####
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.
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.
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.
17
# ##### END GPL LICENSE BLOCK #####
21
# Script copyright (C) Bob Holcomb
22
# Contributors: Bob Holcomb, Richard L?rk?ng, Damien McGinnes, Campbell Barton, Mario Lapin, Dominique Lorre
34
######################################################
36
######################################################
38
#Some of the chunks that we will see
39
#----- Primary Chunk, at the beginning of each file
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
47
#------ sub defines of OBJECTINFO
48
MATERIAL = 0xAFFF # This stored the texture info
49
OBJECT = 0x4000 # This stores the faces, vertices, etc...
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
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
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
76
MAT_FLOAT_COLOR = 0x0010 # color defined as 3 floats
77
MAT_24BIT_COLOR = 0x0011 # color defined as 3 bytes
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
103
OBJECT_CAMERA = 0x4700 # This lets un know we are reading a camera object
105
#>------ sub defines of CAMERA
106
OBJECT_CAM_RANGES = 0x4720 # The camera range values
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
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
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
150
object_dictionary = {}
160
#we don't read in the bytes_read, we compute that
161
binary_format = "<HI"
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)
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)
179
chunk.length = data[1]
180
#update the bytes read function
187
def read_string(file):
188
#read in the characters till we get a null character
191
c = struct.unpack('<c', file.read(1))[0]
197
#remove the null character from the string
198
# print("read string", s)
199
return str(s, "utf-8", "replace"), len(s) + 1
201
######################################################
203
######################################################
206
def process_next_object_chunk(file, previous_chunk):
209
while (previous_chunk.bytes_read < previous_chunk.length):
211
read_chunk(file, new_chunk)
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
221
def add_texture_to_material(image, texture, scale, offset, extension, material, mapto):
222
#print('assigning %s to %s' % (texture, material))
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))
229
texture.image = image
231
mtex = material.texture_slots.add()
232
mtex.texture = texture
233
mtex.texture_coords = 'UV'
234
mtex.use_map_color_diffuse = False
236
mtex.scale = (scale[0], scale[1], 1.0)
237
mtex.offset = (offset[0], offset[1], 0.0)
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'
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
258
def process_next_chunk(file, previous_chunk, importedObjects, IMAGE_SEARCH):
259
from bpy_extras.image_utils import load_image
261
#print previous_chunk.bytes_read, 'BYTES READ'
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)
274
# TEXMODE = Mesh.FaceModes['TEX']
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'
287
object_list = [] # for hierarchy
288
object_parent = [] # index of parent in hierarchy, 0xFFFF = no parent
289
pivot_list = [] # pivots with hierarchy handling
291
def putContextMesh(myContextMesh_vertls, myContextMesh_facels, myContextMeshMaterials):
292
bmesh = bpy.data.meshes.new(contextObName)
294
if myContextMesh_facels is None:
295
myContextMesh_facels = []
297
if myContextMesh_vertls:
299
bmesh.vertices.add(len(myContextMesh_vertls) // 3)
300
bmesh.vertices.foreach_set("co", myContextMesh_vertls)
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)
312
if bmesh.polygons and contextMeshUV:
313
bmesh.uv_textures.new()
314
uv_faces = bmesh.uv_textures.active.data[:]
318
for mat_idx, (matName, faces) in enumerate(myContextMeshMaterials):
322
bmat = MATDICT.get(matName)
323
# in rare cases no materials defined.
325
img = TEXTURE_DICT.get(bmat.name)
327
print(" warning: material %r not defined!" % matName)
328
bmat = MATDICT[matName] = bpy.data.materials.new(matName)
331
bmesh.materials.append(bmat) # can be None
335
bmesh.polygons[fidx].material_index = mat_idx
336
uv_faces[fidx].image = img
339
bmesh.polygons[fidx].material_index = mat_idx
342
uvl = bmesh.uv_layers.active.data[:]
343
for fidx, pl in enumerate(bmesh.polygons):
344
face = myContextMesh_facels[fidx]
349
v1, v2, v3 = v3, v1, v2
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]
359
ob = bpy.data.objects.new(contextObName, bmesh)
360
object_dictionary[contextObName] = ob
362
importedObjects.append(ob)
364
if contextMatrix_rot:
365
ob.matrix_local = contextMatrix_rot
366
object_matrix[ob] = contextMatrix_rot.copy()
372
CreateBlenderObject = False
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)]
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]
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]
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
394
def read_texture(new_chunk, temp_chunk, name, mapto):
395
new_texture = bpy.data.textures.new(name, type='IMAGE')
397
u_scale, v_scale, u_offset, v_offset = 1.0, 1.0, 0.0, 0.0
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)
404
if temp_chunk.ID == MAT_MAP_FILEPATH:
405
texture_name, read_str_len = read_string(file)
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
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)
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)
420
elif temp_chunk.ID == MAT_MAP_TILING:
421
tiling = read_short(temp_chunk)
427
elif temp_chunk.ID == MAT_MAP_ANG:
428
print("\nwarning: ignoring UV rotation")
430
skip_to_end(file, temp_chunk)
431
new_chunk.bytes_read += temp_chunk.bytes_read
433
# add the map to the material in the right channel
435
add_texture_to_material(img, new_texture, (u_scale, v_scale),
436
(u_offset, v_offset), extension, contextMaterial, mapto)
438
dirname = os.path.dirname(file.name)
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'
444
#print 'reading a chunk'
445
read_chunk(file, new_chunk)
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
458
print('\tNon-Fatal Error: Version greater than 3, may not load correctly: ', version)
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)
466
#keep track of how much we read in the main chunk
467
new_chunk.bytes_read += temp_chunk.bytes_read
469
#is it an object chunk?
470
elif new_chunk.ID == OBJECT:
472
if CreateBlenderObject:
473
putContextMesh(contextMesh_vertls, contextMesh_facels, contextMeshMaterials)
474
contextMesh_vertls = []
475
contextMesh_facels = []
477
## preparando para receber o proximo objeto
478
contextMeshMaterials = [] # matname:[face_idxs]
480
#contextMesh.vertexUV = 1 # Make sticky coords.
482
contextMatrix_rot = None
483
#contextMatrix_tx = None
485
CreateBlenderObject = True
486
contextObName, read_str_len = read_string(file)
487
new_chunk.bytes_read += read_str_len
489
#is it a material chunk?
490
elif new_chunk.ID == MATERIAL:
492
# print("read material")
494
#print 'elif new_chunk.ID == MATERIAL:'
495
contextMaterial = bpy.data.materials.new('Material')
497
elif new_chunk.ID == MAT_NAME:
498
#print 'elif new_chunk.ID == MAT_NAME:'
499
material_name, read_str_len = read_string(file)
501
# print("material name", material_name)
503
#plus one for the null character that ended the string
504
new_chunk.bytes_read += read_str_len
506
contextMaterial.name = material_name.rstrip() # remove trailing whitespace
507
MATDICT[material_name] = contextMaterial
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
523
skip_to_end(file, temp_chunk)
524
new_chunk.bytes_read += temp_chunk.bytes_read
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
540
skip_to_end(file, temp_chunk)
542
# print("read material diffuse color", contextMaterial.diffuse_color)
544
new_chunk.bytes_read += temp_chunk.bytes_read
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
560
skip_to_end(file, temp_chunk)
561
new_chunk.bytes_read += temp_chunk.bytes_read
563
elif new_chunk.ID == MAT_TEXTURE_MAP:
564
read_texture(new_chunk, temp_chunk, "Diffuse", "COLOR")
566
elif new_chunk.ID == MAT_SPECULAR_MAP:
567
read_texture(new_chunk, temp_chunk, "Specular", "SPECULARITY")
569
elif new_chunk.ID == MAT_OPACITY_MAP:
570
read_texture(new_chunk, temp_chunk, "Opacity", "ALPHA")
572
elif new_chunk.ID == MAT_BUMP_MAP:
573
read_texture(new_chunk, temp_chunk, "Bump", "NORMAL")
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)
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
584
elif new_chunk.ID == OBJECT_LAMP: # Basic lamp support.
586
temp_data = file.read(STRUCT_SIZE_3FLOAT)
588
x, y, z = struct.unpack('<3f', temp_data)
589
new_chunk.bytes_read += STRUCT_SIZE_3FLOAT
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])
596
importedObjects.append(contextLamp[0])
598
#print 'number of faces: ', num_faces
600
contextLamp[0].location = x, y, z
603
contextMatrix_rot = None
604
#contextMatrix_tx = None
605
#print contextLamp.name,
607
elif new_chunk.ID == OBJECT_MESH:
608
# print 'Found an OBJECT_MESH chunk'
610
elif new_chunk.ID == OBJECT_VERTICES:
612
Worldspace vertex locations
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
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!
624
#print 'object verts: bytes read: ', new_chunk.bytes_read
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
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)]
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.
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
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
651
temp_data = struct.unpack("<%dH" % (num_faces_using_mat), temp_data)
653
contextMeshMaterials.append((material_name, temp_data))
655
#look up the material in all the materials
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
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)
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
672
contextMatrix_rot = mathutils.Matrix((data[:3] + [0],
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)
683
new_chunk.bytes_read += read_str_len # plus one for the null character that gets removed
684
elif new_chunk.ID == EDITKEYFRAME:
687
# including these here means their EK_OB_NODE_HEADER are scanned
688
elif new_chunk.ID in {ED_KEY_AMBIENT_NODE,
693
ED_KEY_L_TARGET_NODE,
694
ED_KEY_SPOTLIGHT_NODE}: # another object is being processed
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
706
child = object_dictionary.get(object_name)
709
child = bpy.data.objects.new(object_name, None) # create an empty object
710
SCN.objects.link(child)
711
importedObjects.append(child)
713
object_list.append(child)
714
object_parent.append(hierarchy)
715
pivot_list.append(mathutils.Vector((0.0, 0.0, 0.0)))
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)
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)
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
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
767
child.rotation_euler = mathutils.Quaternion((axis_x, axis_y, axis_z), -rad).to_euler() # why negative?
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
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
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
803
# There will be a number of objects still not added
804
if CreateBlenderObject:
805
putContextMesh(contextMesh_vertls, contextMesh_facels, contextMeshMaterials)
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:
815
if ob.parent != object_list[parent]:
816
ob.parent = object_list[parent]
818
# pivot_list[ind] += pivot_list[parent] # XXX, not sure this is correct, should parent space matrix be applied before combining?
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)
828
def load_3ds(filepath,
830
IMPORT_CONSTRAIN_BOUNDS=10.0,
837
# if BPyMessages.Error_NoFile(filepath):
840
print("importing 3DS: %r..." % (filepath), end="")
842
if bpy.ops.object.select_all.poll():
843
bpy.ops.object.select_all(action='DESELECT')
846
# time1 = Blender.sys.time()
848
current_chunk = chunk()
850
file = open(filepath, 'rb')
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)
860
if IMPORT_CONSTRAIN_BOUNDS:
861
BOUNDS_3DS[:] = [1 << 30, 1 << 30, 1 << 30, -1 << 30, -1 << 30, -1 << 30]
867
# fixme, make unglobal, clear in case
868
object_dictionary.clear()
869
object_matrix.clear()
872
# scn = bpy.data.scenes.active
874
# SCN_OBJECTS = scn.objects
875
# SCN_OBJECTS.selected = [] # de select all
877
importedObjects = [] # Fill this list with objects
878
process_next_chunk(file, current_chunk, importedObjects, IMAGE_SEARCH)
880
# fixme, make unglobal
881
object_dictionary.clear()
882
object_matrix.clear()
884
# Link the objects into this scene.
885
# Layers = scn.Layers
887
# REMOVE DUMMYVERT, - remove this in the next release when blenders internal are fixed.
890
for ob in importedObjects:
891
if ob.type == 'MESH':
893
me.transform(ob.matrix_local.inverted())
895
# print(importedObjects)
897
for ob in importedObjects:
898
if ob.parent is None:
899
ob.matrix_world = ob.matrix_world * global_matrix
901
for ob in importedObjects:
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
913
grp = Blender.Group.New(name)
914
grp.objects = importedObjects
916
grp_ob = Object.New('Empty', name)
917
grp_ob.enableDupGroup = True
918
grp_ob.DupGroup = grp
920
grp_ob.Layers = Layers
923
# Select all imported objects.
924
for ob in importedObjects:
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():
939
if v[i] < BOUNDS_3DS[i]:
940
BOUNDS_3DS[i] = v[i] # min
942
if v[i] > BOUNDS_3DS[i + 3]:
943
BOUNDS_3DS[i + 3] = v[i] # min
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])
948
if max_axis < 1 << 30: # Should never be false but just make sure.
950
# Get a new scale factor if set as an option
952
while (max_axis * SCALE) > IMPORT_CONSTRAIN_BOUNDS:
956
SCALE_MAT = mathutils.Matrix.Scale(SCALE, 4)
958
for ob in importedObjects:
959
if ob.parent is None:
960
ob.matrix_world = ob.matrix_world * SCALE_MAT
962
# Done constraining to bounds.
964
# Select all new objects.
965
print(" done in %.4f sec." % (time.clock() - time1))
973
use_image_search=True,
974
use_apply_transform=True,
980
IMPORT_CONSTRAIN_BOUNDS=constrain_size,
981
IMAGE_SEARCH=use_image_search,
982
APPLY_MATRIX=use_apply_transform,
983
global_matrix=global_matrix,