Little JS Syntax Bible in Max/MSP
Feb 09 2022 | 2:03 pm
Here I want to collect all the less intuitive Javascript syntax and techniques for Max.
Feel free to comment everything that comes to your mind related to the subject, I'll add it to the list.
Max JS uses ES5 version, so features like "let" and Classes added in ES6 will not work.
Max's documentation for JS:
https://docs.cycling74.com/max8/vignettes/javascriptinmax
Tim's JS Reference:
http://max-javascript-reference.tim-schenk.de/
Get keys pressed in JS:
https://github.com/cskonopka/maxJS
Max Scripting
- Create a new Max object: var myObj = this.patcher.newdefault(<obj_pos_x>, <obj_pos_y>, "<max_obj_name>");
- Add arguments to Object when creating it: var anotherObject = p.newdefault(100, 100, "combine", "text", "::", "moretext")
- Move Object:maxObject.message("patching_position", [posX, posY]);
- Create a new "bpatcher" object with arguments: var myNewObject = p.newdefault(100, 100, "bpatcher", "@name", "myPatch", "@args", name);
- Rescale Max Object: this.patcher.script("sendbox", <max_obj>.varname, "patching_rect", [ <obj_pos_x>, <obj_pos_y>, <obj_size_x>, <obj_size_y> ]);
- Bring to Front/Back Max Object: this.patcher.script("bringtofront" / "sendtoback", <max_obj>.varname);
- Connect two Max objects: this.patcher.connect(<max_obj_1>, <outlet_index>, <max_obj_2>, <inlet_index>);
- Send a message to a Max "receive" object from within JS: messnamed("<receive_obj_name>");
- Get a named Max object and assign it to a JS object: var myMaxObj = this.patcher.getnamed("<obj_varname>");
- Resize a BPatcher that contains the JS object: this.patcher.box.rect = [<leftTopX>, <leftTopY>, <bottomRightX>, <bottomRightY>]
JSUI
- Get the rect of the JSUI object box: var objRect = box.rect.slice(); Get size of box.rect = var objSize = [box.rect[2] - box.rect[0], box.rect[3] - box.rect[1]]; box.rect can also be set.
Folder Object
- Create a Folder object and read the file names in the folder: var myFolder = new Folder(<folder_path>); Set Folder typelist (an empty typelist will make the Folder object read every file type): myFolder.typelist = ["<file_type>", "<file_type>", etc...]; Read the Folder file names: while (!myFolder.end) { post(myFolder.filename); myFolder.next(); }
Dict Object
- Basic querying: function dictionary(dictname) { var d = new Dict(dictname); outlet(0, d.get('layer1key::layer2key'); }
- Iterating through layers of a Dict: function dictionary(dictname) { var d = new Dict(dictname); var layer1 = d.getkeys(); layer1.forEach(function(key) { var layer2 = d.get(key).getkeys(); } }
Max Object Listener
- Listen to a Max Object for attributes or value changes: var myListener = new MaxobjListener(<max_obj>, <callback_function>); The Callback function can be used like this: function MyCallback(data) { data.value; // the output of the Max object data.maxobject; // the object itself }
- To make the "this" inside a callback refer to the current object, you can write the callback directly inside the object in this way:
var MyCallback = (function(data)
{
post(this.myObjProperty);
} ).bind(this);
Jitter in Javascript
- Create Jitter Matrix: var myMatrix = new JitterMatrix("<name_optional>", <int_number_of_planes>, "<type>", [<int_dim_0>, <int_dim_1>, ... ] );
- Get name of object as registered from Max: var objRegName = <jsJitterObj>.getregisteredname();
- Create Jitter Object: var myJitterObj = new JitterObject("<obj_name>", <argument>); Example: var myJitterObj = new JitterObject("jit.gl.gridshape", "<drawto>");
- Free Jitter Object Memory (must be done every time a Jitter object is deleted in JS): myJitterObj.freepeer();
- Use a "jit.gl.pix" object within JS (same for "jit.gl.slab"): var myGLPix = new JitterObject("jit.gl.pix"); myGLPix.gen = "<genjit_file_path>";
Set the input you want to send the next texture:
myGLPix.activeinput = <int_input_index>
Set parameters:
myGLPix.param("<param_name>", <param_value> / [<array_of_param_values>]);
Get the processed texture:
myGLPix.jit_gl_texture("<texture_to_process_name>");
myGLPix.draw(); // IMPORTANT, the jit.gl.pix obj needs also to draw
var myProcessedTexture = myGLPix.out_name;
- Play a movie with "jit.movie" and get a texture output from within JS: First create a dummy matrix, the dimensions don't matter: var dummyMat = new JitterMatrix(4, "char", 320, 240); jit.movie needs output_texture enabled: var moviePlayer = new JitterObject("jit.movie"); moviePlayer.output_texture = 1; Get new frame and output texture: function LoadNewFrame() { moviePlayer.matrixcalc(dummyMat, dummyMat); outlet(0, moviePlayer.texture_name); }
- Use a JitterListener to listen to change of state in Jitter Objects: https://docs.cycling74.com/max8/tutorials/jitterchapter47?q=jitterlistener
Max 4 Live
- Max 4 Live JS guide by Adam Murray: http://compusition.com/writings/js-live-api-setup