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

33
$\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$
23
$\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$
1
$\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$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.