1
$\begingroup$

I have two buttons in my panel, one to import obj and one to import collada. I have two folders named 'obj_files' and 'collada_files' in two locations on my machine. I'm using importhelper, but I want when I press on the Import OBJ button, the obj_files folder will be the default path which shows up which is the location where I stored all the obj files in it instead of browsing for it, and the collada_files path when pressing the import collada button. I've tried

bpy.ops.import_scene.obj(filepath = '/Users/UserA/Temp/obj_files/')

I've also tried the below but still didn't work:

class ImportOBJ(Operator, ImportHelper):
    """Import OBJ"""
    bl_idname = "io.importobj"  
    bl_label = "Import OBJ"

    filename_ext = ".obj"
    filter_glob = StringProperty(
            default="*.obj",
            options={'HIDDEN'},
            maxlen=255,
            )
    filepath = bpy.props.StringProperty(
        name="File Path", 
        description="File path used for importing the OBJ file", 
        maxlen= 1024,
        default= "/Users/UserA/Temp/obj_files/")

def execute(self, context):
    bpy.ops.import_scene.obj(filepath = self.properties.filepath)
$\endgroup$
2
$\begingroup$

My guess is you want the filebrowser to open at this location. My suggestion on this would be to set up default path properties in your addon preferences (if an addon).

class ImportDefaultPathsAddon(AddonPreferences):
    # this must match the addon name, use '__package__'
    # when defining this in a submodule of a python package.
    bl_idname = __name__

    default_obj_path = StringProperty(subtype='DIR_PATH')
    default_collada_path = StringProperty(subtype='DIR_PATH')

    def draw(self, context):
        layout = self.layout
        layout.label(text="Import Paths")
        layout.prop(self, "default_obj_path")
        layout.prop(self, "default_collada_path")

Then in your layouts, menu or panel button

def draw(self, context):
    user_preferences = context.user_preferences
    addon_prefs = user_preferences.addons[__name__].preferences

    layout = self.layout
    op = layout.operator("import_scene.obj")
    op.filepath = addon_prefs.default_obj_path

    op = layout.operator("import_scene.collada")
    op.filepath = addon_prefs.default_collada_path

The user would need to set the paths when the addon is enabled.

PS I've looked at other methods of doing this, and can also suggest ditching the ImportHelper subclass altogether and write your own invoke and parameters.

$\endgroup$
  • $\begingroup$ Thank you. So I can't put the default filebrowser path hardcoded? as I don't want the user to enter it himself. Could you explain the other methods please? $\endgroup$ – Tak May 11 '17 at 8:20
1
$\begingroup$

There are a couple of ways to do this, but as batFinger said, just ditch the ImporterHelper and write your own invoke. A simple way is to just set the file path in the invoke:

class ImportOBJ(Operator, ImportHelper):
    """Import OBJ"""
    bl_idname = "io.importobj"  
    bl_label = "Import OBJ"

    filename_ext = ".obj"
    filter_glob = StringProperty(
            default="*.obj",
            options={'HIDDEN'},
            maxlen=255,
            )
    filepath = bpy.props.StringProperty(
        name="File Path", 
        description="File path used for importing the OBJ file", 
        maxlen= 1024)

    def execute(self, context):
        bpy.ops.import_scene.obj(filepath = self.properties.filepath)

    def invoke(self, context, event):
        self.filepath = "/Users/UserA/Temp/obj_files/"
        wm = context.window_manager.fileselect_add(self)
        return {'RUNNING_MODAL'}
$\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.