BuildingGraphicalInterfacesInTclTk

From Odwiki

Jump to: navigation, search

Contents

Introduction

In this tutorial, we will learn how to build a simple graphical interface for controlling Houdini using Tcl/Tk on Windows. Specifically, we are going to create a simple Tcl/Tk window which can be launched from Houdini to create some custom objects. It's not a terribly useful program but it will illustrate what you can do with Tcl/Tk in Houdini. For those of you who don't know, Tcl/Tk is is an easy to learn, cross-platform scripting language which can be used to create graphical programs.

The idea of this tutorial is to get you up and running creating your own graphical interfaces in Houdini with minimal hassle using Tcl/Tk. This means that I'm going to assume that you already know Houdini's scripting language, hscript. If you don't know it, then go read the first section of the Scripting chapter in the reference manual now. From under the main Help menu in Houdini, choose Scripting. For Tcl/Tk, go read this link: http://www.tcl.tk/scripting/primer.html. The rest of this tutorial will assume that you know at least this information so that we can focus on using Houdini and Tcl/Tk together instead of them individually.

Setting Up

First, I'll assume that you already have Houdini installed on Windows. user posted image Since Tcl/Tk already comes with Houdini, you already have that installed. The other tool you will need for this tutorial is a graphical user interface builder for Tcl/Tk called Visual Tcl. You will need to download this program first. Go to http://sourceforge.net/project/showfiles.php?group_id=285 and download vtcl version 1.2.2. The filename to download is vtcl-1.2.2.EXE. Don't try to download the later versions because they won't work with the version of Tcl/Tk that is included with Houdini. Run the executable you downloaded and install it into c:\vtcl.

For this tutorial, we will also use a special version of the Build desktop that includes a textport pane. Copy/Paste the following and save it into your Houdini desktop directory as TkBuild.desk.nosave. For me, this was in: C:\Program Files\Side Effects Software\Houdini 5.5.36\houdini\desktop. Make sure that the 5.5.36 bit matches the version of Houdini that you are currently using. If you're using version 5.5.197, then make sure you're saving it in the right spot. Here's the file:

fullscreen -1
playbar -1
split 0.121495 V {
    split 0.673077 H {
        pane pane1 -a 0 -C 1 -c 1 -l 1 -h /obj -T OBJ -m viewer
        split 0.5 V {
            pane pane3 -a 0 -C 1 -c 1 -l 1 -h /obj -T OBJ -m parmeditor
            pane pane4 -a 0 -C 1 -c 1 -l 1 -h /obj -T OBJ -m neteditor
        }
    }
    pane pane2 -a 0 -C 1 -c 1 -l 0 -h /obj -T OBJ -m textport
}
viewerstow -t 0 -x 0 -b 1 -l 1 -m 1 -d 1 TkBuild.pane1.world
vieweroption -a 1 TkBuild.pane1.world
neteditor -G 0.75 -P 0.8 -p 0 -c 0 -o 0 -n 1 -s 2 -x 0 -e 0 -I 0 -z 0 -S user pane

Getting Started Using Visual Tcl

Start Houdini and switch to the TkBuild desktop from under the Desktop menu. In bottom most pane, there's now a textport. You probably want to minimize the pane toolbar there as that's not needed and it will give you a bit more screen real estate. At the textport, command prompt type: tk c:/vtcl/vt.tcl

This should start up Visual Tcl. Now minimize the Houdini window and switch to the Visual Tcl program. Here's a screenshot with Visual Tcl up on my system:

Image:Tcltk_screenshot_base.jpg

Creating the Main Window

On the left side is the Widget Toolbox window. It contains a bunch of buttons for creating the interface. First you need to create a window. That's the top-left icon in the toolbox. Go ahead, click on it.

Image:Tcltk_new_window.jpg

On the right side, there is a window named the Attribute Window. This lets you set parameters on the active UI element. Since we just created a new window, this is the active UI element. Scroll to the bottom of this and there will be an input field named "title" filled with a default value of "New Toplevel 1". Change the value of this to "Object Creator" and press the Enter key. Notice that the title of your created window now says "Object Creator".

Image:Tcltk_attribute_window.jpg

On Windows, it appears that there is a bug with quitting the window if you click on the upper-right X button of your window. So we will need to fix that. First note what the Widget name of your window is in the Attribute Editor. It's the first item in the window. For me, it says ".top17" like in the screenshot above. Yours may differ. Double-click on the word "main" in your Function List window. This will bring up a window for you to enter in code that gets executed when you first start your program after your window has been created. In the large text box of the window, type in this replacing .top17 with the widget name of your window:

 wm protocol .top17 WM_DELETE_WINDOW {exit}

This will ensure that if you quit by click on the red X button, it will properly exit. If you didn't do this, then if you quit the window by pressing on the X button, you will be unable to re-start another Tcl/Tk script until you open up the Task Manager and manually kill the "hwish.exe" process.

Image:Tcltk_function_list.jpg

Saving Your Work

Ok, so far, we have set up a basic window which we can re-use for all our future Houdini Tcl/Tk programs. Now is a good time to save our script as a base to be used in the future. In the main Visual Tcl window's File menu, choose "Save As...". Save this as c:\vtcl\hwindow.tcl. Since this is going to be our base for the future, we don't want to modify this anymore, just load from it. So choose "Save As..." again and this time, save it as c:\vtcl\objcreate.tcl instead. Periodically, you should save your results.

Adding the Geometry Button

In the Widget Toolbox window, click on the icon on the left side, third way down that looks like a button with a letter B on it. This will create a button in your window. Your Attribute Editor window should now show the properties for your new button. Scroll down until you find the "text" input field that has "button" written in it. Change the text to "Geometry" and press the Enter key. Now double-click on the newly created Geometry button. It will pop up a window that is named something like "-command for .top17.but17". This window allows you to enter in a Tcl/Tk command to execute when you click on it. In it, type this:

 hscript opcf /obj
 hscript opadd geo

This is how you send hscript commands to Houdini from Tcl. You put in the word hscript, then type in the Houdini hscript command after it. So what we're saying here is that we wish to send the commands "opcf /obj" and then "opadd geo" to Houdini. The "opcf /obj" changes the current working directory in Houdini to /obj which is where we have our objects. The "opadd geo" command tells Houdini to create a new object of type "geo".

Also notice that you have resizers on the Geometry button. You can click on the small squares to resize the button to your liking or drag the button to place it somewhere else in the window.

Testing

Now let's check if what we have created works! Notice that in the main Visual Tcl window, the bottom status line has a button that says "Edit". That indicates that we are currently in Edit mode, which means we are editing our Object Creator window. Click on it to toggle into "Test" mode.

Image:Tcltk_test_mode.jpg

Now we can test out what we created so far. Click on the "Geometry" button. This should create a new geometry object in your Houdini window. Click on it a few more times for fun. You will see more objects created in your network editor. They might overlap each other though so press the lowercase L key inside the network editor to lay everything out. They will be named "geo1", "geo2", "geo3", etc. To go back into Edit mode, click on where it says "Test" in the Visual Tcl window's status bar and it will switch back to editing your window.

Image:Tcltk_geo_button.jpg

The Test mode is used to check if you entered in the commands for your button correctly. If it didn't create the geometry object then go back and double-click on the Geometry button and check that you've typed the commands correctly. Of course, make sure that you are back in Edit mode first.

Now that's all fine and dandy. But how do you run your script from outside Visual Tcl? First, make sure that you save your script as c:\vtcl\objcreate.tcl and then quit Visual Tcl. Now switch to the Houdini window. In the textport, type:

 tk c:/vtcl/objcreate.tcl

Play with it bit, quit and then try running it again. This will make sure that you entered in the correct line for when you entered the wm command for the main function.

Adding the Cone Button

In this section, we're going to create a button that has slightly more complicated hscript. Before we begin, close the Object Creator script and load back Visual Tcl. You remember how to do that right? Just in case, you type tk c:/vtcl/vt.tcl in the textport. After that, you need to load the objcreate.tcl script into Visual Tcl. Do that using the "Open..." option under the File menu.

Now, what we want is to add a button that creates a geometry object with default cone geometry. The hscript code to do that is a bit more involved so I'll show you an easy way to do this using Houdini to tell us what the code is. Inside Houdini, jump down in to the SOPs for the default model object that gets created whenever Houdini starts up. It's a nice empty placeholder to start modelling from. Inside the model object, put down a Tube SOP. Then change its first Radius parameter to 0. Now we have a primitive cone. If you want, you can turn on the End Caps parameter on the Detail page too.

In the textport, we want to generate the commands that can recreate the cone object. To see this, type this into a floating textport window (from under the Dialogs menu):

 opscript -r -g /obj/model

That's a lot of commands to put the prefix hscript in front of if we want to add all this code to our new Cone button! Fortunately, there is a better way. We don't need the floating textport anymore so you can close it now. In the onscreen textport pane, type:

 opscript -r -g /obj/model > c:/vtcl/objcone.cmd

What this does is save all those commands into the file objcone.cmd. What we are going to do is run this file from within Tcl/Tk. We won't need the model object anymore so go back up into the Object level and delete the model object.

There is a slight problem though. The script we saved out isn't general enough to handle what happens if we keep on trying to create an object repeatedly with the same name. So what we need to do is to open the file 'c:\vtcl\objcone.cmd and fix it a bit. Find the line which reads "opadd -n geo $arg1" and replace it with:

 set arg1 = `execute("opadd -nv geo $arg1")`

This code is simply a modification of the old one except that it resets the variable arg1 to the actual new name that opadd created. For example, if arg1 was previously, "cone1" and cone1 already existed in our scene, then opadd would create an object named "cone2" instead and return the new name. We then take this name and put it back into arg1. This allows the rest of the script to work correctly with the new name. That's it. Now our objcone.cmd script is ready for use.

Now switch to your Visual Tcl that we have been ignoring up until now. First, click on empty space inside your Object Creator window so that Visual Tcl knows where to create the button. Then, in the Widget Toolbox window, click on the create button icon to add another button to our script. Move the button so that it is not overlapping with your first one by dragging it over. Your Attribute Editor window should now show the properties for your new button. Scroll down until you find the "text" input field that has "button" written in it. Change the text to "Cone" and press the Enter key. Now double-click on the newly created Cone button. It will pop up a window that is named something like "-command for .top17.but18". In it, type this:

 hscript source c:/vtcl/objcone.cmd cone1

This command tells Houdini to run the script name objcone.cmd we created earlier and pass cone1 to the script to be used as the default name. Now let's try it out. Switch to Test mode and then press the Cone button a few times. user posted image When you are done testing, switch back to Edit mode.

Adding a File Menu

For our program, we'll create a simpe File menu that shows off some of the features that can add to your program. To do this, double-click on some empty space inside the Object Creator Window. This will popup a new window for adding menu entries to your main window menu. Under this menu, we want to add the File menu, and then under the File menu, we want to add some options. Ok, so now under "Entry Type" in the window, change the type to from "command" to "cascade". Under "Entry Label", type "File". Now click on the Add button. This creates a File menu. Now double-click on the newly added "<cascade> File" line in the list box. This will bring up another window to add entries for your File menu. Leave the Entry Type set to "command". In the Entry Label, enter "Open..." and then click on the Add button. Add two more menu entries labelled "Save..." and "Close".

Now double-click on the "<command> Open..." line. It will bring up a window titled "Command for Open..." for you to enter the Tcl/Tk command when the entry is picked by the user. In it, enter this and hit Ok:

 set types { {{HIP} {.hipnc}} }
 set filename [tk_getOpenFile -defaultextension .hipnc -filetypes $types]
 if {$filename != ""} {
     hscript mread $filename
 }

This Tcl/Tk code will start up a file open dialog with HIPNC as an extension for the file to open. Once we have the filename that the user has chosen, we tell Houdini to open it with the mread hscript command.

Now double-click on the "<command> Save..." line. In the new window, enter this and hit Ok:

 set types { {{HIP} {.hipnc}} }
 set filename [tk_getSaveFile -defaultextension .hipnc -filetypes $types]
 if {$filename != ""} {
     hscript mwrite $filename
 }

This Tcl/Tk code will start up a file save dialog with HIP as an extension for the file to save. Once we have the filename that the user has chosen, we tell Houdini to save it with the mwrite hscript command.

Now double-click on the "<command> Quit" line. Type in "exit" and then press OK. For now, we're done with adding menu items. So click on the Done button in the Editing Menu windows until they are all gone.

Ok, we're now all done. Switch Visual Tcl into Test mode and try out the menus! Don't forget to save first! user posted image

What's Next?

Ok, we've learned a couple of things in this tutorial:

  • How to use Visual Tcl from Houdini
  • How to create a Tcl/Tk window that works around the quitting bug
  • Two ways to create a button for creating objects in Houdini
  • How to create a menu that can save and load Houdini HIP files

For some extra fun, play around by tryng to put an icon into your created buttons. Click on the created button and then in the Attribute Editor, there's an input field that says "image". If you click on the "..." button next to it, you can pick a picture to use for your button. Have fun!

Resources

© 2009 od[force].net | advertise