Recursively searching for named object

Mar 8, 2013 at 1:10am

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?

Attachments:
  1. objTest.maxpat
#66912
Mar 8, 2013 at 4:39am

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

#240844
Mar 8, 2013 at 11:06am

Try this one. I still don’t find your object, but it’s iterating properly now. I made a couple of comments in the code.

[attachment removed due to error, see below]

Best, Jeremy

Attachments:
  1. objTest.js
#240845
Mar 8, 2013 at 3:19pm

@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.

#240846
Mar 8, 2013 at 3:35pm

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

#240847
Mar 8, 2013 at 4:03pm

By the way, the problem my semi-solution JS above is a missing () after the word subpatcher on line 49. The attached version works properly.

Attachments:
  1. objTest.js
#240848
Mar 8, 2013 at 4:35pm

Jeremy thanks for your help, it works like a charm. I have been using the following link as documentation
for the Max Javascript API: http://cycling74.com/wiki/index.php?title=Category:Javascript_In_Max_Docs
I can not find any documentation for subpatcher(). Am I missing something?

#240849
Mar 8, 2013 at 4:58pm

Hi, Anthony — it’s in the “The Maxobj Object” JavaScript vignette. Last method from the bottom.

Jeremy

#240850
Mar 8, 2013 at 5:09pm

Guys,

This is much easier to do with the applydeep function:

– Pasted Max Patch, click to expand. –

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);
}

Attachments:
  1. objTest2.js
#240851
Mar 8, 2013 at 6:01pm

@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

#240852
Mar 8, 2013 at 6:14pm

@Nat thanks for pointing this out. It is indeed easier and works!

#240853
Mar 8, 2013 at 6:20pm

@Anthony

How about this one ?

Attachments:
  1. Objtest.zip
#240854
Mar 8, 2013 at 6:23pm

No problem, nevermind my reply then (it was a reply to the message you deleted)

#240855

You must be logged in to reply to this topic.