Sometimes when you’re building an interface for an application you suddenly realize that you need what aught to be a simple feature. Time and again I’ve found myself wanting to be able to quickly select a directory for TouchDesigner to use for a bin of images or movie files. This is a pretty simple feature to add to your network, and we can do this with just a simple panel execute DAT and a few lines of Python. Let’s take a closer look at what goes into make this happen.
Let’s start by assigning our Button to our Panel Execute DAT. We can do this by editing the Panel field in the Panel Execute, or by dragging and dropping our button onto our Panel Execute DAT.
Either of these methods work, and it really just comes down to a matter of personal preference. Next let’s make sure that we’ve set our button to have a “Momentary” Button Type. We don’t want a button that toggles on and off, in this case we just want a button that fires a single pulse when we click on it.
Now we’re finally ready to write a little bit of python. The effect that we’re looking for is to open up a windows dialog box that allows us to select a directory for our folder DAT. To do this, we’re going to use the UI Class in TouchDesigner. They is a ton we can do with this class, and folder or file selection is one of those things. Let’s set up our Panel Execute DAT so that the Off to On Flag is on, and the panel value that we’re watching for is “state.”
We’re going to edit the offToOn definition in this DAT, so it’s important to make sure that you’re editing the correct portion of your script. The first thing we want to do is create a new variable called “userFolder.” Now, in our case, we want this variable to equal to the string that is the pathway to the directory that we want. That’s going to look something like this:
userFolder = ui.chooseFolder(title = 'Choose a Folder')
Inside of our single ticks (or you can use quotes) is the name that we want to see on the pop up window. This is a great start, but at this point we’re not actually assigning our selected directory to our folder DAT. Before we get there we need to consider the following: What happens if I change my mind about selecting a new directory, and just hit cancel? When you do this, instead of a directory being returned, the windows selection dialogue will return a “None” from your folder selection. That’s great, but this could mean that in the middle of a set, you suddenly find that when you hit cancel you loose the directory that you previously navigated to. To prevent this from happening, we can use a simple if test to decide what we’re going to do. In our case, if we get “None” back from our selection, we just want to leave our directory as it is, otherwise we want to change it. Okay, what might that look like?
userFolder = ui.chooseFolder(title = 'Choose a Folder') if userFolder == None: op('folder1').par.rootfolder = op('folder1').par.rootfolder else: op('folder1').par.rootfolder = userFolder
This is looking pretty good. Let’s review what’s happening here. First we select a new folder, next we do a quick test. If our userFolder is exactly “None” then our folder DAT root folder stays the same. If our userFolder is anything else, then we set the folder DAT root folder to our new address. Simple.
If you want to see this example in action, you can download it from GitHub here: TD Examples.