# Blender Python path directory for file output node

i'm trying to make a little script to change all file outputs node in my compositing scene, relatively to my file output render directory.

For example, if i render something in /Render/Combined/Combined_#### i want the file outputs nodes to create some folders in /Render/

So far, i've got this :

import bpy

import os

# Get absolute path:
filepath = bpy.context.scene.render.filepath
absolutepath = bpy.path.abspath(filepath)

# set the path for all file output nodes:
for scene in bpy.data.scenes:
for node in scene.node_tree.nodes:
if node.type == 'OUTPUT_FILE':
node.base_path = absolutepath


And it works, but i can't find a way to get to the "superior" directory... Is someone out here have some answers ? thank you very much !

• What do you mean by "superior" directory? – cmomoney Jan 4 '17 at 18:57
• I mean the parent directory, if my render output ils /render/combined my parent directory is /render – tonton Jan 4 '17 at 19:52
• So, using your example, you want to get the parent directory of absolutepath? – cmomoney Jan 4 '17 at 20:23
• yep exactly ! (sorry if i'm not clear, not really good in english) – tonton Jan 4 '17 at 22:00

You can just add ../.. at the end of your absolutepath, like this

node.base_path = absolutepath+'../..'


or if you insist on getting the actual path cleanly, you can use the python os module to split the path into parts and then join them again except the last two

import bpy
import os

# Get absolute path:
filepath = bpy.context.scene.render.filepath
absolutepath = bpy.path.abspath(filepath)

path = os.path.normpath(absolutepath)
parts = path.split(os.sep)

absolutepath = os.sep.join(parts[:-2])

# set the path for all file output nodes:
for scene in bpy.data.scenes:
for node in scene.node_tree.nodes:
if node.type == 'OUTPUT_FILE':
node.base_path = absolutepath


If you want this to work even if your original path has a filename in the end, add

absolutepath, file = os.path.split(absolutepath)


before path = os.path.normpath(absolutepath)

• this one is perfect too ! and the [:-2] give me control on the number of directory to "climb up" really good to know ! thanks for the quick answer ! i have one more question, i tried to use that and replace the last line by node.base_path = absolutepath + "\new directory" to create a new directory, but it doesn't work, do you have an idea ? – tonton Jan 4 '17 at 22:24
• I don't know if blender auto-creates directories. Use os.mkdir to create it first – qwazix Jan 5 '17 at 7:53
• i found a way (i think it's not the good one, but it works); when i create the name i add "\_" followed by the right name, but i had to keep the underscore, i don't know why but doesn't work without... Example : node.base_path = absolutepath + "\_" + "name of the new directory" – tonton Jan 5 '17 at 17:28
• That's weird... In my case it does not need the underscore, and when I put it there, I can see it in the created directory (mkdir not needed btw) – qwazix Jan 5 '17 at 21:03
• perhaps it's about being on windows ? anyway it allows the file output to be quickly recognizable so not a real problem ! thank you for your quick answers ! – tonton Jan 6 '17 at 10:06

Like this:

parent_dir = os.path.dirname(*your_path*)

• Might be useful for new Python coders to see the code in context with a bit of an explanation. – VRM Jan 4 '17 at 21:17