How to get started with HScript coding?
10286 9 0-
- whalerider
- Member
- 194 posts
- Joined:
- Offline
-
- edward
- Member
- 8081 posts
- Joined: July 2005
- Offline
Have you looked at the online help under Commands and Expressions?
There's also some info here:
http://odforce.net/wiki/index.php/HScript [odforce.net]
There's also some info here:
http://odforce.net/wiki/index.php/HScript [odforce.net]
-
- whalerider
- Member
- 194 posts
- Joined:
- Offline
Thanks, edward, that will definitely help. I'll have to refresh my knowledge of csh before diving into HScript.
edward
Have you looked at the online help under Commands and Expressions?
There's also some info here:
http://odforce.net/wiki/index.php/HScript [odforce.net]
-
- pyramation
- Member
- 176 posts
- Joined: July 2005
- Offline
HERE ARE SOME BASICS:
*for loops:
BASIC FOR LOOP:
for x = 1 to 10 step 4
echo $var
end
EXPRESSION IN FOR LOOP:
/*
Using expressions in hscript:
this just makes a loop based on the number of arguments in $var and increments y
When using expressions in hscript, you enclose them with backticks ` `
*/
set var = hmmmm… hello world!
set y = 0
for x = 1 to `argc($var)-1`
set y = `$y+1`
echo $y
end
*foreach loops
BASIC FOREACH
/*
use backticks ` ` inside the parenthesis when using foreach loops.
*/
foreach z(`$var`)
echo $z
end
USING FOREACH WITH OPNAME
/*
using (`run(“ls”)`) we are telling hscript show the output of running hscript with the run expression.
The (`run(“ls”)`) lets the foreach loop set $z to the name of the operator, for use when renaming.
*/
cd img/img1
set y = 0
foreach z (`run(“ls”)`)
set y = `$y+1`
opname $z pic$y
end
OPFIND AND FOREACH
/*
ah….the opfind command. My favorite. Think of the unix find command within houdini.
The -t flag is for type. To find out a type for a node, you can use optype command.
The -p is for the path to look in, without it, it looks everywhere.
Type “help opfind” in the textport for other flags.
This generates a list of all the mantra nodes, excluding the ones you don't want…hence the continue command, and creates a render script to disk.
You can source this script with hscript shell using the source command
The way to find the current unix path is upwd… pwd would return the houdini working path. (ucd to navigate unix path, cd is to change path in houdini's hierchy.)
“>>” appends to file, “>” would write over.
*/
foreach z (`run(“opfind -t mantra -p /out”)`)
if($z==“/out/mantra1”||$z==“/out/mantra2”)
continue
endif
echo -n render $z\;>>renderme
end
*while loops
BASIC WHILE LOOP:
while ($strlength==0)
echo hello world!
end
WHILE LOOP WITH READ:
/*
The read command allows for user input, just like in bash.
*/
while ($strlength==0)
echo
echo please enter in a value
read curvename
set strlength = `strlen(“$curvename”)`
end
*if statements
BASIC IF:
/*
Same as most languages…
&& is for and
|| is for or
!= is not equal to.
*/
if ($curvetype==n)
set curvetype = nurbs
else if ($curvetype==p)
set curvetype = poly
else if ($curvetype==b)
set curvetype = bezier
else ($curvetype!=n && $curvetype!=p && $curvetype!=b)
echo nurbs will be the default due to bad input.
set curvetype = nurbs
endif
IF WITH EXPRESSION:
/*
Embeds an expression, and the word “then” is kind pointless…but you can put it there.
*/
if `argc($var)`==1 then
echo hello world!
endif
USING IF WITH ARGUMENT COUNT
if $argc == 2
set OPP= $SOP/geo/rayed
else if $argc == 1
set OPP= $SOP/geo/merged
endif
CREATING SCRIPTS, AND REFERENCING THEM AS COMMANDS IN HOUDINI…..
/*
this way you can type the name scripts to run them, and keep them in a bin folder somewhere you would like.
HOUDINI_SCRIPT_PATH is a variable houdini needs to read scripts.
There is a scripts folder that it needs to read by default…so point it to your custom bin, and the scripts folder it is currently referencing.
add this line to your .bashrc, or whichever startup file you use.
*/
export HOUDINI_SCRIPT_PATH=“path/to/custom/bin:&”
FAKING AN ARRAY
/*
Returns all the point positions of all points in $SOP, in the format that the curve sop likes…ie: 0,0,0 1,1,1 2,2,2
Using the point expression to get the values.
Then opparms the curves coords parameter to be set to the $coords variable we created.
opparm is used to change values of parameters.
opparm syntax
opparm node parm value
you get the parm name by waving your mouse over the parameter label…they are usually different.
I had to put the $coords variable in parenthesis to get it to work,
because its a string with spaces.
*/
set SOP = /obj/model/node
set coords=“”
for i = 0 to `npoints($SOP)`
set pex = `point(“$SOP”,“$i”,“P”,0)`
set pey = `point(“$SOP”,“$i”,“P”,1)`
set pez = `point(“$SOP”,“$i”,“P”,2)`
set coords = $coords $pex,$pey,$pez “ ”
end
opparm /obj/model/curve1 coords (“$coords”)
*for loops:
BASIC FOR LOOP:
for x = 1 to 10 step 4
echo $var
end
EXPRESSION IN FOR LOOP:
/*
Using expressions in hscript:
this just makes a loop based on the number of arguments in $var and increments y
When using expressions in hscript, you enclose them with backticks ` `
*/
set var = hmmmm… hello world!
set y = 0
for x = 1 to `argc($var)-1`
set y = `$y+1`
echo $y
end
*foreach loops
BASIC FOREACH
/*
use backticks ` ` inside the parenthesis when using foreach loops.
*/
foreach z(`$var`)
echo $z
end
USING FOREACH WITH OPNAME
/*
using (`run(“ls”)`) we are telling hscript show the output of running hscript with the run expression.
The (`run(“ls”)`) lets the foreach loop set $z to the name of the operator, for use when renaming.
*/
cd img/img1
set y = 0
foreach z (`run(“ls”)`)
set y = `$y+1`
opname $z pic$y
end
OPFIND AND FOREACH
/*
ah….the opfind command. My favorite. Think of the unix find command within houdini.
The -t flag is for type. To find out a type for a node, you can use optype command.
The -p is for the path to look in, without it, it looks everywhere.
Type “help opfind” in the textport for other flags.
This generates a list of all the mantra nodes, excluding the ones you don't want…hence the continue command, and creates a render script to disk.
You can source this script with hscript shell using the source command
The way to find the current unix path is upwd… pwd would return the houdini working path. (ucd to navigate unix path, cd is to change path in houdini's hierchy.)
“>>” appends to file, “>” would write over.
*/
foreach z (`run(“opfind -t mantra -p /out”)`)
if($z==“/out/mantra1”||$z==“/out/mantra2”)
continue
endif
echo -n render $z\;>>renderme
end
*while loops
BASIC WHILE LOOP:
while ($strlength==0)
echo hello world!
end
WHILE LOOP WITH READ:
/*
The read command allows for user input, just like in bash.
*/
while ($strlength==0)
echo
echo please enter in a value
read curvename
set strlength = `strlen(“$curvename”)`
end
*if statements
BASIC IF:
/*
Same as most languages…
&& is for and
|| is for or
!= is not equal to.
*/
if ($curvetype==n)
set curvetype = nurbs
else if ($curvetype==p)
set curvetype = poly
else if ($curvetype==b)
set curvetype = bezier
else ($curvetype!=n && $curvetype!=p && $curvetype!=b)
echo nurbs will be the default due to bad input.
set curvetype = nurbs
endif
IF WITH EXPRESSION:
/*
Embeds an expression, and the word “then” is kind pointless…but you can put it there.
*/
if `argc($var)`==1 then
echo hello world!
endif
USING IF WITH ARGUMENT COUNT
if $argc == 2
set OPP= $SOP/geo/rayed
else if $argc == 1
set OPP= $SOP/geo/merged
endif
CREATING SCRIPTS, AND REFERENCING THEM AS COMMANDS IN HOUDINI…..
/*
this way you can type the name scripts to run them, and keep them in a bin folder somewhere you would like.
HOUDINI_SCRIPT_PATH is a variable houdini needs to read scripts.
There is a scripts folder that it needs to read by default…so point it to your custom bin, and the scripts folder it is currently referencing.
add this line to your .bashrc, or whichever startup file you use.
*/
export HOUDINI_SCRIPT_PATH=“path/to/custom/bin:&”
FAKING AN ARRAY
/*
Returns all the point positions of all points in $SOP, in the format that the curve sop likes…ie: 0,0,0 1,1,1 2,2,2
Using the point expression to get the values.
Then opparms the curves coords parameter to be set to the $coords variable we created.
opparm is used to change values of parameters.
opparm syntax
opparm node parm value
you get the parm name by waving your mouse over the parameter label…they are usually different.
I had to put the $coords variable in parenthesis to get it to work,
because its a string with spaces.
*/
set SOP = /obj/model/node
set coords=“”
for i = 0 to `npoints($SOP)`
set pex = `point(“$SOP”,“$i”,“P”,0)`
set pey = `point(“$SOP”,“$i”,“P”,1)`
set pez = `point(“$SOP”,“$i”,“P”,2)`
set coords = $coords $pex,$pey,$pez “ ”
end
opparm /obj/model/curve1 coords (“$coords”)
-
- whalerider
- Member
- 194 posts
- Joined:
- Offline
-
- goldfarb
- Staff
- 3465 posts
- Joined: July 2005
- Online
the textport is your friend here…
just type
help <wordYouDon'tRecognise>
or
exhelp <wordYouDon'tRecognise>
for eg:
exhelp if
float if (float expression, float true_value, float false_value)
If the expression is true, the function evaluates to the
true_value, otherwise the false_value is returned. The following
example shows that if the frame number is less than 12, the
resulting number equals the frame number. When the frame
number reaches 12 or greater, the resulting number is always 75.
NOTE: This is a function, which means all parameters to the
function are evaluated. So something like if($F > 1,
system('echo 1'), system('echo 2')) will result in both
system calls being run regardless of the result of the
expression.
EXAMPLES
| if ($F<12, $F, 75)
SEE ALSO
ifs
just type
help <wordYouDon'tRecognise>
or
exhelp <wordYouDon'tRecognise>
for eg:
exhelp if
float if (float expression, float true_value, float false_value)
If the expression is true, the function evaluates to the
true_value, otherwise the false_value is returned. The following
example shows that if the frame number is less than 12, the
resulting number equals the frame number. When the frame
number reaches 12 or greater, the resulting number is always 75.
NOTE: This is a function, which means all parameters to the
function are evaluated. So something like if($F > 1,
system('echo 1'), system('echo 2')) will result in both
system calls being run regardless of the result of the
expression.
EXAMPLES
| if ($F<12, $F, 75)
SEE ALSO
ifs
-
- whalerider
- Member
- 194 posts
- Joined:
- Offline
Thanks, arctor. I've been using help/exhelp to look up commands I know about from the HScript section in the help - but didn't think about ‘asking’ about things like if, while, etc.
Now I know that run is the short form of execute.
Here's another question: Does opcook -f work? In the help for opcook, the -f flag is “tbd”
How do I specify a frame range for which I want a simulation to be recooked? I tried things like:
opcook -f 1 48 - recooked the first frame only
opcook -f 1, 48 - “Invalid usage” error
opcook -f 1-48 - “Invalid usage” error
opcook -f (1 48) - Can't find 48
opcook -f “(1 48)” - “Invalid usage” error
Now I know that run is the short form of execute.
Here's another question: Does opcook -f work? In the help for opcook, the -f flag is “tbd”
How do I specify a frame range for which I want a simulation to be recooked? I tried things like:
opcook -f 1 48 - recooked the first frame only
opcook -f 1, 48 - “Invalid usage” error
opcook -f 1-48 - “Invalid usage” error
opcook -f (1 48) - Can't find 48
opcook -f “(1 48)” - “Invalid usage” error
Edited by - Nov. 20, 2006 18:13:11
-
- wolfwood
- Member
- 4344 posts
- Joined: July 2005
- Offline
As a side note, based on the buzz from the Houdini Roadshow you might also want to have a look at this….


http://forums.odforce.net/index.php?showtopic=4609
HOM - Houdini Object Model - Our hscript and expression languages will be replaced by the HOM initiative which will use Python for all of these tasks. Now you will be able to use Python directly in Houdini for typical scripting tasks and expression writing. Many tasks will now be easier to script and learning how to script Houdini will be easier for anyone with a foundation in Python.
Edited by - Nov. 20, 2006 17:43:20
if(coffees<2,round(float),float)
-
- whalerider
- Member
- 194 posts
- Joined:
- Offline
-
- whalerider
- Member
- 194 posts
- Joined:
- Offline
-
- Quick Links

