8
$\begingroup$

I am writing a Quake 1 .BSP import script with python and I want to extract the textures from the .BSP so that the mesh can be textured. However, the textures are paletized (instead of a color, only an index into a palette is stored for each pixel) and the palette is not contained in the .BSP.

I'd like to include the palette in a file with my script, and load it from the script at run time. However, I don't know how to get the path to the palette file, so I can't load it. Is there a way to get the path my script is running in? I've tried using just the file name of the palette, but it didn't work.

p.s. I know I could store the palette in a python file, but I'd rather use the original .PAL format so that users can swap it for another palette if desired.

$\endgroup$

1 Answer 1

9
$\begingroup$

Python has a built-in __file__ global to access the path a script is running at. You can use it in your script by using this code snippet:

import os
script_directory = os.path.dirname(os.path.abspath(__file__))

WARNING: this will only work, i.e. will provide a correct path, only when passing python script using -P parameter, like this:

blender blenderfile.blend -P my_python_script.py

IF you would load that my_python_script.py into blender, then you will find that file has blenderfile.blend added as folder!

$\endgroup$
0

You must log in to answer this question.

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