operator_file_export.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import bpy
  2. def write_some_data(context, filepath, use_some_setting):
  3. print("running write_some_data...")
  4. f = open(filepath, 'w', encoding='utf-8')
  5. f.write("Hello World %s" % use_some_setting)
  6. f.close()
  7. return {'FINISHED'}
  8. # ExportHelper is a helper class, defines filename and
  9. # invoke() function which calls the file selector.
  10. from bpy_extras.io_utils import ExportHelper
  11. from bpy.props import StringProperty, BoolProperty, EnumProperty
  12. from bpy.types import Operator
  13. class ExportSomeData(Operator, ExportHelper):
  14. """This appears in the tooltip of the operator and in the generated docs"""
  15. bl_idname = "export_test.some_data" # important since its how bpy.ops.import_test.some_data is constructed
  16. bl_label = "Export Some Data"
  17. # ExportHelper mixin class uses this
  18. filename_ext = ".txt"
  19. filter_glob: StringProperty(
  20. default="*.txt",
  21. options={'HIDDEN'},
  22. maxlen=255, # Max internal buffer length, longer would be clamped.
  23. )
  24. # List of operator properties, the attributes will be assigned
  25. # to the class instance from the operator settings before calling.
  26. use_setting: BoolProperty(
  27. name="Example Boolean",
  28. description="Example Tooltip",
  29. default=True,
  30. )
  31. type: EnumProperty(
  32. name="Example Enum",
  33. description="Choose between two items",
  34. items=(
  35. ('OPT_A', "First Option", "Description one"),
  36. ('OPT_B', "Second Option", "Description two"),
  37. ),
  38. default='OPT_A',
  39. )
  40. def execute(self, context):
  41. return write_some_data(context, self.filepath, self.use_setting)
  42. # Only needed if you want to add into a dynamic menu
  43. def menu_func_export(self, context):
  44. self.layout.operator(ExportSomeData.bl_idname, text="Text Export Operator")
  45. def register():
  46. bpy.utils.register_class(ExportSomeData)
  47. bpy.types.TOPBAR_MT_file_export.append(menu_func_export)
  48. def unregister():
  49. bpy.utils.unregister_class(ExportSomeData)
  50. bpy.types.TOPBAR_MT_file_export.remove(menu_func_export)
  51. if __name__ == "__main__":
  52. register()
  53. # test call
  54. bpy.ops.export_test.some_data('INVOKE_DEFAULT')