1
$\begingroup$

I'm trying to make and export a file. I have a string which is basically the file content and I'd like to save this as a .dat file. So I need the user to click a button and the file explorer to open and then correctly save the file. I wasn't able to reference the file content itself on the following sample code:

class TEST_OT_export_tst(Operator, ExportHelper):
    bl_idname = 'test.export_tst'
    bl_label = 'test export test'
    bl_options = {'PRESET', 'UNDO'}
 
    filename_ext = '.dat'
    
    filter_glob: StringProperty(
        default='*.dat',
        options={'HIDDEN'}
    )
 
    def execute(self, context):
        print('exported file: ', self.filepath)
        return {'FINISHED'}
$\endgroup$
2
  • 1
    $\begingroup$ I'm not sure which part of the problem you are stuck on. To give the user a button, you might want to create a panel to contain the button and then add an operator to the layout that will invoke your test operator. See the panel in this answer for an example. But to invoke the file browser, you need your execute function to call the appropriate exporter. You could do that by invoking the existing export menu entry. Which are you looking for help with? $\endgroup$ Dec 4, 2021 at 18:35
  • $\begingroup$ My confusion is where do i input the actual life content? otherwise i would be exporting nothing... $\endgroup$
    – Paulo Kim
    Dec 4, 2021 at 19:58

1 Answer 1

1
$\begingroup$

Have a look into the templates (Text Editor -> Templates -> Python -> Operator Export). Straight after the user has selected the location and filename, the execute method of the operator is called. What do operator methods do? (poll, invoke, execute, draw & modal)

Complete example how to invoke the file dialog and write a .bat file according to the wikipedia entry: https://en.wikipedia.org/wiki/Batch_file.

import bpy

# https://en.wikipedia.org/wiki/Batch_file
def write_some_data(context, filepath):
    print("running write_some_data...")
    f = open(filepath, 'w', encoding='utf-8')
    f.write("@ECHO OFF\n")
    f.write("ECHO Hello World!\n")
    f.write(":: %s\n" % filepath)
    f.write(":: %s\n" % bpy.data.filepath)
    f.write("PAUSE")
    f.close()
    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.types import Operator

class ExportSomeData(Operator, ExportHelper):
    """This appears in the tooltip of the operator and in the generated docs"""
    bl_idname = "export_test.some_data"
    bl_label = "Export Some Data"

    # ExportHelper mixin class uses this
    filename_ext = ".bat"
    filter_glob: bpy.props.StringProperty(default="*.bat", options={'HIDDEN'}, maxlen=255)

    def execute(self, context):
        return write_some_data(context, self.filepath)

def register():
    bpy.utils.register_class(ExportSomeData)
def unregister():
    bpy.utils.unregister_class(ExportSomeData)

if __name__ == "__main__":
    register()
    bpy.ops.export_test.some_data('INVOKE_DEFAULT')

Output is:

@ECHO OFF
ECHO Hello World!
:: /Users/USER/Desktop/untitled.bat
:: /Users/USER/Desktop/untitled.blend
PAUSE
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .