Maya, PySide2 and relative paths

I’m having some problems with relative paths in Maya and PySide2.

I understand all Maya’s buttons are bundled in a file,
but since I don’t understand exactly how it works
why doesn’t using relative paths work for my own custom icons?

My custom icons that are inside custom defined icon paths works with maya.cmds

import maya.cmds as cmds

window = cmds.window()
cmds.rowColumnLayout( adjustableColumn=True )
cmds.iconTextButton( style='iconAndTextVertical', image1='orient_top.png', label='top' )
cmds.iconTextButton( style='iconAndTextVertical', image1='orient_front.png', label='front' )
cmds.iconTextButton( style='iconAndTextVertical', image1='orient_side.png', label='side' )

cmds.showWindow( window )

PySide2 example

does not work with my custom icon
btn1 = MyPushButton("my_icon.png")
or
btn1 = MyPushButton(":/my_icon.png")

works with Maya native icons
btn1 = MyPushButton(":/cube.png")

my solution in PySide2

get the path to the file itself

path = os.path.dirname(os.path.abspath(__file__))
# because I’m using modules I have an icon folder in every collection
btn1 = MyPushButton(path+"/icons/my_icon.png")

and while I am on the topic. Why is it that when I create a new shelf button, navigate to my button that is in an icon folder that I’ve added to Maya, it still shows the full absolute path so I manually have to go in and remove the whole folder path to just have the icon name left. I do this with a script, but anyway. Is there any other workaround for this?

As far as I know to be able to use relative paths for images with PySide you’d need to either build the path relative to __file__ as you show, or compile a qrc (xml file listing relative paths etc) to a python module using the pyside2-rcc executable that comes with PySide.

On Windows this would be something like:

>>> pyside2-rcc.exe .\my_resources.qrc -o my_resources.py

With this you can then import my_resources and reference whatever you’ve compiled to it using that format available with the builtin icons, eg
":/my_icon.png"

2 Likes

Ah thank you. Interesting. Very cool. And welcome! =)