2009-06-12 09:54:28 +00:00
|
|
|
import bpy
|
|
|
|
|
2009-06-16 08:54:38 +00:00
|
|
|
def write_obj(filepath, scene, ob):
|
|
|
|
out = open(filepath, 'w')
|
|
|
|
|
|
|
|
# create a temporary mesh
|
2009-06-18 19:59:20 +00:00
|
|
|
mesh = ob.create_render_mesh(scene)
|
2009-06-16 08:54:38 +00:00
|
|
|
|
|
|
|
# for vert in mesh.verts:
|
|
|
|
# ^ iterating that way doesn't work atm for some reason
|
|
|
|
|
|
|
|
for i in range(len(mesh.verts)):
|
|
|
|
vert = mesh.verts[i]
|
|
|
|
out.write('v {0} {1} {2}\n'.format(vert.co[0], vert.co[1], vert.co[2]))
|
|
|
|
|
|
|
|
for i in range(len(mesh.faces)):
|
|
|
|
face = mesh.faces[i]
|
|
|
|
out.write('f')
|
|
|
|
|
|
|
|
# but this works
|
|
|
|
for index in face.verts:
|
|
|
|
out.write(' {0}'.format(index + 1))
|
|
|
|
out.write('\n')
|
|
|
|
|
2009-06-18 19:59:20 +00:00
|
|
|
# delete mesh gain
|
|
|
|
bpy.data.remove_mesh(mesh)
|
2009-06-16 08:54:38 +00:00
|
|
|
|
|
|
|
out.close()
|
|
|
|
|
2009-06-12 09:54:28 +00:00
|
|
|
class SCRIPT_OT_export_obj(bpy.types.Operator):
|
2009-06-16 08:54:38 +00:00
|
|
|
'''A very basic OBJ exporter, writes only active object's mesh.'''
|
|
|
|
|
2009-06-12 09:54:28 +00:00
|
|
|
__label__ = 'Export OBJ'
|
|
|
|
|
|
|
|
# List of operator properties, the attributes will be assigned
|
|
|
|
# to the class instance from the operator settings before calling.
|
|
|
|
__props__ = [
|
2009-06-18 19:59:20 +00:00
|
|
|
bpy.props.StringProperty(attr="filename", name="filename")
|
2009-06-12 09:54:28 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
def debug(self, message):
|
|
|
|
print("{0}: {1}".format(self.__class__.__name__, message))
|
|
|
|
|
2009-06-18 19:59:20 +00:00
|
|
|
def execute(self, context):
|
2009-06-12 09:54:28 +00:00
|
|
|
self.debug("exec")
|
2009-06-15 08:12:28 +00:00
|
|
|
self.debug("filename = " + self.filename)
|
2009-06-15 13:49:02 +00:00
|
|
|
|
2009-06-16 08:54:38 +00:00
|
|
|
act = context.active_object
|
2009-06-15 13:49:02 +00:00
|
|
|
|
2009-06-16 08:54:38 +00:00
|
|
|
if act.type == 'MESH':
|
|
|
|
write_obj(self.filename, context.scene, act)
|
|
|
|
else:
|
|
|
|
self.debug("Active object is not a MESH.")
|
2009-06-15 13:49:02 +00:00
|
|
|
|
|
|
|
# XXX errors are silenced for some reason
|
|
|
|
# raise Exception("oops!")
|
|
|
|
|
2009-06-15 08:12:28 +00:00
|
|
|
return ('FINISHED',)
|
2009-06-12 09:54:28 +00:00
|
|
|
|
|
|
|
def invoke(self, context, event):
|
|
|
|
self.debug("invoke")
|
2009-06-18 19:59:20 +00:00
|
|
|
wm = context.manager
|
|
|
|
wm.add_fileselect(self.__operator__)
|
2009-06-16 08:54:38 +00:00
|
|
|
return ('RUNNING_MODAL',)
|
2009-06-12 09:54:28 +00:00
|
|
|
|
|
|
|
def poll(self, context): # poll isnt working yet
|
|
|
|
self.debug("poll")
|
|
|
|
return True
|
2009-06-18 19:59:20 +00:00
|
|
|
|
|
|
|
bpy.ops.add(SCRIPT_OT_export_obj)
|
|
|
|
|