Help Not Hard-coding directories

Hi.

Sorry if topic doesnt make much sense.

Working on a little project in maya, python and pyqt. I have my script located in a folder and within that folder there is also the ui file.

I am trying to figure out a way so that as long as they are both in the same folder it will be able to find the ui file.

I tried using os.getcwd() put that just gives me the directory that maya is running in.

I am importing the script into maya to run it, but not sure how to go about finding the ui file which is in the same folder as the imported script.

I dont want to hardcode it as running it on several different machines and there all in different places.

I’m sure its really simple but I cant seem to figure it out at the moment.

Cheers.

you can try using the file attribute, which will give you back a string that is the filename of the script, ie:

# Save this as <some_file>.py
import os
def return_this_path():
     return os.path.dirname(__file__)

import <some_file>
<some_file>.return_this_path()

Another option is to use the path attribute if you’re setting up a package, then when you import your package or any of the subpackages you can just look on <package>.path. I’d probably recommend using file method, as i don’t know the specifics of your setup.

Many thanks for the reply, I am using a package at the moment simply to keep things clean as there will be several projects within one folder.

I’ll give it a try as soon as I reinstall maya, its decided not to play nice for some reason.

Cheers.

Finally managed to get maya working again, darn new anti-malware killed it.

It seems to work great, used it in conjunction with os.path.join() and used it within the script itself so its run when the script is imported.

Thanks again, will test on Laptop tomorrow.