Syntax for expressions using loops

   4163   7   1
User Avatar
Member
2042 posts
Joined: Sept. 2015
Offline
Is it possible to use a loop within an expression?

One of the tutorials I saw used an `if` statement which worked,
but I haven't found any reference to loops.

I've tried doing a search and saw reference to an old houdini forum post where the guy basically says just use c style syntax:

for (i=0;i<2;i++) {…}

so I gave this a try,

for (i=0;i<2;i++) {$F}

The $F within the curly braces is not actually what I want to do but was put there simply as a placeholder until I can sort out the syntax of using loops.

But my Houdini complains about this syntax and I haven't found any tutorials that go more into depth about using scripting to drive elements of whatever I may build.

Perhaps I'll have to dive into using Python? I'm trying to hold off on that for the time being and try to learn how to get the most out of using expressions ( entering code into the textboxes ).

Any feedback is appreciated
User Avatar
Member
8597 posts
Joined: July 2007
Offline
here is an examlple of multiline expression using for loop to accumulate some value and return it

{
float out = 0;
for(i=0;i<10;i++)
{
out += i;
}
return out;
}
Tomas Slancik
FX Supervisor
Method Studios, NY
User Avatar
Member
2042 posts
Joined: Sept. 2015
Offline
I left out a part in my post.

It's been while since I've coded in c but I think I remember `i` I reference in the above example would need to of been declared before as an int.

So maybe that's the issue with the syntax here? The hasn't yet been declared?

Then I guess the question is how to declare the variables in houdini.

I've seen examples of something getting close like use the stamp feature of the copy node. But this can make the code a bit lengthy, eg. stamp(“../copy1”, “i”, 0) when `i' would suffice if possible with it being declared elsewhere with a simple `int i;'
User Avatar
Member
2042 posts
Joined: Sept. 2015
Offline
Ok great tamte,

Thanks very much for that, I will play with it, unfortunately have to run out the door and I just saw your post.

Just wanted to say thanks to give me something to work with.
User Avatar
Member
2042 posts
Joined: Sept. 2015
Offline
Great - had a chance to play with this loop and it works like a charm.

Thanks much tamte.

I was even able to put in channel references to get the results I was looking for.

This is what attracted me to Houdini.

So I'm really looking forward to researching more in depth the ins and outs of using expressions in this manner.

I like this because I am familiar with writing small programs in c, so the familiar syntax makes it easy.

On that loop though I did try out of curiosity to define the `i` used in the for loop as an int just after the float is declared.

{
float out = 0;
int i;

for(i=0;i<10;i++)
{
out += i;
}
return out;
}

But Houdini complained with a syntax error; So I guess it may be like c but not quite exactly.

Would be nice to have a compact syntax reference that explains these little nuances; Something like a K&R book but tailored to Houdini.

Maybe someday when I am more experienced with Houdini I'll write one.
User Avatar
Member
8597 posts
Joined: July 2007
Offline
hscript expressions don't have int type, only float, string, vector and matrix
Tomas Slancik
FX Supervisor
Method Studios, NY
User Avatar
Member
2042 posts
Joined: Sept. 2015
Offline
Thanks for that tip tamte
User Avatar
Member
2042 posts
Joined: Sept. 2015
Offline
Ok…so its called hscript…so far in the videos I've seen the simple entries referred to as expressions….

So I googled houdini hscript and found the perfect link I think I should sink my teeth into.

Hopefully any other new users like myself might find it useful.

I don't think its currently listed under the current tutorials. (unless I somehow missed it ).

It's dated 2009 but I suspect most of it still applies.

http://www.sidefx.com/index.php?option=com_content&task=view&id=1500&Itemid=254 [sidefx.com]
  • Quick Links