23

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.

iKlsR
  • 43,379
  • 12
  • 156
  • 189
Polosson
  • 6,544
  • 8
  • 33
  • 61

3 Answers3

33

It's in bpy.data.filepath.

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

brockmann
  • 12,613
  • 4
  • 50
  • 93
Adhi
  • 14,310
  • 1
  • 55
  • 62
24

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.

ideasman42
  • 47,387
  • 10
  • 141
  • 223
iKlsR
  • 43,379
  • 12
  • 156
  • 189
0

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.

CeDeROM
  • 141
  • 7