1
$\begingroup$

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 !

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

2 Answers 2

1
$\begingroup$

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)

$\endgroup$
5
  • $\begingroup$ 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 ? $\endgroup$
    – tonton
    Commented Jan 4, 2017 at 22:24
  • $\begingroup$ I don't know if blender auto-creates directories. Use os.mkdir to create it first $\endgroup$
    – qwazix
    Commented Jan 5, 2017 at 7:53
  • $\begingroup$ 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" $\endgroup$
    – tonton
    Commented Jan 5, 2017 at 17:28
  • $\begingroup$ 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) $\endgroup$
    – qwazix
    Commented Jan 5, 2017 at 21:03
  • $\begingroup$ 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 ! $\endgroup$
    – tonton
    Commented Jan 6, 2017 at 10:06
0
$\begingroup$

Like this:

parent_dir = os.path.dirname(*your_path*)
$\endgroup$
1
  • 2
    $\begingroup$ Might be useful for new Python coders to see the code in context with a bit of an explanation. $\endgroup$
    – J Sargent
    Commented Jan 4, 2017 at 21:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .