Welcome to Doom9's Forum, THE in-place to be for everyone interested in DVD conversion.

Before you start posting please read the forum rules. By posting to this forum you agree to abide by the rules.

 

Go Back   Doom9's Forum > Capturing and Editing Video > Avisynth Usage

 
 
Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
Old 18th June 2009, 11:18   #1  |  Link
Gavino
Avisynth language lover
 
Join Date: Dec 2007
Location: Spain
Posts: 3,431
GScript - language extensions for Avisynth

This is something I started as a 'proof of concept' for my own amusement.
As I think some people will find it useful, I am making it generally available.

GScript is a plugin that extends the Avisynth scripting language to provide additional control-flow constructs:
multi-line conditionals (if-then-else blocks), 'while' loops and 'for' loops.
Rather than trying to simulate these constructs with functions, GScript effectively extends the language syntax, making it easy to use the new constructs in a natural way and in arbitrary combinations.

Here are the constructs in detail:

'if' statement
Code:
if ( condition ) {
  statements
}
else {
  statements
}
where condition is any boolean expression.
The statements can be any Avisynth statements, including the GScript extensions themselves, and so the new constructs can be nested.
The else part may be omitted (equivalent to else {}).

GScript also provides the 'else if' construct (optionally repeated to create a chain of conditionals). Thus
Code:
if ( condition ) {
  statements
}
else if ( condition ) {
  statements
}
else if (...) { ... }
...
else {
  statements
}
'while' loop
Code:
while ( condition ) {
  statements
}
The statements are repeated while the condition is true.

Example:
Code:
while (Height() < h) {
  StackVertical(last, c) 
}
'for' loop
Code:
for ( variable = init , limit , step ) {
  statements
}
init, limit and step are integer expressions, with step non-zero. step is optional and defaults to 1.

First the variable is set to the value of init.
The statements are repeated until the exit condition is met,
ie variable exceeds limit (if step > 0), or is less then limit (if step < 0).
After each iteration, variable is incremented by step.
If the initial value satisfies the exit condition, the number of iterations will be zero.

Example:
Code:
for (i=1, nBlurs) {
  Blur(0.5)
}
Using GScript
Once the plugin is loaded (either via LoadPlugin or by installing GScript.dll in your plugins folder), there are two ways to use the extended language features.
Firstly, a script (or part of a script) containing extensions can be passed as a text string to the GScript function (similar to the way functions like ScriptClip or MT are used). For example,
Code:
GScript("""
if (i > 10) {
  x = 1
  y = 2
  z = 3
}
else {
  x = 4
  y = 5
  z = 6
}
""")
The second way is to use the GImport function, which reads the entire contents of a file. This is similar to the standard Import, but supports the use of GScript extensions directly in the file.
The advantage of this is that you don't have to put quotes around the script, or worry about possible problems if the embedded script itself contains both triple and single quotes.
Thus, you can write entire scripts directly in the extended language and just pass to Avisynth a single GImport command to read it.
Code:
GImport("MyGScript.avs")
For completeness, there is also a function GEval, analagous to the standard Eval, which evaluates an arbitrary string that is permitted to contain GScript extensions.

Update: Version 1.1 (GScript_11.zip) released 6th Dec 2009
Attached Files
File Type: zip GScript_11.zip (76.1 KB, 4551 views)

Last edited by Gavino; 15th February 2010 at 13:58. Reason: Remove v1.0 dll from attachments
Gavino is offline   Reply With Quote
 

Tags
for-loop, if-then-else, while-loop

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 13:20.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.