23
$\begingroup$

I'm looking for an efficient way to get the current .blend file name, in a python script.

I know how to get its path, with bpy.path.abspath('//'), but it returns only the current path, without the file name. I'm sure it's possible, but I can't find out how.

$\endgroup$

3 Answers 3

35
$\begingroup$

It's in bpy.data.filepath.

This is the absolute path to the currently open blend file, see is_saved, and is_dirty too.

$\endgroup$
25
$\begingroup$

You can get the filename with bpy.path.basename(bpy.context.blend_data.filepath). This will return it as a string without the path attached. Use for Windows compatibility and some more info here.

$\endgroup$
0
$\begingroup$

If you run Python script directly from a Unix Shell with a Blender as interpreter you may use one of below:

#!blender -P import os,sys print("OS.PATH.ABSPATH(__FILE__): "+ os.path.abspath(__file__)) print("SYS.ARGV[-1]: " + sys.argv[-1]) 

os.path.abspath(__file__) will give you an absolute path (relpath() available as well).

sys.argv[-1] will give you a relative path.

$\endgroup$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.