#!BPY """ Name: '3D Studio (.3ds)...' Blender: 237 Group: 'Export' Tooltip: 'Export to 3DS file format (.3ds).' """ __author__ = ["Campbell Barton", "Bob Holcomb", "Richard Lärkäng", "Damien McGinnes"] __url__ = ("blender", "elysiun", "http://www.gametutorials.com") __version__ = "0.82" __bpydoc__ = """\ 3ds Exporter This script Exports a 3ds file and the materials into blender for editing. Exporting is based on 3ds loader from www.gametutorials.com(Thanks DigiBen). """ # ***** BEGIN GPL LICENSE BLOCK ***** # # Script copyright (C) Bob Holcomb # # 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 ***** # -------------------------------------------------------------------------- ###################################################### # Importing modules ###################################################### import Blender from Blender import NMesh, Scene, Object, Material import struct ###################################################### # Data Structures ###################################################### #Some of the chunks that we will export #----- Primary Chunk, at the beginning of each file PRIMARY= long("0x4D4D",16) #------ Main Chunks OBJECTINFO = long("0x3D3D",16); #This gives the version of the mesh and is found right before the material and object information VERSION = long("0x0002",16); #This gives the version of the .3ds file EDITKEYFRAME= long("0xB000",16); #This is the header for all of the key frame info #------ sub defines of OBJECTINFO MATERIAL=45055 #0xAFFF // This stored the texture info OBJECT=16384 #0x4000 // This stores the faces, vertices, etc... #>------ sub defines of MATERIAL MATNAME = long("0xA000",16); # This holds the material name MATAMBIENT = long("0xA010",16); # Ambient color of the object/material MATDIFFUSE = long("0xA020",16); # This holds the color of the object/material MATSPECULAR = long("0xA030",16); # SPecular color of the object/material MATSHINESS = long("0xA040",16); # ?? MATMAP = long("0xA200",16); # This is a header for a new material MATMAPFILE = long("0xA300",16); # This holds the file name of the texture RGB1= long("0x0011",16) RGB2= long("0x0012",16) #>------ sub defines of OBJECT OBJECT_MESH = long("0x4100",16); # This lets us know that we are reading a new object OBJECT_LIGHT = long("0x4600",16); # This lets un know we are reading a light object OBJECT_CAMERA= long("0x4700",16); # This lets un know we are reading a camera object #>------ sub defines of CAMERA OBJECT_CAM_RANGES= long("0x4720",16); # The camera range values #>------ sub defines of OBJECT_MESH OBJECT_VERTICES = long("0x4110",16); # The objects vertices OBJECT_FACES = long("0x4120",16); # The objects faces OBJECT_MATERIAL = long("0x4130",16); # This is found if the object has a material, either texture map or color OBJECT_UV = long("0x4140",16); # The UV texture coordinates OBJECT_TRANS_MATRIX = long("0x4160",16); # The Object Matrix #==============================================# # Strips the slashes from the back of a string # #==============================================# def stripPath(path): return path.split('/')[-1].split('\\')[-1] #==================================================# # New name based on old with a different extension # #==================================================# def newFName(ext): return Blender.Get('filename')[: -len(Blender.Get('filename').split('.', -1)[-1]) ] + ext #the chunk class class chunk: ID=0 size=0 def __init__(self): self.ID=0 self.size=0 def get_size(self): self.size=6 def write(self, file): #write header data=struct.pack(\ " 2] facenr=0 #fill in faces for face in valid_faces: #is this a tri or a quad num_fv=len(face.v) #it's a tri if num_fv==3: mesh.f_chunk.faces.append((face[0].index, face[1].index, face[2].index)) if (face.materialIndex < len(mesh.f_chunk.m_chunks)): mesh.f_chunk.m_chunks[face.materialIndex].faces.append(facenr) facenr+=1 else: #it's a quad mesh.f_chunk.faces.append((face[0].index, face[1].index, face[2].index)) # 0,1,2 mesh.f_chunk.faces.append((face[2].index, face[3].index, face[0].index)) # 2,3,0 #first tri if (face.materialIndex < len(mesh.f_chunk.m_chunks)): mesh.f_chunk.m_chunks[face.materialIndex].faces.append(facenr) facenr+=1 #other tri if (face.materialIndex < len(mesh.f_chunk.m_chunks)): mesh.f_chunk.m_chunks[face.materialIndex].faces.append(facenr) facenr+=1 #fill in the UV info if blender_mesh.hasVertexUV(): for vert in blender_mesh.verts: mesh.uv_chunk.uv.append((vert.uvco[0], vert.uvco[1])) elif blender_mesh.hasFaceUV(): for face in valid_faces: # Tri or quad. for uv_coord in face.uv: mesh.uv_chunk.uv.append((uv_coord[0], uv_coord[1])) #filled in our mesh, lets add it to the file primary.obj_info.obj_chunks[len(primary.obj_info.obj_chunks)-1].mesh_chunks.append(mesh) #check the size primary.get_size() #open the files up for writing file = open( filename, "wb" ) #recursively write the stuff to file primary.write(file) file.close() print "3ds export time: %.2f" % (Blender.sys.time() - time1) Blender.Window.FileSelector(save_3ds, "Export 3DS", newFName('3ds'))