python pathlib not working with openBitMap

python pathlib not working with openBitMap

w.haak
Contributor Contributor
425 Views
1 Reply
Message 1 of 2

python pathlib not working with openBitMap

w.haak
Contributor
Contributor

In Max 22 python 3.7 ships with pathlib, a convenient library to handle file ops. Pathlib correctly resolves the directory picked via a dialog  as a windows path, but the mxs wrapper for openBitMap only appears to work with paths as string literals:

 

def blenddir():
    filedir = mxs.getBitmapOpenFilename()
    return Path(filedir).parent

 

returns: 

(print(type(img), img)
<class 'pathlib.WindowsPath'> C:\Users\user\Documents\test.tif

 

#---------

if img.is_file():
    print("Image found")

works, indicating the file exists/path is valid. 

 

however 

#---------

try:

    my_img = mxs.openBitMap(img)

 

throws an error:

MAXScript exception raised.
-- Unable to convert: C:\Users\user\Documents\test.tif to type: FileName

 

If I hardcode the path C:\\Users\\user\\Documents\\test.tif then the mxs.openBitMap(img) function works, so it would appear that the mxs wrapper for openBitMap requires a string literal as a file path. 

Is this a bug? Is there a convenient way of having pathlib return a string literal so I don't have to re-write my code base with os.walk?

Thanks!

0 Likes
Accepted solutions (1)
426 Views
1 Reply
Reply (1)
Message 2 of 2

w.haak
Contributor
Contributor
Accepted solution

I wrote function that converts the pathlib path back to a 3ds max string:

def maxdirstr(path: Path) -> str:
return str(path).replace("/", "\\")
0 Likes