PYTHON MAYA convert string u'001' to 001?

113 views
Skip to first unread message

gnn

unread,
Apr 17, 2017, 9:29:59 AM4/17/17
to Python Programming for Autodesk Maya

did someone know how to convert this result: u'001 in : 001
To put it in a variable path: Z:\ProjectPath\001\
By advance, thanks a lot for your help!!


SBpath = 'G:\\ProjectPath\\'
import maya.cmds as cmds
import os, glob, time
   

def buttonLGTPressed(*args):
   
EpisodeName = `cmds.textField("Episode", query = True, text = True)`
   
print EpisodeName
    rootEp
= SBpath + "{}\\".format(EpisodeName)
   
print(rootEp) # EXPECTED: G:\ProjectPath\*\241\ # RESULT: G:\ProjectPath\u'241'\

window
= cmds.window(title = "Open Last scene", width = 150, height = 100)
cmds
.rowColumnLayout( numberOfColumns=2, columnAttach=(1, 'left', 0), columnWidth=[(1, 100), (2, 250)] )
cmds
.textField( "Episode", text = "001")
cmds
.button('OpenLGT', l="Open Last LGT scene", w=180, h=30, command=buttonLGTPressed)
cmds
.showWindow()



Robert White

unread,
Apr 17, 2017, 10:28:57 AM4/17/17
to Python Programming for Autodesk Maya
Try it without the backticks around cmds.textField.


SBpath = 'G:\\ProjectPath\\'
import maya.cmds as cmds
import os, glob, time
   

def buttonLGTPressed(*args):
   
EpisodeName = cmds.textField("Episode", query = True, text = True)
   
print EpisodeName
    rootEp
= SBpath + "{}\\".format(EpisodeName)
   
print(rootEp) # EXPECTED: G:\ProjectPath\*\241\ # RESULT: G:\ProjectPath\u'241'\

window
= cmds.window(title = "Open Last scene", width = 150, height = 100)
cmds
.rowColumnLayout( numberOfColumns=2, columnAttach=(1, 'left', 0), columnWidth=[(1, 100), (2, 250)] )
cmds
.textField( "Episode", text = "001")
cmds
.button('OpenLGT', l="Open Last LGT scene", w=180, h=30, command=buttonLGTPressed)
cmds
.showWindow()


Andres Weber

unread,
Apr 17, 2017, 5:00:32 PM4/17/17
to Python Programming for Autodesk Maya
You could also just cast to a string using str() around your EpisodeName but honestly the removal of back ticks makes more sense (in the future if you ever find you can't remove the leading 'u' otherwise).  Also since you're already using the os module you should be using os.path.join instead of the string concatenation you're currently doing to mitigate any possible formatting issues around file paths.

gnn

unread,
Apr 19, 2017, 6:52:16 PM4/19/17
to Python Programming for Autodesk Maya
Thank you very much for yours answers!

Jesse Kretschmer

unread,
Apr 22, 2017, 12:57:09 PM4/22/17
to Python Programming for Autodesk Maya
Howdy,
I'm glad you were able to get past your issue, however I see a couple issues in the code that are worth addressing still:
 - bacticks; Robert sent you in the right direction. Backticks are just a shorthand for repr() which is probably not what you want at all.
 - backslashes; Using backslashes in strings can get tricky; this is an escape character.

In python on windows you can actually use forward slashes for convenience so you could have 'G:/ProjectPath/' instead, and it would work. Rather than add the backslashes manually, I suggest using os.path.join() or os.path.sep rather than writing out the path with backslashes. To compare paths with different slashes you can just use os.path.normpath() in order to get the system path separator restored.

Here's how the join would look:
os.path.join('G:', 'ProjectPath')

Cheers,
Jesse
Reply all
Reply to author
Forward
0 new messages