Inverted slashes

import maya.cmds as cmds
from functools import partial
import os

script_dir = cmds.internalVar(userScriptDir=1)
LayerTree_dir = os.path.join (script_dir, ‘LayerTree’)
icons_dir = os.path.join (LayerTree_dir, ‘icons’)
file_name = os.path.join (icons_dir, ‘image_0.png’)

is printing

C:/Users/henry/Documents/maya/2020/scripts/LayerTree\icons\image_0.png

with inverted slashes, how to fix this?

Full code:

import maya.cmds as cmds
from functools import partial
import os

script_dir = cmds.internalVar(userScriptDir=1)
package_dir = os.path.join (script_dir, ‘package’)
icons_dir = os.path.join (package_dir, ‘icons’)
file_name = os.path.join (icons_dir, ‘image_0.png’)

def Base_layer(btn_num,*args):
items = [“Layer_1”, “Layer_2”, “Layer_3”, “Layer_4”]

if  cmds.objExists('Base_layer'):
        cmds.select('Base_layer')
        cmds.showHidden('Base_layer')
        update_Base_img(btn_num, 'image_1')


        for item in items:
            if cmds.objExists(item):
                cmds.hide(item)
            else:
                pass



else:
        cmds.rename('Mesh')
        cmds.duplicate(n='Base_layer')
        cmds.group(name='LayerTree')
        cmds.delete('Mesh')
        cmds.select('Base_layer')
        print ("Base Layer stored")
        update_Base_img(btn_num, 'image_1')

def update_Base_img(btn_num, status):
cmds.image(‘Base_{0}’.format(btn_num), e=1, i=’{0}.png’.format(status))

def myUI():

if(cmds.window('LayerTree', exists=True)):
     cmds.deleteUI("LayerTree", window=True)

UIwin = cmds.window("LayerTree", title="LayerTree v1.0", iconName = "LayerTree", sizeable=False, w = 250, h = 120)

cmds.rowColumnLayout(numberOfColumns=3, columnWidth=[(1, 100),(2, 55),(3, 80)], columnOffset = [(2,'right',3)] )

for i in range(1):
    cmds.button(label='Base layer', c=partial(Base_layer, i))
    cmds.image('Base_{0}'.format(i), i='image_0.png')


cmds.setParent("..")
cmds.showWindow(UIwin)

myUI()

Linux uses forward slashes as directory separators /, but Windows uses backslashes \.
Maya always uses forward slashes, no matter the operating system. Python uses the proper slashes for the current operating system when using os.path.join

The thing is, however, Windows will accept mixed slashes just fine in pretty much every case, so what you’re seeing there likely isn’t a problem at all. You can probably just not worry about it unless it’s causing an error.

If, however, you can’t bring yourself to stop worrying about it, os.path.normpath will conform a path to the current OS. So, if you’re in Windows, it will use all backslashes \.

Or you could just use the built in string replace method.

2 Likes

im finding an error, the file im calling for (image_0.png) isnt being found for some reason :frowning: ,when Im posting the full code above so you can check it out. The issue im mainly having is the image not being found, but once i use the C:/Users/henry/Documents/maya/2020/scripts/LayerTree\icons\image_0.png it does work and the image shows up

Did you try abspath?
Did you try just doing a string replacement?
If you’re getting errors, what are the errors?


On a side note, if you surround your code with three backticks it’ll show up nicer

```
def test():
print “Some code in here”
```

will turn into

def test():
    print "Some code in here"

got it working, it was an issue with the file I was working on, somehow i opened the wrong file and was saving and redoing. Its just like u said, os.path.join is absolute. Thanks for your help!