How to make a path universal

How to make a path like C:\Users\henry\Documents\maya\2020\prefs\icons\LayerTree universal for me to use? so if anyone gets to use this script, all they need to do is just place the icon folder inside prefs\icons

Example:

import maya.cmds as cmds
from functools import partial

for i in range(1):
cmds.button(label=‘Base layer’, c=partial(Base_layer, i))
cmds.image(‘Base_{0}’.format(i), i=“C:\Users\henry\Documents\maya\2020\prefs\icons\LayerTree”)

Because of the way the code is composed, i cant use cmds.internalVar (upd = True) + icons\LayerTree\

so, how to approach this with cmds.image directly?

I think the way most people do it is by using relative paths. In python the variable __file__ holds the path to the current file if the file was imported. So you can do something like this, assuming the script is in the scripts folder

import os

scriptFolder = os.path.dirname(__file__)
userFolder = os.path.dirname(scriptFolder)
layerTreeFolder = os.path.join(userFolder, 'prefs', 'icons', 'LayerTree')

However, it would probably be even better if you made a folder for your script, and put your icons and .py files somewhere under that folder rather than in prefs. That way you don’t have to worry about where all of the parts go, so long as they stay together in the way you made them.

Thank you! :slight_smile: