Creating a file import dialog

Hi all,
first of all I’m new to Blender coding, so apologies in advance if the question is too obvious.

I’m trying to write an addon that loads an XML file with some additional info that I’m using in my work, that info is applied to objects in the scene.
The XML parsing and usage is ready but at the moment, it’s just a script I’m running and I need to type the path to the XML in the script. I want to turn it into something resembling an addon, that has a file-browser dialog so I can select the XML file. possibly one that is registered as a file import.

Update: I found the ImportHelper template and some pages that explain it to some degree but due to my beginner status in python I’m having trouble implementing it. I got as far as registering the addon, and when running from a script, I do have a file browser and everything works.
The bit that I’m missing, I think, is getting it to show in the file–>import list. how do I do that? thanks.

Thanks!

If you are referring to the addon filter:

This is based on the addon info:

bl_info = {
    "name": "Export Blend",
    "author": "Nezumi",
    "description": "Export as blend file",
    "blender": (3, 1, 2),
    "version": (0, 0, 1),
    "location": "View3D > UI > Export Blend",
    "warning": "",
    "wiki_url": "",
    "category": "Import-Export",
    "support": "TESTING",
}

Specifically the “category” setting.

You need to add a custom layout to the relevant menu class. The layout can be a single operator button, eg.

def menu_func_import(self, context):
    self.layout.operator(YourOperator.bl_idname)

def register():
    bpy.types.TOPBAR_MT_file_import.append(menu_func_import)

Thanks! I already have that info, and it was already showing in the addon list after I installed the addon, and I was able to activate it by checking the box next to it.
The thing I still can’t get to work is it showing up in the File->Import list along with other formats. I tried copying the relevant lines from the File Import template and modyfying the specifics, but as a beginner (I’m transitioning from 3ds Max’s Maxscript which means I have to learn both Python AND blender internals) there are still many things I don’t quite understand in that process.

As @Gorgious mentioned above if you are referring to adding to the menu you need to append to the existing menu.

this is from the OBJ import export init.py file:

Untitled

2 Likes

Okay, I figured it out. I made some mistake in bl_idname and it didn’t get registered.
I still don’t quite understand what all those things do but it’s there in the least. Thanks everyone!