6b7f9dcbdfb9fd0f2d146d1348c366551675a1e1
1 # io_import_off.py Stefan Huber
3 # Import DEC Object File Format (*.off)
7 "name": "Import DEC Object File Format (.off)",
8 "author": "Stefan Huber (shuber2@gmail.com)",
10 "blender": (2, 5, 57),
12 "location": "File > Import > Import DEC Object File Format (.off)",
13 "description": "Import DEC Object File Format (.off) files",
15 "category": "Import-Export"
19 This script imports DEC Object File Format files to Blender.
21 The DEC (Digital Equipment Corporation) OFF format is very old and
22 almost identical to Wavefront's OBJ. I wrote this so I could get my huge
23 meshes into Moonlight Atelier. (DXF can also be used but the file size
24 is five times larger than OFF!) Blender/Moonlight users might find this
25 script to be very useful.
28 Execute this script from the "File->Import" menu and choose an OFF file to
32 Port to blender 2.5 beta 2. - shuber
33 UV Coordinate support has been added. - Scorpius
34 FGON support has been added. - Cam
35 New Mesh module now used. - Cam
38 __version__
= '.'.join([str(s
) for s
in bl_info
['version']])
44 from bpy
.props
import *
49 """Is this line a comment line?"""
59 """Read next line from file 'f' and ignore comments"""
60 line
= f
.readline().strip()
61 while isComment(line
):
62 line
= f
.readline().strip()
67 def unpack_vertices( vertices
):
74 def unpack_faces(faces
):
80 for k
in range(1, len(face
)-1):
82 tri
= [face
[0], face
[k
], face
[k
+1]]
84 # Rotate triangle, such that last index is not zero
86 tri
= [ tri
[2], tri
[0], tri
[1] ]
94 def importFile(filepath
, context
):
96 # List of vertices, a vertex is a 3-tuple (x,y,z)
98 # List of faces, a face is a list of indices within vertices
103 f
= open(filepath
, "r")
105 # Get the header line
106 line
= getNextLine(f
)
108 print("Error: header line does not start with 'OFF'.")
112 #Get number of vertices, faces and edges
113 line
= getNextLine(f
).split()
115 print("Error: Line of number of vertices, faces and edges is invalid.")
117 [numVertices
, numFaces
] = map(int, line
[0:2])
120 print("Error: Number of vertices is negative!")
123 print("Error: Number of faces is negative!")
127 for n
in range(numVertices
):
128 line
= getNextLine(f
).split()
130 print("Error: to few coordinates for vertex", n
)
133 vertices
+= [ list(map(float, line
[0:3])) ]
136 for n
in range(numFaces
):
137 line
= getNextLine(f
).split()
139 # Get number of vertices
140 lenFace
= int(line
[0])
142 if len(line
) < lenFace
:
143 print("Error: to few vertices for face", n
)
146 faces
+= [ list(map(int, line
[0:lenFace
])) ]
148 print("Finish reading file. Got %d vertices, %d faces." % \
149 (len(vertices
), len(faces
)) )
152 me
= bpy
.data
.meshes
.new("Mesh")
154 vertexdata
= unpack_vertices(vertices
)
155 facedata
= unpack_faces(faces
)
157 # Add given number of vertices and faces
158 me
.vertices
.add( len(vertexdata
)//3 )
159 me
.faces
.add( len(facedata
)//4 )
160 me
.vertices
.foreach_set("co", vertexdata
)
161 me
.faces
.foreach_set("vertices_raw", facedata
)
164 ob
= bpy
.data
.objects
.new("Mesh", me
)
166 scene
= context
.scene
168 scene
.objects
.link(ob
)
169 scene
.objects
.active
= ob
171 except Exception as e
:
172 print("Error reading .off file")
178 class IMPORT_OT_dec_off(bpy
.types
.Operator
):
179 '''Import DEC object file format (.off) files as specified by
180 http://shape.cs.princeton.edu/benchmark/documentation/off_format.html'''
182 bl_idname
= "import_scene.dec_off"
183 bl_label
= "Import DEC off"
184 bl_description
= "Imports DEC object file format (.off)"
185 bl_options
= {'REGISTER'}
187 filepath
= StringProperty(name
="File Path",
188 description
="Filepath used for importing the file",
194 ('*', 'All image formats', 'Import all know image (or movie) formats.'),
195 ('off', 'OFF (.off)', 'Object File Format') ]
197 extension
= EnumProperty(name
="Extension",
198 description
="Only import files of this type.",
201 def execute(self
, context
):
204 filepath
= self
.properties
.filepath
206 importFile(filepath
, context
)
210 def invoke(self
, context
, event
):
211 wm
= bpy
.context
.window_manager
212 wm
.fileselect_add(self
)
214 return {'RUNNING_MODAL'}
217 # Registering / Unregister
218 def menu_func(self
, context
):
219 self
.layout
.operator(IMPORT_OT_dec_off
.bl_idname
,
220 text
="DEC Object File Format (.off)",
224 bpy
.utils
.register_module(__name__
)
225 bpy
.types
.INFO_MT_file_import
.append(menu_func
)
228 bpy
.utils
.unregister_module(__name__
)
229 bpy
.types
.INFO_MT_file_import
.remove(menu_func
)
232 if __name__
== "__main__":