3
$\begingroup$

I'm writing an export script and I want the user to be able to export the currently selected object. The real script is finished but now I want to finish my UI. For this I want to display a fileselector like this:

Example for a working fileselector

So here is what I got so far:

class MHMDLExporterObject(Panel):
    """
    This exports the current object
    """

    bl_label = "Export My Stuff"
    bl_space_type = "PROPERTIES"
    bl_region_type = "WINDOW"
    bl_context = "object"

    # can I let it write into this property?
    dir = StringProperty(name="Export path", default="C:/tmp/",
                                description="Export which objects?",
                                subtype='DIR_PATH')

    def draw(self, context):
        from .constants import OP_exportMHMUIdent
        layout = self.layout
        col = layout.column()
        from .constants import uvResultIdent
        if hasattr(context.object, '["%s"]' % uvResultIdent):
            uvL = col.prop(context.object, '["%s"]' % uvResultIdent, text="UV Layer")
            uvL.enabled = False
        else: #property doesn't exist
            col.label('To preselect an uv-layer to export') 
            col.label('you have to create a Custom Property')
            col.label('with the name %s.' % uvResultIdent)
        from .constants import OP_uvPromtpIdent
        col.operator(OP_uvPromtpIdent, text='Select the uv-layer to export')
        ## here is wher I want to get display a fileselector that writes the
        ## current value in self.dir (see above)
$\endgroup$
  • $\begingroup$ Wouldn't it be more appropriate to use a real file selector (file browser) like other addons offer? You could also let the user export multiple objects at once, and build the output filepath e.g. based on object names. $\endgroup$ – CoDEmanX Jan 26 '14 at 15:28
  • $\begingroup$ Yes and no... it's kind of a utility thing to also offer an export button in the Properties > Object and as you see, it's not that difficult. Of course the other method exists but the fileselector always defaults to some location the user may not want so why not? $\endgroup$ – WorldSEnder Jan 26 '14 at 15:38
  • $\begingroup$ Sure, if that suites your needs. The default path for the file selector can be overwritten however, see my answer below: blender.stackexchange.com/a/6541/1363 $\endgroup$ – CoDEmanX Jan 26 '14 at 16:54
2
$\begingroup$

I suggest to use a full file selector dialog. It can be called and default path be set like:

    def invoke(self, context, event):
        self.filepath = "C:\\path\\name_of_file.ext"
        context.window_manager.fileselect_add(self)
        return {'RUNNING_MODAL'}
$\endgroup$
  • $\begingroup$ You don't actually have to extend ExportHelper. If you look at the source, all he does is checking self.filepath and then invoking context.window_manager.fileselect_add(self). There's no need for super().invoke(...) So all one has to do is assigning self.filepath and then calling context.window_manager.fileselect_add(self). $\endgroup$ – WorldSEnder Jan 26 '14 at 17:35
  • $\begingroup$ You're right, I called the super function to ensure the file extension is checked on invocation, but it actually works without. Edited answer. $\endgroup$ – CoDEmanX Jan 26 '14 at 19:40
2
$\begingroup$

It's not possible to use a path variable from inside the Panel to achieve this effect. The fact that the used path is either used per object or per scene on the other hand offers a great workaround:

In your __init__.py-file you need the following code:

from bpy.props import StringProperty

def register():
    bpy.utils.register_module(__name__)

    # assigns a propery someName to every object. 
    # If you want one var per scene use bpy.types.Scene
    bpy.types.Object.someName = StringProperty(name="Exported Layer", 
        description="The UVLayer that will get exported",
        subtype='DIR_PATH') # so blender notices that as
                            # a directory path

if __name__ == "__main__":
    register()

Now you can use that variable inside yor panel:

from bpy.types import Panel

class MyExporter(Panel):
    """
    This exports the current object
    """

    bl_label = "Mobster Hunter"
    bl_space_type = "PROPERTIES"
    bl_region_type = "WINDOW"
    bl_context = "object"

    def draw(self, context):
        layout = self.layout
        col = layout.column()

        # use either context.object or context.scene and 
        # the same name
        col.prop(context.object, 'someName', text='Export directory')
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.