1

I use Maya to render my cartoon, and got a problem when I programming with python. Maya gives me an string variable which is a path, and I want to convert it to a normalized path. However, when it comes to '\b', I totally failed to play with it. Here's the problem:

I want to convert the string in paths whose value looks like 'D:\notebook\bot' to 'D/notebook/bot':

paths = ['D:\notebook\bot', 'D:\newworld']
for p in paths:
    p = '{0!r}'.format(p).replace('\\', '/').replace(':','')
    print p

The output is :

D/notebook/x08ot
D/newworld

See, the \n is correctly printed out as /n, however, \b is being escaped as /x08. How can I achieve what I want? Note I can't format the input paths as:

paths = [r'D:\notebook\bot', r'D:\newworld']

because it's passed from Maya. Python version is 2.6.

3
  • 2
    If they're really being passed from Maya like that then they're broken in the source and there's no reliable way to fix it. Commented Feb 9, 2012 at 5:29
  • 1
    How exactly do you get the string from Maya? Declaring the string as you did in your first code example will automatically result in '\b' being converted to '\x08'. Commented Feb 9, 2012 at 5:36
  • 1
    I assume you meant the \ in paths should be "D:\\notebook\\bot", "D:\\newworld" ? Commented Feb 9, 2012 at 5:45

3 Answers 3

1

The answer I came up with was to use the re module and make all the strings "raw" strings. For more info about raw strings check out http://docs.python.org/reference/lexical_analysis.html

import re

paths = [r'D:\notebook\bot', r'D:\newworld']

for p in paths:
    result = re.sub(r':?\\', "/", p)
    print(result)
Sign up to request clarification or add additional context in comments.

Comments

0

If your MAYA files are .MA (Maya Ascii) you can open them in a text editor and search and replace the paths manually.

Or, do it programmatically using perl outside of Maya:

perl -pi -e 's/\Qold-path\E/\Qnew-path\E/g' Your.MA

Comments

0

My workaround solution to convert str to raw string (converts control characters in string to it original escape sequences representation). Works with '\a', \b', '\f', '\n', '\r', '\t', '\v' . List of all escape sequences is here.

def str_to_raw(s):
    raw_map = {8:r'\b', 7:r'\a', 12:r'\f', 10:r'\n', 13:r'\r', 9:r'\t', 11:r'\v'}
    return r''.join(i if ord(i) > 32 else raw_map.get(ord(i), i) for i in s)

Demo:

>>> file_path = "C:\Users\b_zz\Desktop\fy_file"
>>> file_path
'C:\\Users\x08_zz\\Desktop\x0cy_file'
>>> str_to_raw(file_path)
'C:\\Users\\b_zz\\Desktop\\fy_file'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.