View Full Version : How to use tkFileDialog.askopenfilenames() in LWPython scripts to multiselect files

09-06-2013, 11:50 AM
LWFileReqFunc (http://www.lawson3d.com/lwpywiki/index.php?title=LWFileReqFunc) can handle single filepaths, but it does not provide a means for the user to select multiple files. However, LWPython can import other Python modules, including the native tkFileDialog (http://tkinter.unpythonic.net/wiki/tkFileDialog) module.

The following single-shot code example will run in either Modeler or Layout. I wrote this script to assist a technical director; others may find it useful.

To use this script:

Open the PCore Console in Layout or Modeler.
Run the attached script (tkFileDialog-askopenfilenames.py)
Select one or more files in the requester and click "OK."

import lwsdk, Tkinter, tkFileDialog, re

contentDir = lwsdk.LWDirInfoFunc(lwsdk.LWFTYPE_CONTENT)

Tkinter.Tk().withdraw() # this hides the Tk window.
result = tkFileDialog.askopenfilenames(title="Choose file(s).", initialdir=contentDir)

if type(result) != 'tuple': # convert the string into separate filepaths.
c = result[:2]
P1 = c + ".*? (?=" + c + ")"
P2 = c + ".*?(?=$)"
P = re.compile(P1 + "|" + P2)
filepaths = re.findall(P, result)
filepaths = result

for f in filepaths:
print f