Post #5560: That’s a good approach. Also, thank you for the encouraging words.

📋 Metadata

🏷️ Tags

scripting import-export advanced solved

  • Python Scripting
  • Autorun Scripts
  • JSON Import/Export
  • Pie Menu Editor

💬 Content

That’s a good approach. Also, thank you for the encouraging words.

As you may have tried, PME has an internal operator called wm.pm_import that imports JSON files. You can call this with exec to meet your requirements, but there are some implementation considerations.

Implementation location: https://github.com/Pluglug/pie-menu-editor-fork/blob/7babd3423e9e114d6507680d47e2b22b139db448/preferences.py#L168

Key points:

  1. This operator cannot interpret relative paths, so you need to prepare the full path to the JSON file on the calling side
  2. The current implementation assumes it’s called from a file dialog, so dummy parameters need to be passed

※ However, I’ve modified it to work with direct filepath specification. This will be available from the next release (7babd34)

Implementation method:
This time, I’ll use autorun to also introduce PME’s features.

Note: Autorun executes every time Blender starts. I’ve created it to avoid issues, but if you have a dedicated initial setup project, please arrange and integrate it accordingly.

Please place the following script in the pie_menu_editor/scripts/autorun/ folder. It will load the settings file only when PME is in its initial state:

import bpy
import os

from pie_menu_editor.addon import ADDON_ID


def is_pme_default_state():
    """Check if PME is in initial default state"""
    try:
        prefs = bpy.context.preferences.addons[ADDON_ID].preferences
        menus = prefs.pie_menus
        return (
            len(menus) == 1
            and menus[0].name == "Pie Menu"
            and menus[0].mode == "PMENU"
            and all(pmi.mode == "EMPTY" for pmi in menus[0].pmis)
        )
    except:
        return False


def import_pme_settings(filepath, mode="REPLACE"):
    """Import PME settings using the correct operator parameters"""
    try:
        directory = os.path.dirname(filepath)
        filename = os.path.basename(filepath)

        result = bpy.ops.wm.pm_import(
            "EXEC_DEFAULT",
            filepath=filepath,  # Full path required
            directory=directory,
            files=[{"name": filename}],  # Required for PME-F 1.18.9 implementation
            mode=mode,
        )

        return result == {"FINISHED"}

    except Exception as e:
        print(f"Import error: {e}")
        return False


# Auto-import PME settings on first run
if is_pme_default_state():
    # JSON file requires full path, so we construct it here
    blender_dir = os.path.dirname(bpy.app.binary_path)
    settings_path = os.path.join(
        blender_dir, "portable", "addon_settings", "PME_menu_settings.json"
    )

    if os.path.exists(settings_path):
        # Remove the default Pie Menu if desired
        # pr = bpy.context.preferences.addons[ADDON_ID].preferences
        # while len(pr.pie_menus) > 0:
        #     pr.remove_pm()

        if import_pme_settings(settings_path):
            print("PME settings auto-imported successfully")
            # bpy.ops.wm.save_userpref() # if you want to save the preferences
        else:
            print("PME auto-import failed")
    else:
        print(f"Settings file not found: {settings_path}")
else:
    print("PME not in default state - skipping auto-import")

I hope this will be helpful to you.


❤️ 2 likes


🔗 View on Blender Artists