1
$\begingroup$

I want a button to open a specific file, so I tried open_mainfile as an operator, but I cannot seem to pass the file as an argument for the operator using the row.operator("wm.open_mainfile").filepath solution. Here's the code I run from the Text Editor:

import bpy


class LayoutDemoPanel(bpy.types.Panel):
    """Creates a Panel in the scene context of the properties editor"""
    bl_label = "Layout Demo"
    bl_idname = "SCENE_PT_layout"
    bl_space_type = 'PROPERTIES'
    bl_region_type = 'WINDOW'
    bl_context = "scene"

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

        scene = context.scene

        # Create a simple row.
        layout.label(text="Open a file dialog:")
        row = layout.row()
        row.operator("wm.open_mainfile")
        
        layout.label(text="Open a file directly:")
        row = layout.row()
        row.operator("wm.open_mainfile").filepath = "C:/path/to/my/file.blend"


def register():
    bpy.utils.register_class(LayoutDemoPanel)


def unregister():
    bpy.utils.unregister_class(LayoutDemoPanel)


if __name__ == "__main__":
    register()

To me, both buttons open the same file dialog.

$\endgroup$

0

You must log in to answer this question.

Browse other questions tagged .