Log in

View Full Version : Repeated use of function with global vars


foxyshadis
15th December 2005, 16:03
I'm trying to use a conditional function in a script and breaking everything. First the script:


x=blankclip(length=10000, pixel_type="YV12").showframenumber()

s1=x.trim(0,346)
s2=x.trim(347,2053).FixBlendIVTC()
s3=x.trim(2054,2481)
s4=x.trim(2482,4918).FixBlendIVTC()
s5=x.trim(4919,8372)
s6=x.trim(8373,9282).FixBlendIVTC()
s7=x.trim(9283,0)

y=s1+s2+s3+s4+s5+s6+s7
return s2 # x # y

If you want fixblendivtc for testing or whatever, you can get it from my thread (http://forum.doom9.org/showthread.php?t=101477), but it's not unique in this respect.

Problem is, s6 overwrites s2 and s4, because fixblendivtc uses global variables to communicate with the conditional, and apparently the last set value overrides, so s6 repeats each time the others are supposed to show. It's not a bug, just the way global variables work, but if I need to use it this way is there any way around?

I came up with using a conditionalreader with on/off ranges as a workaround, though I haven't actually tried it yet. Are there any ways to make the above code work, though, without importing 3 versions of the filter, with different variable names?

stickboy
15th December 2005, 19:28
I'm not familiar with FixBlendIVTC, but can you invoke FixBlendIVTC once on the entire clip and then do the trims and splices?

foxyshadis
15th December 2005, 21:41
I didn't even think about that. There's no reason it should be any worse... I'll try it. Thanks!

gzarkadas
18th December 2005, 07:48
It's not a bug, just the way global variables work, ...If Avisynth script was a "true" scripting language it would be a bug, since by assigning in a separate variable you (should) create a new copy independent from the others. However, since the implementation actually appears to be a filter graph specification in the form of a scripting language, this makes sense.
...but if I need to use it this way is there any way around?The only way around I have been able to devise is to use different names for the global variables. If you use the same, then you always end up with the last invocation prevailing.

The 1st of the attached scripts demonstrates this. If you change the first 2 calls to be the same function or the last 2 to use the same suffix, then you will get the "strange" behavior.

The 2nd demonstrates a more general and elegant solution to the problem: code your function so that it auto-generates different global variables' names in every invocation :cool: . This works.

Thus, the recipe I suggest (see the scripts for details) is the following:

Code your function the normal way, just group your conditional code in functions and put all global variables' use together (it will make rest steps easier).
After debugging (with 1 call of your function) is finished, wrap your global variables blocks into a """ quoted string as in the example script.
Enter a @ character at the end of each global variable's name inside the """ quoted string created at the previous step.
Create at the top-level wrapper an auto generated suffix and use Eval(ins_prefix(string)) to replace all occurences of @ and evaluate (thus execute) the group of expressions.


This will allow transparent multiple invocations of a function that uses scriptclip in the same script. But, since every call creates a bunch of new globals that stay around, use as sparingly as you can.

NB: Where the scripts say "prefix", the right word is "suffix".