Retag for 2.54 beta release
[blender-addons.git] / io_mesh_raw / import_raw.py
blob1cbf0a312c294cdb9374659ec8b198e88325e76a
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 #####
19 __author__ = ["Anthony D'Agostino (Scorpius)", "Aurel Wildfellner"]
20 __version__ = '0.2'
21 __bpydoc__ = """\
22 This script imports Raw Triangle File format files to Blender.
24 The raw triangle format is very simple; it has no verts or faces lists.
25 It's just a simple ascii text file with the vertices of each triangle
26 listed on each line. In addition, a line with 12 values will be
27 imported as a quad. This may be in conflict with some other
28 applications, which use a raw format, but this is how it was
29 implemented back in blender 2.42.
31 Usage:<br>
32 Execute this script from the "File->Import" menu and choose a Raw file to
33 open.
35 Notes:<br>
36 Generates the standard verts and faces lists, but without duplicate
37 verts. Only *exact* duplicates are removed, there is no way to specify a
38 tolerance.
39 """
43 import bpy
45 # move those to a utility modul
46 from io_utils import unpack_face_list, unpack_list # TODO, make generic
49 def readMesh(filename, objName):
50 file = open(filename, "rb")
52 def line_to_face(line):
53 # Each triplet is an xyz float
54 line_split = []
55 try:
56 line_split = list(map(float, line.split()))
57 except:
58 return None
60 if len(line_split) == 9: # Tri
61 f1, f2, f3, f4, f5, f6, f7, f8, f9 = line_split
62 return [(f1, f2, f3), (f4, f5, f6), (f7, f8, f9)]
63 elif len(line_split) == 12: # Quad
64 f1, f2, f3, f4, f5, f6, f7, f8, f9, A, B, C = line_split
65 return [(f1, f2, f3), (f4, f5, f6), (f7, f8, f9), (A, B, C)]
66 else:
67 return None
70 faces = []
71 for line in file.readlines():
72 face = line_to_face(line)
73 if face:
74 faces.append(face)
76 file.close()
78 # Generate verts and faces lists, without duplicates
79 verts = []
80 coords = {}
81 index_tot = 0
83 for f in faces:
84 for i, v in enumerate(f):
85 index = coords.get(v)
87 if index is None:
88 index = coords[v] = index_tot
89 index_tot += 1
90 verts.append(v)
92 fi[i] = index
94 mesh = bpy.data.meshes.new(objName)
95 mesh.vertices.add(len(verts))
96 mesh.faces.add(len(faces))
97 mesh.vertices.foreach_set("co", unpack_list(verts))
98 mesh.faces.foreach_set("vertices_raw", unpack_face_list(faces))
100 return mesh
103 def addMeshObj(mesh, objName):
104 scn = bpy.context.scene
106 for o in scn.objects:
107 o.select = False
109 mesh.update()
110 nobj = bpy.data.objects.new(objName, mesh)
111 scn.objects.link(nobj)
112 nobj.select = True
114 if scn.objects.active == None or scn.objects.active.mode == 'OBJECT':
115 scn.objects.active = nobj
118 from bpy.props import *
120 class RawImporter(bpy.types.Operator):
121 '''Load Raw triangle mesh data'''
122 bl_idname = "import_mesh.raw"
123 bl_label = "Import RAW"
125 filepath = StringProperty(name="File Path", description="Filepath used for importing the RAW file", maxlen=1024, default="")
126 filename = StringProperty(name="File Name", description="Name of the file.")
128 def execute(self, context):
130 #convert the filename to an object name
131 objName = bpy.path.display_name(self.filename)
133 mesh = readMesh(self.filepath, objName)
134 addMeshObj(mesh, objName)
136 return {'FINISHED'}
138 def invoke(self, context, event):
139 wm = context.window_manager
140 wm.add_fileselect(self)
141 return {'RUNNING_MODAL'}
143 # package manages registering