Hi
I ran into a bit of a problem while writing a Tk script for use in
Houdini. I looked at the examples in the User Guide, and in
$HFS/houdini/scripts. However, these examples weren't overly useful,
because the only hscript command they ever used was "echo", whereas
I needed to use a command that takes parameters (chkey). The
documentation is rather sketchy in this area as well.
After a few days of bashing my head against the idiosyncracies of
Tk, I've figured out what's going on. In the hopes of saving someone
a headache:
The following three lines demonstrates the problem I was having (I'm
really using chkey, as opposed to echo, but for debugging purposes...):
set chan tx
hscript set chan = $chan
puts [hscript echo -f 1 -v {`1 - ch("/obj/sky/$chan")`} /obj/sky/$chan ]
When those are put into a file and run (using a fresh Houdini), you get the
following output:
/ -> tk /home/ammon/test.tk
-f 1 -v {1} /obj/sky/tx
Note the {}'s around the 1? Without them, things don't work in Tk.
With them, things don't work in Houdini.
Aside: I think the documentation may be a little off here. It says
that the " and $ characters have special meaning, and that unless
encapsulated in {}, things wouldn't work. However, try the following:
set chan tx
puts [ hscript echo 1 - ch("/obj/rleg/$chan") ]
The resulting output is:
/ -> tk /home/ammon/test.tk
1 - ch ( "/obj/rleg/tx" )
So the " characters come through just fine. However, when you try
and use the backticks:
set chan tx
hscript set chan = $chan
puts [ hscript echo `1 - ch("/obj/rleg/$chan")` ]
You get a bracing error:
/ -> tk /home/ammon/test.tk
Expression error: Bracing error
However, substitute `1 + 1` for the `1 - ch()` expression, and things
work fine. It seems that the combination of embedded " characters
within the backticks causes a problem, even without any variables --
the same bracing error occurs with this:
puts [ hscript echo `1 - ch("/obj/sky/tx")` ]
Further more, this also gives you a bracing error:
puts [ hscript echo `ch("/obj/sky/tx")` ]
But whereas wrapping the former in braces solves the problem for the
former, it doesn't for the latter -- {`ch("/obj/sky/tx")`} also gives
a bracing error. Yet {` ch("/obj/sky/tx")`} doesn't.
If anyone (who has followed this far) can explain to me just how
Tk goes about parsing things of this nature, I'd me much obliged
if you did so.
The only thing I've really been able to conclude is that any time you
do an expression evaluation with backticks (that contains a quoted
string), you have the option of using the braces, or dying with a
Bracing error. If you use the braces, then the result is _also_
encapsulated in the stupid braces.
The way around this is to do all the necessary expression evaluations
before hand so you can a) strip out all the extraneous braces in Tk and
b) build up the command line values with all the variables are in the
Tk namespace, as opposed to having some in Houdini, and some in Tk:
# Tk namespace...
set chan tx
# To Houdini namespace...
hscript set chan = $chan
# Get the current state -- using things in Houdini namespace, but
# saving the return value in Tk namespace.
set val [ hscript echo {`1 - ch("/obj/sky/$chan")`} ]
# However, that leaves {}'s around the value, so we have to trim them
# out -- still in Tk namespace, remember.
set val [ string range $val 1 [ expr [ string length $val ] - 2 ] ]
# Now that we have all the values we need
hscript echo chkey -f 1 -v $val /obj/sky/$chan
Which gives the output we're looking for:
/ -> tk /home/ammon/test.tk
chkey -f 1 -v 1 /obj/sky/$chan
Ammon
--
Ammon Riley * Technical Director * Toybox
ammon(a)compt.com * 416.585.9995