I was looking into writing a fairly simple batch file to install some stuff and update environment variables. At some point I wanted to do something like this (constructing a string of ‘;’ separated paths):
set env_vars = \path\to\stuff
if x%user%==xsveinbjorn (
set env_vars = %env_vars%;\another\path
)
if x%hostname%==xgefjun (
set env_vars = %env_vars%;\whoah\this\is\radical
)
echo env_vars
When I would run this I would always get
\path\to\stuff;\whoah\this\is\radical
(Notice that the second path \another\path is missing from the result)
Being very new to writing batch scripts I found this extremely confusing since this sort of stuff would work like a charm in python. So I started the google machine.
The reason this happens is that variables enclosed in ‘%’ are expanded when the script is read, which is quite different from the way python handles this resolving stuff at the last possible time. So when the script is read it will actually look something like this:
set env_vars = \path\to\stuff
if xsveinbjorn==xsveinbjorn (
set env_vars = \path\to\stuff;\another\path
)
if xgefjun==xgefjun (
set env_vars = \path\to\stuff;\whoah\this\is\radical
)
echo env_vars
(I resolved the variables %user% to ‘sveinbjorn’ and %hostname% to ‘gefjun’)
And we can clearly see why we got the result that we got.
So what we have to do is tell the batch file to expand the variables as we go along (something close to how python does things) instead of this expand-it-when-you-read-it nonsense.
To do this we simply have to do three things:
- Add this line to the top of the code
setlocal EnableDelayedExpansion
- Add this to the end of it
- Replace the enclosing ‘%’ to ‘!’ for the variable you want to delay the expansion for.
if x%user%==xsveinbjorn (
set env_vars = !env_vars!;\another\path
)
So my original example would look like this:
setlocal EnableDelayedExpansion
set env_vars = \path\to\stuff
if xsveinbjorn==xsveinbjorn (
set env_vars = \path\to\stuff;\another\path
)
if xgefjun==xgefjun (
set env_vars = \path\to\stuff;\whoah\this\is\radical
)
echo env_vars
endlocal
Now we get the result we were looking for:
\path\to\stuff;\another\path;\whoah\this\is\radical
This also applies to loops and other places where the variable might change between the time you start the batch script and when you actually want to expand it.
I had this issue the other day where I had at some point deleted the sound that accompanied a clip from a timeline. The quickest way I found was the ‘match frame’ (shortcut: f) command. Just put your playhead on the start of the clip you want to get the sound back from and hit f. Then the viewer loads with the source clip (not the clip from the timeline) with the appropriate in and out points. Then you can either hit f10 or use your preffered method of inserting thing into your timeline. And there’s your clip with sound.
I found this extremely useful when I had to recover the sound from around 100 shots. Then I pressed the down arrow key, hit f, f10 and repeat.
Jason Schleifer wrote a really nice post about GTD and some ideas about how to use it for animation.
He’s also got some nice posts about using gtdagenda.com and other nice stuff. Check it out.
I found this tip somewhere and thought I’d share this as the first in hopefully several posts of the “post a week ‘othon”.
To replace a layer in a composition with a new item from the project window select the layer to be replaced in the composition window. Press option and drag the item from the project window and drop it on top of the selected layer in the composition window. Simple.
This preserves everything you had done to the first layer. So this saves you the hassle from dropping in the new item, copying animation, effects and transforms.
I don’t know why I didn’t know before last week.
Here’s a little mel snippet that I found occasionally useful while animating, all these commands do is add (or subtract) 360 from a keyframe value – which is perfect for those rotational values that need to come down (or go up).
Anyway, to add 360 degrees:
keyframe -e -r -vc 360;
Subtract 360 degrees:
keyframe -e -r -vc -360;
I’ve found this useful in certain cases (gimbal lock?). And hopefully someone can find this useful as well.
Just a little Maya tip here if you have Maya 8.5 or later.
If you need to make some calculations and don’t want to go find your calculator of choice you can use Maya to calculate stuff for you. First, make sure you have python active in the command line (bottom left text field).

If it says MEL, just click it and it should switch to Python

Then you just type in what you want calculated, for example if you type in “4+4″ and then hit control-enter, then the results pops up in the command response window. Easy.

IMPORTANT NOTE
To use Python as your calculator you have to remember that if you are using division remember to type the number you want to divide as a decimal number (like 5.0 instead of 5) otherwise you’ll get a rounded down result. 5/2 gives you 2 and 5.0/2 gives you 2.5. This obviously doesn’t matter if you are adding, multiplying or subtracting.