0
$\begingroup$

After a couple of suggestions which I couldn't make work, I've decided to simplify my goal and question.

My original script would generate the images properly but I was limited to having it just spit them out into w/e folder the .blend file is in.

# write image
image.filepath_raw = "//BaseRough.png"
image.file_format = 'PNG'
image.save()

I've been trying to tell it to make a new folder, direct to that, and generate the images there, but it's not working.

This line:

bpy.ops.file.directory_new(directory='//textures/', open=False, confirm=True)

Yields the error: RuntimeError: Operator bpy.ops.file.directory_new.poll() failed, context is incorrect

I'm not sure how to give it context.

I also need it to be able to read the files and assign them to the appropriate nodes, though I assume changing the load path accordingly will do that.

img = bpy.data.images.load("//BaseRough.png", check_existing=True)
to:
img = bpy.data.images.load("//textures/BaseRough.png", check_existing=True)

Update

I've been scouring the internet looking for bits of code for this, as well as any kind of explanation how the os. system works in blender, and it led me eventually back around to this question.

I still have no idea how the window system works, but the code section on the bottom does actually work if I give it a file path to use.

Now my question is, how do I get it to use the local file's directory and add a folder to that?

os.getcwd()
'/home/monty'

# changed to:

os.getcwd()
'//'

Doesn't do anything at all. But there must be a way to define the path in a way it can see. If I can get that to work that will be good enough for now.

Someday I definitely do want to get the file window option to do the job, but clearly I'm not at that level yet.

Update

More digging has led me to this question, which inspired me to figure out this:

filepath = "//"
abs_filepath = bpy.path.abspath(filepath) # returns the absolute path
os.mkdir(str(abs_filepath+"textures"))

I'm so close I can taste it. Now I just need to write up a check for this so it's doesn't spit out FileExistsError: [Errno 17] File exists: 'textures'

$\endgroup$
4
  • $\begingroup$ Hello ! In the script editor you should look into Templates > Python > Operator File Import $\endgroup$
    – Gorgious
    Jul 25, 2022 at 6:24
  • $\begingroup$ I had a poke around in that template you mentioned, it hasn't helped me yet. See my update for the details. $\endgroup$ Jul 25, 2022 at 22:27
  • $\begingroup$ If your question is how to get the path that was picked by fileselect_add, you read self.filepath in the execute() function. Why are you creating a text object and then running it vs just calling a function (to which you could pass the path)? $\endgroup$
    – scurest
    Jul 25, 2022 at 22:57
  • $\begingroup$ I'm not creating a text object, it's just the only template I could find. Nobody else seems to want to do what I'm trying to do. I have a button on a menu that runs a script. That script makes 12 image files and assigns them to image nodes. At present, it just makes them in the directory with the active .blend file. I'm trying to add a way to make/choose a specific folder instead. $\endgroup$ Jul 25, 2022 at 23:32

1 Answer 1

0
$\begingroup$

Alright, I think this is the simplest possible way to make this happen, so here I go.

filepath = "//" # this line finds where the current .blend file is relative to itself
abs_filepath = bpy.path.abspath(filepath) # returns the absolute path
if not os.path.isdir(str(abs_filepath+"textures")): # checks whether the directory exists
    os.mkdir(str(abs_filepath+"textures")) # if it does not yet exist, makes it
# if it does exist, moves on to the rest of the script

After the above, the image generator can use //textures/imgname.png as both the destination, and to call the image and load it for use.

If anyone has a recommendation for the windowed file path chooser, and an explanation of how their recommendation works, I'd still like to try getting to that level.

$\endgroup$

You must log in to answer this question.

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