0

I use Maya2014 + PyQt4. All my scripts are stored on the server. I have a paths:

## a place where all are read python scripts:
PYTHONPATH=X:\tools\Maya\windows\2014\python\
## a place for *.ui files:
# X:\tools\Maya\windows\2014\python\ui\
## a place for *.ico and *.png files:
XBMLANGPATH=X:\tools\Maya\windows\2014\icons\

When i run scripts from the maya python are no problems. But for download *.ui files i have to specify the full path:

form, base = uic.loadUiType('X:/tools/Maya/windows/2014/python/UI/lightSets.ui')

How i can load *.ui files automatically without specifying a full path? I have a lot of system variables and want to search in this file system paths. For example when i working in python without maya simply specify let 'ui/aaa.ui' and the file will be found as the starting point of the path is in the run python script and from subfolders and files already.

The second problem with the *.ui files. QtDesigner while maintaining creates such way to all the pictures:

<property name="icon">
  <iconset>
    <normaloff>../../icons/close_btn.png</normaloff>
    ../../icons/close_btn.png
  </iconset>
</property>

And when i load this file in maya - she can not find a picture of this relative paths. I believe that the problem again in the same way that the starting set is not known where and relative already obtained from him wrong.

Now I have to specify the full path to all images, making it difficult to change paths and transfer scripts. In general, the direct path to all icons and resources, this is a bad way. Tell me how to use into python maya relative paths?

For example I want to use this base path:

X:/tools/Maya/windows/2014/python/

And then have simply indicate relative paths for all that load from this folder. How to do it?

Massimo
  • 836
  • 3
  • 17
  • 38
  • i fix it... get path like: "path = os.path.dirname(unicode(os.path.abspath(__file__)))" and change working directory like: "os.chdir(path)" – Massimo May 13 '15 at 10:27

2 Answers2

0

If I understand correctly your post, this is my advice:

PYTHONPATH works for other stuff like importing modules, not arbitrary files passed as strings. I would leave the PYTHONPATH variable alone and set a new one. Then I would access the new variable from Python using:

import maya.mel
my_path = maya.mel.eval("getenv MY_NEW_PATH") 

And add it to each string path using os.join:

import os
form, base = uic.loadUiType(os.join(my_path, 'python/UI/lightSets.ui'))
  • and that if this variable does not contain only one path, and as a variable path that through semicolons contains a variety of ways? I understand you correctly, you are offering to have just my own environment variable, and from it has repelled? my scripts are stored in subfolders and transported to other places - in this case one variable is no longer suitable. I think there is a way to find out the same starting point from which the script was run. at least in Python this is done automatically, but why the Maya reassigns this way somewhere else and I can not even figure out where. – Massimo May 11 '15 at 08:26
  • os.getenv('PYTHONPATH') # Result: X:/tools/Maya/windows/2014/python/animation_manager/;X:/tools/SOFT/Pixar/RenderManStudio-18.0-maya2014/scripts;X:/tools/SOFT/Pixar/RenderManStudio-18.0-maya2014/rmantree/bin;X:/tools/Maya/windows/2014/python;;x:/tools/SOFT/Exocortex/Alembic/scripts;C:/Program Files/Pixar/RenderManStudio-18.0-maya2014/scripts;C:/Program Files/Autodesk/Maya2014/vray/scripts # – Massimo May 11 '15 at 08:28
  • Then, as suggested by @theodox, why don't you implement a search function that loops through all the possible paths until it finds a match? Like the ones in here: http://stackoverflow.com/a/1724723/3492244 – chris from local group May 11 '15 at 16:51
0

Getting familiar with os.walk() is a good idea here:

items = []
for directory in list_of_dirs:
    for root, _, files in os.walk(directory):
        for f in files:
            if f.endswith(".ui"):
               items.append(uic.load(os.join(root, f)

that will give you a list of form, base pairs for all the UI files anywhere under any of the directories in list_of_dirs

theodox
  • 12,028
  • 3
  • 23
  • 36
  • What if I do not even know the drive letter on which the search for folders with the elements? where to begin then? where to look for among the 10 mapped drives? I think it is necessary for a solution based on automatic search, not specifying where that is – Massimo May 11 '15 at 08:20
  • it will find *.ui files, and how to get the script to look for these icons in the *.ui? uic.loadUiType() - load a file and not find a images when it have a relative paths like ../../icons/close_btn.png – Massimo May 11 '15 at 09:58
  • You don't want to use `walk` on random drives: every time this runs you would be trolling huge amounts of files. You probably want to put a list of valid search locations into environment variables, and then read that into the lister. – theodox May 11 '15 at 19:32
  • Validating the uic files is a separate job, I'd write the importer to safely read the ones it can and to report errors or warnings as they occur. Validating all that data requires that you have a very good idea of what's supposed to be in the files, and it's much better to have a process where you can rely on the data being good and get a good error message when it's bad. – theodox May 11 '15 at 19:35
  • i find a simple method: pyPath = os.path.dirname(unicode(os.path.abspath(__file__), encoding)).replace('\\', '/') This method gives me the path where the file is launched. It's only solves the first problem - the starting directory for all files. But what about the relative paths to *.ui files? Reading the file and available for processing by the exposure. it is a function of uic.loadUiType(). If the file contains a relative path - it is not just a function of the image and displays. how to deal with this problem? how to make a function uic.loadUiType() to find pictures? – Massimo May 13 '15 at 07:17