View Full Version : Help with GScript nested while loop


bus_labi
17th January 2011, 09:14
Hello anyone,

I wrote a script to test different settings of two variables of fft3dfilter (sigma and block size). I used Gscript and nested while loops, but for some reason i can not get it to work. Whatever i do only the inner while loop is executed. Could it be that nested while loop is not supported within Gscript (documentation is not 100% clear on that). Possibly something very simple i am overlooking ... any help will be appreciated.


sigma_start = 1.5
sigma_incr = 0.5
sigma_finish = 3.0
bs_start = 16
bs_incr = 16
bs_finish = 64
c = DirectShowSource ("2001 03 229.avi").converttoyv12
global c2 = BlankClip(c, length=0)

GScript ("""
while (sigma_start <= sigma_finish)
{
while (bs_start <= bs_finish)
{
fft3dfilter (c, sigma = sigma_start, bh = bs_start, bw = bs_start,\
measure = true, interlaced = true, ncpu = 6)
Subtitle(last, "sigma = " + String(sigma_start) + "\n bsize = " + String(bs_start), align=9, lsp=0)
c2 = c2 + last
bs_start = bs_start + bs_incr
Return(c2)}
sigma_start = sigma_start + sigma_incr }
""")
return (c2)

Gavino
17th January 2011, 10:56
Possibly something very simple i am overlooking
Yes, it is (although I had to stare at it for a while before I saw it :)).
You are not resetting bs_start after the inner loop, so on later iterations of the outer loop, the inner one will do nothing (its exit condition will always be true).
GScript ("""
while (sigma_start <= sigma_finish)
{
bs = bs_start
while (bs <= bs_finish)
{
fft3dfilter (c, sigma = sigma_start, bh = bs, bw = bs,\
measure = true, interlaced = true, ncpu = 6)
Subtitle(last, "sigma = " + String(sigma_start) + "\n bsize = " + String(bs), align=9, lsp=0)
c2 = c2 + last
bs = bs + bs_incr
}
sigma_start = sigma_start + sigma_incr
}
""")
return (c2)
It would probably be simpler to use a for-loop for the inner one:
for (bs=bs_start, bs_finish, bs_incr) { ... }
That's a bit messy to do for the outer loop as the for-loop index must be an integer.

Incidentally, c2 does not need to be global as GScript runs in the same scope as the outside script.

bus_labi
17th January 2011, 11:36
Hey, many thanks !

I spent several hours trying to get script to work, but probably was seeing what i wanted to see not what was in there. Next time i will take break.

I thought of using for loop, but dropped idea it because of int increments and doing two while loops seemed more consistent and logical than one for-loop and another while-loop. My goal is to test this (and some other filters) with many variables so being consistent was important.

BTW, is there any limit of levels of nesting in GScript?

P.S. Thank you for making GScript ... I wrote one loop using recursive function, but doing two loops scared pants off me. GScript made my life much easier (except for those miserable hours i spent looking for my simple error :)

Gavino
17th January 2011, 12:14
There are no (practical) limits on nesting in GScript - all the new constructs can be arbitrarily nested in any combination.

That was one of my design goals and comes about because GScript extends the actual script language, rather than trying to simulate the constructs with functions. It is this nesting ability that makes GScript much easier to use than other methods such as Eval blocks. Using recursion, a nested double loop requires writing two separate functions - with GScript you just write the code in-line in a natural manner.