Recursively searching for named object
I am trying to come up with a js function that will recursively search a patch for a named object...
What I am finding is that I am not able to look inside subpatchers. It looks like when I call firstobject of a patcher object it does not return anything. I have reviewed my code, but I don't see any obvious problem.
Does firstobject work differently than I am using it? Am I missing something?
Hi Anthony,
I haven't looked closely at your JS code, and I'm sure you have a good reason for doing this via the JS/Max API.
Another way to do it though would be to search the JSON data structure of the main patch and/or other embedded abstraction patches using dict, or using your own JSON code in Javascript (I have a dict-like js object for Max5: http://www.zacharyseldess.com/software/z.jsonIO_013112.zip).
best,
Zachary
@Zachary the reason why I am doing this in js is because I am developing a 3D scene rendering interface in js. For this task, implementing things in javascript is so much easier than patching things. Of course it requires that I be able to access objects from javascript. Having to search a JSON data structure seems a little convoluted when I have javascript methods that are supposed to traverse the patcher. I will certainly look at the example you posted.
Also, searching the JSON structure directly is not exactly future-proof. We might change the patcher document format again, or start saving patchers in "Copy Compressed" format, or introduce a binary format. Or maybe it's a M4L device and there's no valid JSON document available, or, or, or... Anyway, the JS I posted should be appropriate for all anticipated version of Max in which patchers and objects are still part of the landscape.
Best, Jeremy
Jeremy thanks for your help, it works like a charm. I have been using the following link as documentation
for the Max Javascript API: https://cycling74.com/wiki/index.php?title=Category:Javascript_In_Max_Docs
I can not find any documentation for subpatcher(). Am I missing something?
Hi, Anthony -- it's in the "The Maxobj Object" JavaScript vignette. Last method from the bottom.
Jeremy
Guys,
This is much easier to do with the applydeep function:
autowatch = 1;
var scripting_name = null;
function findObject(obj)
{
if (obj.varname == scripting_name)
{
post(obj.varname);post();
post(obj.maxclass);post();
}
}
function find(_scripting_name)
{
scripting_name = _scripting_name;
this.patcher.applydeep(findObject);
}
@Anthony, Jeremy, yes of course you should do this in the officially supported way, if that exists (which it does apparently).
@Jeremy, binary format sounds nice! If for nothing else, would be great to export as standalone using BSON or similar for smaller package size, etc. I know you were just making a point of future-proofing your patches, but anyways...
best,
Zachary
@Nat thanks for pointing this out. It is indeed easier and works!
No problem, nevermind my reply then (it was a reply to the message you deleted)
Great! Thanks. :)