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 #####
22 "name": "Wavefront OBJ format",
23 "author": "Campbell Barton, Bastien Montagne",
25 "blender": (2, 74, 0),
26 "location": "File > Import-Export",
27 "description": "Import-Export OBJ, Import OBJ mesh, UV's, "
28 "materials and textures",
30 "wiki_url": "http://wiki.blender.org/index.php/Extensions:2.6/Py/"
31 "Scripts/Import-Export/Wavefront_OBJ",
32 "support": 'OFFICIAL',
33 "category": "Import-Export"}
37 if "import_obj" in locals():
38 importlib
.reload(import_obj
)
39 if "export_obj" in locals():
40 importlib
.reload(export_obj
)
44 from bpy
.props
import (
50 from bpy_extras
.io_utils
import (
53 orientation_helper_factory
,
59 IOOBJOrientationHelper
= orientation_helper_factory("IOOBJOrientationHelper", axis_forward
='-Z', axis_up
='Y')
62 class ImportOBJ(bpy
.types
.Operator
, ImportHelper
, IOOBJOrientationHelper
):
63 """Load a Wavefront OBJ File"""
64 bl_idname
= "import_scene.obj"
65 bl_label
= "Import OBJ"
66 bl_options
= {'PRESET', 'UNDO'}
69 filter_glob
= StringProperty(
70 default
="*.obj;*.mtl",
74 use_edges
= BoolProperty(
76 description
="Import lines and faces with 2 verts as edge",
79 use_smooth_groups
= BoolProperty(
81 description
="Surround smooth groups by sharp edges",
85 use_split_objects
= BoolProperty(
87 description
="Import OBJ Objects into Blender Objects",
90 use_split_groups
= BoolProperty(
92 description
="Import OBJ Groups into Blender Objects",
96 use_groups_as_vgroups
= BoolProperty(
98 description
="Import OBJ groups as vertex groups",
102 use_image_search
= BoolProperty(
104 description
="Search subdirs for any associated images "
105 "(Warning, may be slow)",
109 split_mode
= EnumProperty(
111 items
=(('ON', "Split", "Split geometry, omits unused verts"),
112 ('OFF', "Keep Vert Order", "Keep vertex order from file"),
116 global_clamp_size
= FloatProperty(
118 description
="Clamp bounds under this value (zero to disable)",
120 soft_min
=0.0, soft_max
=1000.0,
124 def execute(self
, context
):
125 # print("Selected: " + context.active_object.name)
126 from . import import_obj
128 if self
.split_mode
== 'OFF':
129 self
.use_split_objects
= False
130 self
.use_split_groups
= False
132 self
.use_groups_as_vgroups
= False
134 keywords
= self
.as_keywords(ignore
=("axis_forward",
140 global_matrix
= axis_conversion(from_forward
=self
.axis_forward
,
141 from_up
=self
.axis_up
,
143 keywords
["global_matrix"] = global_matrix
145 if bpy
.data
.is_saved
and context
.user_preferences
.filepaths
.use_relative_paths
:
147 keywords
["relpath"] = os
.path
.dirname((bpy
.data
.path_resolve("filepath", False).as_bytes()))
149 return import_obj
.load(self
, context
, **keywords
)
151 def draw(self
, context
):
154 row
= layout
.row(align
=True)
155 row
.prop(self
, "use_smooth_groups")
156 row
.prop(self
, "use_edges")
160 row
.prop(self
, "split_mode", expand
=True)
163 if self
.split_mode
== 'ON':
164 row
.label(text
="Split by:")
165 row
.prop(self
, "use_split_objects")
166 row
.prop(self
, "use_split_groups")
168 row
.prop(self
, "use_groups_as_vgroups")
170 row
= layout
.split(percentage
=0.67)
171 row
.prop(self
, "global_clamp_size")
172 layout
.prop(self
, "axis_forward")
173 layout
.prop(self
, "axis_up")
175 layout
.prop(self
, "use_image_search")
178 class ExportOBJ(bpy
.types
.Operator
, ExportHelper
, IOOBJOrientationHelper
):
179 """Save a Wavefront OBJ File"""
181 bl_idname
= "export_scene.obj"
182 bl_label
= 'Export OBJ'
183 bl_options
= {'PRESET'}
185 filename_ext
= ".obj"
186 filter_glob
= StringProperty(
187 default
="*.obj;*.mtl",
192 use_selection
= BoolProperty(
193 name
="Selection Only",
194 description
="Export selected objects only",
197 use_animation
= BoolProperty(
199 description
="Write out an OBJ for each frame",
204 use_mesh_modifiers
= BoolProperty(
205 name
="Apply Modifiers",
206 description
="Apply modifiers (preview resolution)",
211 use_edges
= BoolProperty(
212 name
="Include Edges",
216 use_smooth_groups
= BoolProperty(
217 name
="Smooth Groups",
218 description
="Write sharp edges as smooth groups",
221 use_smooth_groups_bitflags
= BoolProperty(
222 name
="Bitflag Smooth Groups",
223 description
="Same as 'Smooth Groups', but generate smooth groups IDs as bitflags "
224 "(produces at most 32 different smooth groups, usually much less)",
227 use_normals
= BoolProperty(
228 name
="Write Normals",
229 description
="Export one normal per vertex and per face, to represent flat faces and sharp edges",
232 use_uvs
= BoolProperty(
234 description
="Write out the active UV coordinates",
237 use_materials
= BoolProperty(
238 name
="Write Materials",
239 description
="Write out the MTL file",
242 use_triangles
= BoolProperty(
243 name
="Triangulate Faces",
244 description
="Convert all faces to triangles",
247 use_nurbs
= BoolProperty(
249 description
="Write nurbs curves as OBJ nurbs rather than "
250 "converting to geometry",
253 use_vertex_groups
= BoolProperty(
260 use_blen_objects
= BoolProperty(
261 name
="Objects as OBJ Objects",
265 group_by_object
= BoolProperty(
266 name
="Objects as OBJ Groups ",
270 group_by_material
= BoolProperty(
271 name
="Material Groups",
275 keep_vertex_order
= BoolProperty(
276 name
="Keep Vertex Order",
281 global_scale
= FloatProperty(
283 min=0.01, max=1000.0,
287 path_mode
= path_reference_mode
289 check_extension
= True
291 def execute(self
, context
):
292 from . import export_obj
294 from mathutils
import Matrix
295 keywords
= self
.as_keywords(ignore
=("axis_forward",
302 global_matrix
= (Matrix
.Scale(self
.global_scale
, 4) *
303 axis_conversion(to_forward
=self
.axis_forward
,
307 keywords
["global_matrix"] = global_matrix
308 return export_obj
.save(self
, context
, **keywords
)
311 def menu_func_import(self
, context
):
312 self
.layout
.operator(ImportOBJ
.bl_idname
, text
="Wavefront (.obj)")
315 def menu_func_export(self
, context
):
316 self
.layout
.operator(ExportOBJ
.bl_idname
, text
="Wavefront (.obj)")
320 bpy
.utils
.register_module(__name__
)
322 bpy
.types
.INFO_MT_file_import
.append(menu_func_import
)
323 bpy
.types
.INFO_MT_file_export
.append(menu_func_export
)
327 bpy
.utils
.unregister_module(__name__
)
329 bpy
.types
.INFO_MT_file_import
.remove(menu_func_import
)
330 bpy
.types
.INFO_MT_file_export
.remove(menu_func_export
)
332 if __name__
== "__main__":