Skip to content

Instantly share code, notes, and snippets.

@Eiyeron
Created July 24, 2018 21:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Eiyeron/d8c05fe46c31101e5e42f303d4a4b848 to your computer and use it in GitHub Desktop.
Save Eiyeron/d8c05fe46c31101e5e42f303d4a4b848 to your computer and use it in GitHub Desktop.
import bpy
def write_some_data(context, filepath, use_some_setting):
print("running write_some_data...")
with open(filepath, 'w', encoding='utf-8') as f:
current_object = bpy.context.scene.objects.active
mesh = current_object.data
f.write("local v = {\n")
for i,vertex in mesh.vertices.items():
x,y,z = vertex.co
f.write("{{{:5f}, {:5f}, {:5f}}},\n".format(x,y,z))
f.write("}\n")
f.write("local f = {\n")
for i,face in mesh.polygons.items():
a,b,c = face.vertices
color_index = current_object.material_slots[face.material_index].material.get('pico8_index', 7)
f.write("{{{}, {}, {}, {}}},\n".format(a+1, b+1, c+1, color_index))
f.write("}\n")
return {'FINISHED'}
# ExportHelper is a helper class, defines filename and
# invoke() function which calls the file selector.
from bpy_extras.io_utils import ExportHelper
from bpy.props import StringProperty, BoolProperty, EnumProperty
from bpy.types import Operator
class ExportSomeData(Operator, ExportHelper):
"""This appears in the tooltip of the operator and in the generated docs"""
bl_idname = "export.pico8"
bl_label = "Export to Pico-8"
# ExportHelper mixin class uses this
filename_ext = ".txt"
filter_glob = StringProperty(
default="*.txt",
options={'HIDDEN'},
maxlen=255, # Max internal buffer length, longer would be clamped.
)
)
def execute(self, context):
return write_some_data(context, self.filepath)
# Only needed if you want to add into a dynamic menu
def menu_func_export(self, context):
self.layout.operator(ExportSomeData.bl_idname, text="Text Export Operator")
def register():
bpy.utils.register_class(ExportSomeData)
bpy.types.INFO_MT_file_export.append(menu_func_export)
def unregister():
bpy.utils.unregister_class(ExportSomeData)
bpy.types.INFO_MT_file_export.remove(menu_func_export)
if __name__ == "__main__":
register()
# test call
bpy.ops.export.pico8('INVOKE_DEFAULT')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment