0
$\begingroup$

I wrote my save function so it automatically changes its name based on the date and I want it to be added to the Recent Files menu. When I save my file via python like this:

import bpy
import getpass


filepath = "C:\\Users\\" + getpass.getuser() + "\\Desktop\\"
name = 'test'
bpy.ops.wm.save_as_mainfile(filepath = filepath + name + '.blend')

The file will not be added to the Recent Files menu, but when I save it manually via the File > Save command it will be added the Recent Files menu.

It does not change even if I immediately open the newly saved scene via:

bpy.ops.wm.open_mainfile(filepath = filepath + name + '.blend')

Is there a way to add my scene, that was saved by script, to the Recent Files menu?

$\endgroup$

3 Answers 3

1
$\begingroup$

Blender stores your recent files in a txt file, [1][2] located in your OS' .config/blender/version/config/ folder. So once you save it, you just manually prepend the filepath to the beginning of that txt file and reread it in.

import bpy
from pathlib import Path

# recent files stored in a text file in your .config folder

recent_files = "C:\\Users\\<Userbane>\\AppData\\Roaming\\Blender Foundation\\Blender\\<your-blender-version>\\config\\recent-files.txt" 

save_path = Path('C:\\Desktop\\my_blend.blend')
bpy.ops.wm.save_mainfile(filepath=save_path)

# add the blendfile path to the recent-files.txt
with open(recent_files, 'r+') as f:
    contents = f.read()
    f.seek(0,0) # go to beginning of file
    if save_path.is_file():
        newfile = save_path.resolve().rstrip('\n\r') + "\n" + contents
        f.write(newfile)
    else:
        print("File did not save correctly.")

# reread file history    
bpy.ops.wm.read_history()
$\endgroup$
0
$\begingroup$

Here's an alternate version that uses the %APPDATA% variable on windows instead of getpass and fixes the blenderVersionFolderName variable scope error.

import bpy
from pathlib import Path
import os

new_filename = "C:\\some\\path\\foo.blend"
clean_filename = bpy.path.native_pathsep(new_filename)
bpy.ops.wm.save_as_mainfile(filepath=clean_filename)


# add the blendfile path to the recent-files.txt
blenderVersion = bpy.app.version_string
blenderVersionFolderName = "3.2"
if blenderVersion[-1] == '0':
    blenderVersionFolderName = blenderVersion[:-1]
if blenderVersionFolderName[-1] == '.':
    blenderVersionFolderName = blenderVersionFolderName[:-1]
recent_files = os.getenv('APPDATA') + '\\Blender Foundation\\Blender\\' + blenderVersionFolderName + '\\config\\recent-files.txt'
save_path = Path(clean_filename)
with open(recent_files, 'r+') as f:
    contents = f.read()
    f.seek(0,0) # go to beginning of file
    if save_path.is_file():
        newfile = str(save_path.resolve()) + '\n' + contents
        f.write(newfile)
    else:
        print("File did not save correctly.")
bpy.ops.wm.read_history()
$\endgroup$
0
$\begingroup$

Based on Jakemoyo's answer. Changed a bit to work for Windows and getting username and Blender version by itself:

import bpy
from pathlib import Path
import getpass

blenderVersionFolderName = '3.3'
blenderVersion = bpy.app.version_string
if blenderVersion[-1] == '0':
    blenderVersionFolderName = blenderVersion[:-1]
if blenderVersionFolderName[-1] == '.':
    blenderVersionFolderName = blenderVersionFolderName[:-1]

recent_files = 'C:\\Users\\' + getpass.getuser() + '\\AppData\\Roaming\\Blender Foundation\\Blender\\' + blenderVersionFolderName + '\\config\\recent-files.txt'

save_path = Path('C:\Desktop\my_blend.blend')
bpy.ops.wm.save_mainfile(filepath=save_path)

# add the blendfile path to the recent-files.txt
with open(recent_files, 'r+') as f:
    contents = f.read()
    f.seek(0,0) # go to beginning of file
    if save_path.is_file():
        newfile = str(save_path.resolve()) + '\n' + contents
        f.write(newfile)
    else:
        print("File did not save correctly.")

# reread file history    
bpy.ops.wm.read_history()
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .