Mewa User's Guide/Mewa Script Programming

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Language Reference[edit | edit source]

Mewa programming language, although resembling a lot like javascript, does not follow any programming language specfication. It was designed to answer Mewa application requirements on performance and ease of use.

The language syntax might change in order to match future quirements or users preferences.

Data Types[edit | edit source]

All numbers in Mewa are stored as floating-point.

Booleans true and false are stored as numbers 1.0 and 0.0.

Strings are represented by text within quotation marks, as shown below:

myString = "Hello";

if Conditionals[edit | edit source]

if(x == 0) 
{
    print('Zero')
}
else
{
    print('Non zero')
}

while Loops[edit | edit source]

while( download.inProgress() ) {
    dialog.setProgress( download.progress() );
}

do-while Loops[edit | edit source]

i=0;
do {
    // ...
    i+=1;
} while( i < 3 );

Functions[edit | edit source]

Below is a list of all functions supported by Mewa scripting.

If you need a function that is currently not available please contact us, we want to help you.

connectNodes( outNodeName, inNodeName, inputIndex )[edit | edit source]

Adds a connection between output of node with name outNodeName and input of node with name inNodeName. inputIndex is the index of the input port. Node input ports are indexed from 0 to N, where N is the number of input ports. In other words, the first input port of a node has index 0, the second input has index 1, and so on.

Example:

connectNodes("ColorWheel", "OverlayBlend", 0);

delete( var1, var2, ... )[edit | edit source]

delete function deletes variables. Accepts as input arguments a list of variables names.

The example below creates a node and then deletes the variable referencing the node. The data referenced by a variable is only deleted if the data is not referenced anywhere else.

myNode = ColorWheelNode();
delete( myNode );

Because the myNode is internally referenced (owned) by the nodegraph the myNode variable is deleted but the node is not.

deleteAll()[edit | edit source]

Deletes all set variables.

Note that deleting a variable does not necessarily delete the data referenced by the variable. The data referenced by the variable is only deleted if not referenced anywhere else.

See also #delete().

nodegraph()[edit | edit source]

Returns the #Nodegraph object.

Classes[edit | edit source]

ColorWheelNode[edit | edit source]

Creates a new ColorWheel node and adds it to the node-graph.

Constructors[edit | edit source]

  • ColorWheelNode()
  • ColorWheelNode( name, pos )

The constructor ColorWheelNode() adds a new ColorWheel node to the node-graph. The name and position of the node are automatically filled.

The constructor ColorWheelNode( name, pos ) adds a new ColorWheel node to the node-graph with the given name and at given pos. This constructor is used to save and restore a node-graph.

Dialog[edit | edit source]

Contructors[edit | edit source]

  • Dialog( text )
  • Dialog( text , title )

Creates and shows a Dialog with text. The title argument is optional.

Example:

dialog = Dialog("Do you wish to continue?");
dialog.addButton("Continue"); // button 0
dialog.addButton("Cancel"); // button 1
clickedOption = dialog.wait();
if( clickedOption == 1 ) { // if clicked cancel button
    dialog.close();
    return;
}

dialog.setText("Downloading update...");
clickedOption = dialog.wait();



addButton( text )[edit | edit source]

dialog = Dialog("Do you wish to continue?");
dialog.addButton("Continue");
dialog.addButton("Cancel");
clickedOption = dialog.wait(); // returns the button index when clicked
if( clickedOption == 1 ) { // clicked cancel button
    dialog.close();
    return;
}

addButton( text, functionHandler )[edit | edit source]

Example:

function onOkButton()
{
    dialog.close();
}

dialog = Dialog("Downloading update...");
dialog.addButton( "Ok", onOkButton );

setProgress( percent )[edit | edit source]

Progress value needs to be between 0 and 1. To hide the progress bar set a negative value (< 0). To show a running progress set a value bigger than 1 (> 1).

setTitle( title )[edit | edit source]

set empty text to hide

setText( text )[edit | edit source]

set empty text to hide

wait()[edit | edit source]

Returns only when a button is clicked. The returned value is the index of the clicked button.

close()[edit | edit source]

Closes and hides the dialog.


Dir[edit | edit source]

cd( dirName )[edit | edit source]

Enters directory dirName. Returns true if successfull.

exists( name )[edit | edit source]

name is a file or directory name. Returns true if file/dir exists.

size( name )[edit | edit source]

Returns the file size of the given file name. Returns -1 if the file does not exist.

delete( name )[edit | edit source]

Deletes the file or directory with given name.

makeDir( name )[edit | edit source]

Download[edit | edit source]

Contructors:

  • Download( dir, url )

Starts a downloads the specified url to the given dir.

progress()[edit | edit source]

Returns the downloaded data size in bytes.

status()[edit | edit source]

Return an integer, that can be either 1, 2 or 3:

  1. in progress
  2. completed
  3. failed

cancel()[edit | edit source]

Cancels the running download.


FootageNode[edit | edit source]

Constructors[edit | edit source]

  • FootageNode( source )
  • FootageNode( source, pos, name )

source is a video filename or a list of filenames of images (image sequence). pos is a vector with 2 numbers.

node = FootageNode( "C:\MyVideos\sun.mp4" );

See also Node class.

Move2DNode[edit | edit source]

Constructors[edit | edit source]

  • Move2DNode()
  • Move2DNode( name, pos )

The constructor Move2DNode() adds a new Move2D node to the node-graph. The name and position of the node are automatically filled.

The constructor Move2DNode( name, pos ) adds a new Move2D node to the node-graph with the given name and at given pos. This constructor is used to save and restore a node-graph.


Nodegraph[edit | edit source]

The nodegraph() function returns a Nodegraph object.

setTranslation( x, y )[edit | edit source]

Translates the node-graph view to position x,y.

Node[edit | edit source]

Node is the base class for all Mewa nodes, like #FootageNode and #ShaderNode.


setName( name )[edit | edit source]

Sets the given name to the node. If the name already exist, a number is added at the end of the name in order to make it unique.

setPos( x, y )[edit | edit source]

setHelpPage( url )[edit | edit source]

[panel image]

Use this function associate a help page to a node. The help page is url link. The link is opened in the system web browser when the user clicks [help button] located at the top of node parameters window.


ShaderNode[edit | edit source]

Constructors[edit | edit source]

  • ShaderNode( glslCode, nameHint )
  • ShaderNode( glslCode, name, pos )

Adds node to the node-graph.

nameHint is the name given to the node while generating a unique name for the node. If there is already a node with name nameHint a number will be added at the end of the nameHint to make it unique.

If pos is not provided the node will be placed at a "best guess" position.

The best way to learn on how to create shader nodes is to look at examples. Mewa web store provides many scripts with it's source available. One example is Ether

To fill the node's panel use the functions #addFloatControl, #addVec2Control and #addColorControl. Each of those functions adds one row to the node's panel, starting from the bottom.

Controls[edit | edit source]

Controls are graphical widgets used to change the value of input variables in the shader program. There are 3 different control types:

By default the controls will show the name of the variable as a text label. The label text can be modified by through the function:

  • setName( name )

The control name should be unique within the node as the name can be used in the control search.

addFloatControl( variableName , defaultValue )[edit | edit source]

Adds a GUI control to the node [Mewa_User%27s_Guide/Using_the_Node_Graph | node panel] to change the value of a given variable in the shader program.

variableName is the name of variable in the shader source that will be controlled.

File:FloatControl.png
Float Control

Returns a #FloatControl object.

addVec2Control( variableName, defaultValues )[edit | edit source]

File:Vec2Control.png
Vec2 Control

addColorControl( variableName )[edit | edit source]

This function creates a #ColorControl connected to variable with name variableName and initialized with black color (0,0,0). See more at #ColorControl.

addColorControl( variableName, gray )[edit | edit source]

gray argument is a number. This function creates a #ColorControl connected to variable with name variableName and initialized with gray value set on the three channels R,G and B.

addColorControl( variableName, red, green, blue )[edit | edit source]

The color control has 3 parameters. Clicking on the color button expands to show the 3 parameters.

setRenderArea( area )[edit | edit source]

The input should be "iResolution", "iChannel0" or "iChannel0+iChannel1".

setRenderArea options
setRenderArea options


  • setRenderArea("iResolution") An example of a node using setRenderArea("iResolution") is Ether. This example uses the input coordinates passed into fragCoord in mainImage( out vec4 fragColor, in vec2 fragCoord ) to generate a procedural image. Because "iResolution" option fills the entire viewport using fragCoord values that go always from 0 to 1, this option is mostly used to generate images.
  • setRenderArea("iChannel0") An example of a node using setRenderArea("iChannel0") is HexPixelate. The iChannel0 option automatically adds an input port to the node. to connect an input image. The input coordinates fragCoord passed into mainImage( out vec4 fragColor, in vec2 fragCoord ) cover only the input image. Also, note that fragCoord coordinates can have any range of values within the 0 to 1 limits. Use the texture size in variable iChannelResolution[0] to get, always, a 0 to 1 range in the fragCoord input variable. The iChannel0 option is aminly aimed at the creation of image processing nodes, taking an input image for processing and outputing the result.
  • setRenderArea("iChannel0+iChannel1") An example using setRenderArea("iChannel0+iChannel1") can be found in OverlayBlend. This option is mostly used to merge 2 images. It adds 2 input ports to the node. When using the iChannel0+iChannel1 option make sure to be use mainImage( out vec4 fragColor, in vec2 fragCoordA, in vec2 fragCoordB ) function in your shader source to access the texture coordinates of both images. Use the texture size in variable iChannelResolution[0] to get, always, a 0 to 1 range in the fragCoord input variable.


setFilter( channel, filter )[edit | edit source]

Options are Nearest, Linear or Mipmap

setWrap( channel, filter )[edit | edit source]

Options are Clamp or Repeat.

setFlip( channel, filter )[edit | edit source]

Options are NoFlip, HorizontalFlip or VerticalFlip.

ColorControl[edit | edit source]

Color Control

Create a color control initialized with red color. uColor is the name of the vec3 uniform variable in the glsl shader code.

parameter = node.addColorControl("uColor", 0.8, 0.2, 0.2 );

Color control has 3 parameters, one for each color channel.

setName( name )[edit | edit source]

Sets the name of this control. name is shown in the control as a text label.

See also Controls section.

FloatControl[edit | edit source]

The code below, taken from Ether.mw, shows how a FloatControl is used. The function addFloatControl creates a control, adds it to the node panel and returns the control for further setup.

parameter = node.addFloatControl("uDelta", 2.5);
parameter.setName("Delta");
parameter.setStep(0.01);
parameter.setRange(-2, 7);

setRange( min, max )[edit | edit source]

Sets a minimum and maximum value a parameter can have. To disable min and max, letting the UI control to reach any value set the same value for min and max (min == max).

setStep( increment )[edit | edit source]

Adjust the step value to make a parameter increase faster or slower with the mouse movement.

setName( name )[edit | edit source]

Sets the name of this control. name is shown in the control as a text label.

The control name should be unique within the node as the name can be used in the control search.


Vec2Control[edit | edit source]

setName( name )[edit | edit source]

Sets the name of this control. name is shown in the control as a text label.

See also #Controls.