Custom Global Variables

Create Custom Global Variables in Houdini

Global variables are handy in Houdini. You’re already familiar with them I’m sure. Think $HIP, $JOB, $HIPFILE, $COOLCATS, etc. Ok, maybe not $COOLCATS but you could create it if you so desired. They also go by system or environment variables but we will stick to global. Some folks use the terms interchangeably but I kind of prefer to leave the environment term to the variables available through the hcofig command such HOUDINI_MAXTHREADS and HOUDINI_VEX_PATH. There is some overlap and there might be technical differences in what Houdini considers system, environment and global variables. If you have a better understanding on the subject, ping me on Twitter and school me. For now, don’t worry too much about the technical differences between the terms, just concentrate on why variables are useful. Here are a couple of ways to create your own custom global or system variables in Houdini.

What Are Variables?

First, what is a variable? A variable represents a particular piece of your computer’s memory where you can store, retrieve, and manipulate data. It’s basically a storage unit. There are various types of variables but the ones we are referring to here are global variables. The reason they are called global is because they have global scope or are accessible throughout the application. You can use the variable anywhere in Houdini where it makes sense.

Now, don’t confuse this with a particular node’s standard variables or local variable mappings created through an Attribute Create geometry node. These only work where local variables are allowed or only available through that node.

Second, why would you want to create one? Well, as a simple example, let’s say you are saving out a file to a deeply nested path. Instead of writing out the path manually or using the directory browser every time you want to save to that location, you can just set the value of your custom variable to that location and anywhere you use the variable, Houdini will automatically expand it to the correct path.

They are also great as constant values. Maybe you are using some specific value for some expressions in your network. If you decide you need to tweak the value, you would have to go in and change that value for every operator that is using it. Tedious no doubt. Worse, you may end up cranking up a 9 month render and you realize you forgot to change the value in that one node named geo350.

This is where a variable would come in handy because you can create a custom variable with your value and if your boss or client asks “Can you have it rotate faster by .001%?,” all you have to do is change your variable in one place and you are done.

Set It & Forget It

So the question is how do you go about creating one. It’s actually really easy. There are two ways. For me, using HScript is the easiest but if you like GUIs, there is also that option. I want to replace my long file path with a simple variable. So I will store the location of the path in my custom variable which I will call MYPATH. You can call it whatever you want but just don’t use spaces in the name.

Custom Global Variables
Custom global Variables

The way to create a custom variable through HScript is by using the set or setenv commands in the Textport. Both commands take optional arguments that are useful when working with scripts. The main difference between the two is set creates local variables and setenv creates global variables, although both can be used interchangeably to a certain extent. We will use setenv.

For now lets just see what the commands do on their own. If you just type set or setenv without any arguments, you will get the current variables available in Houdini. Some of these should be familiar to you.

set and setenv commands
set and setenv commands

Let’s create a custom variable. You type setenv followed by the identifier or name you want your variable to be called, followed by an equals sign and finally the path or value you want to assign to your variable. All you are saying is set this word equal to this value. No sweat. Capitalization matters here. The variable MYPATH is not the same as MyPath or mypath. The variables THREES_COMPANY and threesCompany are different pieces in memory even if they hold the same value. By convention, variables that do not change are all capitalized. In some programming circles, they are called Constants.

 setenv MYPATH = /Volumes/HDOne/RESOURCES/Models/Stadium/versions/SoccerStadium
Creating a custom global variable
Creating a custom global variable

You can also use set to create global variables but you have to pass the -g option otherwise it creates a local variable. If you wanted to create a local variable with setenv, then you would add the -l option. The differences are more apparent when you are crafting script files.

set -g MYPATH = /Volumes/HDOne/RESOURCES/Models   #This creates a global variable
setenv -l threshold = .5                          #this creates a local variable

Visual Mode

The other way of creating a custom variable is by using a GUI. You will find the Aliases and Variables window by selecting the Aliases and Variables... option under the Edit menu. This will open up a window with three panes. We are interested in the middle one called, appropriately, Variables.  This section lists some of the Houdini global variables. At the bottom of the window, you will see two text fields. The left one takes your custom variable name and the right one is the value.

Aliases & Variables
Aliases & Variables Window

 

Aliases & Variables
global Variables

 

Unsetting Variables

So what happens when you want to change or clear out the variables? If you want to just remove the variable then you would use the -u option. This unsets the variable and poofs it away. If you want to change the variable to a different value, then you can just set the variable again using the same name. Wouldn’t it better to unset first then recreate it? Possibly, but I haven’t encountered any ill effects as of yet. Sometimes I unset first, other times I just set again directly.

Remember what I said about variables that you don’t anticipate changing using all capitals? Well, for those variables that do change more frequently, you would then use lowercase. You don’t have to do this but it’s good etiquette. Sort of like eating BBQ chicken with a fork.

Unsetting Variables
Unsetting Variables

Overall, global variables are handy. If you want to learn more, head on over to the Houdini documentation and read up on set and setenv. Just remember to be mindful if you do use global variables and pass the scene file to someone else. Either let them know or add notes to make them aware of what you have cooking. Global variables when used poorly can lead to a great deal of headaches. You are warned!

Share:
990adjustments

990adjustments

I am a motion designer & developer based out of South Florida. When not designing or animating pixels, I wrangle some code. If all else fails, I watch Twilight Zone, I Love Lucy, or Three's Company reruns.