How to set render filepath using blend filename in python Let say I have blend file called test01.blend, then I want to automatically assign render filepath via python to /media/data/render/[current blend filename]/[current blend filename]_.

So, it should look like this /media/data/render/test01/test01_

I'm trying to access it via bpy.data.filepath, but I got only full file path for current blend file not blend file name.

Thanks for your help
From iKlsR's answer:


  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.


You can then assign the render output path with it:

import bpy

filename = bpy.path.basename(bpy.data.filepath)
if filename:
    bpy.context.scene.render.filepath = filename


Note that basename will include the file extension (.blend). You can strip it using os.path.splittext():

import os
import bpy

filename = bpy.path.basename(bpy.data.filepath)
filename = os.path.splitext(filename)[0]

if filename:
    bpy.context.scene.render.filepath = os.path.join("/media/data/render", filename, filename + "_")