1
$\begingroup$

In my Blender add-on in Python, I would like to be able to create a directory and then add it to the bookmark list in the File Browser. The code I wrote almost works. It creates a new directory and adds a bookmark of the current directory in the File Browser if it is visible in the GUI.

Here is my code:

import bpy
import os
project_path=os.path.join(r"C:\Users\Shari\Documents","New_Project")
if not os.path.exists(project_path):
    os.mkdir(project_path)
    os.chdir(project_path)
# add new project folder to bookmarks, but need context override
# got context_override code from:https://b3d.interplanety.org/en/context-override/
    override_context = bpy.context.copy()
    area = [area for area in bpy.context.screen.areas if area.type == "FILE_BROWSER"][0]
    override_context['window'] = bpy.context.window
    override_context['screen'] = bpy.context.screen
    override_context['area'] = area
    override_context['region'] = area.regions[-1]
    override_context['scene'] = bpy.context.scene
    override_context['space_data'] = area.spaces.active
    bpy.ops.file.select(override_context)
    bpy.ops.file.bookmark_add(override_context)
# change project_path so we don't get weird folder hierarchy
os.chdir(project_path)

I would like the 'New_Project' folder to appear in the bookmark list instead of whatever folder happens to be open in the File Browser.

$\endgroup$
2
  • $\begingroup$ It seems it's only possible using an open area since it relies on the currently selected file in the file browser, which is AFAIK not overridable. I think you can circumvent that by temporarily switching the area type to the file browser, navigate to the desired file, selecting it, then running the bookmark operator and switching the area back. Cumbersome. BTW if you're using 3.2 the new override syntax is with bpy.context.temp_override docs.blender.org/api/current/… $\endgroup$
    – Gorgious
    Commented Jun 14, 2022 at 6:01
  • $\begingroup$ Thanks. In my code, I found which area is the File Browser, but I don't know how to make it active or select a folder in Python. $\endgroup$
    – SJK
    Commented Jun 14, 2022 at 16:06

0

You must log in to answer this question.

Browse other questions tagged .