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"]
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.
32 Execute this script from the "File->Import" menu and choose a Raw file to
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
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
56 line_split
= list(map(float, line
.split()))
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
)]
71 for line
in file.readlines():
72 face
= line_to_face(line
)
78 # Generate verts and faces lists, without duplicates
84 for i
, v
in enumerate(f
):
88 index
= coords
[v
] = index_tot
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
))
103 def addMeshObj(mesh
, objName
):
104 scn
= bpy
.context
.scene
106 for o
in scn
.objects
:
110 nobj
= bpy
.data
.objects
.new(objName
, mesh
)
111 scn
.objects
.link(nobj
)
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
)
138 def invoke(self
, context
, event
):
139 wm
= context
.window_manager
140 wm
.add_fileselect(self
)
141 return {'RUNNING_MODAL'}
143 # package manages registering