How to get a script's home directory?

(rocketship) #1

There must be an easy answer to this question - how can I figure out what directory a script is in from within python?

RS

0 Likes

(Semmelb) #2

Hello,

take a look at “Blender.Get(…)”.
http://members.optusnet.com.au/cjbarton/BPY_API/Blender-module.html#Get
There are several keywords to get information about the current configuration.
Maybe you can find the information you need.

Semmelb

0 Likes

(rocketship) #3

It seems to only be info about directories you’ve already set in Blender…

It seems like a very simple thing - I can’t find anything in the Python docs, either!

0 Likes

(Duoas) #4
from Blender import Text

for t in Text.Get():
  print 'The text\'s name is', t.getName()
  print 'The text\'s filename is', t.getFilename()

Hope this helps.

[edit] Oh yeah, almost forgot. http://members.optusnet.com.au/cjbarton/BPY_API/Text-module.html

0 Likes

(Sanne) #5

I haven’t testet if it works inside Blender, but it should give you some ideas. Here’s a python script that should print out it’s location. Paste it to a new python file and start it with “python name_of_your_script.py” on the command line:

# Print out the absolute path to the script

import sys
import os

print sys.path[0] + os.sep + sys.argv[0]

sys.path[0] is the path to the script, os.sep is the separator, sys.argv[0] is the name of the script.

0 Likes

(Duoas) #6

Blender always reports sys.path and sys.argv as the path to the Blender executable. Hence, you’ll have to use the prescribed methods to get the script name and path.

0 Likes