a1ab4b5b54d59954d0935fed779e2801fbe64300
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 (shuber)",
12 "location": "File > Import > Import DEC Object File Format (.off)",
13 "description": "Import DEC Object File Format (.off) files",
15 "category": "Import/Export"
20 __author__
= "Stefan Huber"
21 #__url__ = ("blender", "blenderartists.org", "Author's homepage, http://www.redrival.com/scorpius")
22 #__version__ = "Part of IOSuite 0.5"
25 This script imports DEC Object File Format files to Blender.
27 The DEC (Digital Equipment Corporation) OFF format is very old and
28 almost identical to Wavefront's OBJ. I wrote this so I could get my huge
29 meshes into Moonlight Atelier. (DXF can also be used but the file size
30 is five times larger than OFF!) Blender/Moonlight users might find this
31 script to be very useful.
34 Execute this script from the "File->Import" menu and choose an OFF file to
38 Port to blender 2.5 beta 2. - shuber
39 UV Coordinate support has been added. - Scorpius
40 FGON support has been added. - Cam
41 New Mesh module now used. - Cam
45 from bpy
.props
import *
50 """Is this line a comment line?"""
60 """Read next line from file 'f' and ignore comments"""
61 line
= f
.readline().strip()
62 while isComment(line
):
63 line
= f
.readline().strip()
68 def unpack_vertices( vertices
):
75 def unpack_faces(faces
):
81 for k
in range(1, len(face
)-1):
83 tri
= [face
[0], face
[k
], face
[k
+1]]
85 # Rotate triangle, such that last index is not zero
87 tri
= [ tri
[2], tri
[0], tri
[1] ]
95 def importFile(filepath
, context
):
97 # List of vertices, a vertex is a 3-tuple (x,y,z)
99 # List of faces, a face is a list of indices within vertices
104 f
= open(filepath
, "r")
106 # Get the header line
107 line
= getNextLine(f
)
109 print("Error: header line does not start with 'OFF'.")
113 #Get number of vertices, faces and edges
114 line
= getNextLine(f
).split()
116 print("Error: Line of number of vertices, faces and edges is invalid.")
118 [numVertices
, numFaces
] = map(int, line
[0:2])
121 print("Error: Number of vertices is negative!")
124 print("Error: Number of faces is negative!")
128 for n
in range(numVertices
):
129 line
= getNextLine(f
).split()
131 print("Error: to few coordinates for vertex", n
)
134 vertices
+= [ list(map(float, line
[0:3])) ]
137 for n
in range(numFaces
):
138 line
= getNextLine(f
).split()
140 # Get number of vertices
141 lenFace
= int(line
[0])
143 if len(line
) < lenFace
:
144 print("Error: to few vertices for face", n
)
147 faces
+= [ list(map(int, line
[0:lenFace
])) ]
149 print("Finish reading file. Got %d vertices, %d faces." % \
150 (len(vertices
), len(faces
)) )
153 me
= bpy
.data
.meshes
.new("Mesh")
155 vertexdata
= unpack_vertices(vertices
)
156 facedata
= unpack_faces(faces
)
158 # Add given number of vertices and faces
159 me
.vertices
.add( len(vertexdata
)//3 )
160 me
.faces
.add( len(facedata
)//4 )
161 me
.vertices
.foreach_set("co", vertexdata
)
162 me
.faces
.foreach_set("vertices_raw", facedata
)
165 ob
= bpy
.data
.objects
.new("Mesh", me
)
167 scene
= context
.scene
169 scene
.objects
.link(ob
)
170 scene
.objects
.active
= ob
172 except Exception as e
:
173 print("Error reading .off file")
179 class ImportOffFile(bpy
.types
.Operator
):
180 '''Import DEC object file format (.off) files as specified by
181 http://shape.cs.princeton.edu/benchmark/documentation/off_format.html'''
183 bl_idname
= "import.off_files"
184 bl_label
= "Import DEC Object File Format (.off)"
185 bl_description
= "Imports DEC object file format (.off)"
186 bl_options
= {'REGISTER'}
188 filepath
= StringProperty(name
="File Path",
189 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
.add_fileselect(self
)
214 return {'RUNNING_MODAL'}
217 # Registering / Unregister
218 def menu_func(self
, context
):
219 self
.layout
.operator(ImportOffFile
.bl_idname
, \
220 text
="DEC Object File Format (.off)", icon
='PLUGIN')
223 bpy
.types
.INFO_MT_file_import
.append(menu_func
)
226 bpy
.types
.INFO_MT_file_import
.remove(menu_func
)
229 if __name__
== "__main__":