Forums > Javascript

Recursively searching for named object

March 8, 2013 | 1:10 am

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

March 8, 2013 | 4:39 am

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


March 8, 2013 | 11:06 am

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

March 8, 2013 | 3:19 pm

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


March 8, 2013 | 3:35 pm

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


March 8, 2013 | 4:03 pm

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

March 8, 2013 | 4:35 pm

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?


March 8, 2013 | 4:58 pm

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

Jeremy



Nat
March 8, 2013 | 5:09 pm

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

March 8, 2013 | 6:01 pm

@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


March 8, 2013 | 6:14 pm

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



Nat
March 8, 2013 | 6:20 pm

@Anthony

How about this one ?

Attachments:
  1. Objtest.zip


Nat
March 8, 2013 | 6:23 pm

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


Viewing 13 posts - 1 through 13 (of 13 total)