Max patch instrospection
I'm curious about introspection in Max patches. Is there a way to query patches to receive information on the current objects, or query max to get info on current patches, etc? I'm specifically curious to do this while patching, not on traversing a saved .maxpat file with another language.
I'm thinking of something along the lines of a message:
"; max listObjects"
to get a list of all objects in patch, or in JS / NodeJS:
this.patcher.listObjects()
Max.listPatches()
etc...
Does anyone know of any resources or methods? If this can be done in NodeJS or js, that works well. Just looking to stay inside of Max
This javascript comes from this forum, but not sure which thread:/*
www.leafcutterjohn.com August 2013
based on 'patchdescribe.maxpat' which is a simple example of iterating through
all objects in a patch (included in the Max examples).
*/
// set up inlets/outlets/assist strings
outlets = 2;
setoutletassist(1,"clear message for coll");
setoutletassist(0,"patcher.name / maxclass ");
function bang()
{
outlet(1,"clear");
this.patcher.applydeep(iterfun);
}
function iterfun(b)
{
outlet(0, b.patcher.name + " " + b.maxclass );
return true;
}
iterfun.local=1; // keep private
Name it jpatchmap.js
And here is the example patch for it:
It will iterate through any subpatchers that are in the patch, as illustrated in the example
This is beautiful! Thank you so much for sharing!
Before I go about building something out from this, it looks like this stuff is designed to be formatted as a dictionary / object. Does any code exist to convert this to JSON?
i dont remember offhand now, but listing all objects in a patcher can be achieved by some key combination and clicking in the patcher window since ever.
this is even less work than the other option, using the file menu.
I think you'd need to hand-code it; have a look at the "Javascript in Max" pdf that should come with Max, especially the Patcher Object section. I think you'd probably need to iterate through the patcher, place object names in an array, then stringify and format the string. The stringification is helped using JSON.stringify().
an example:function bang() {
var first = [1, 2];
var second = [3, 4];
var nested = [first, second];
post(first);
post();
post(second);
post();
post(nested);
post();
post(JSON.stringify(nested));
post();
}
But I haven't done anything like this in years and I'm no js expert, so there might be a way more straightforward way to get info into a dict