3

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

iKlsR
  • 43,379
  • 12
  • 156
  • 189
aditia
  • 970
  • 9
  • 18

1 Answers1

5

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 + "_")
gandalf3
  • 157,169
  • 58
  • 601
  • 1,133