Walking the LOM tree in Javascript

Learn about building and using Max for Live devices.
Post Reply
TomSwirly
Posts: 109
Joined: Wed Apr 30, 2008 7:55 pm

Walking the LOM tree in Javascript

Post by TomSwirly » Fri Jan 15, 2010 2:35 am

Hello all!

Long time, no see. I've written an awful lot of Javascript for Max For Live code since last I appear here, and a paper or two...

I have lots of utilities and little toys to show off in the next few weeks, including a Morse code generator and a generalized command mapper program... but I have some questions.


To start, I'd like to walk through the entire LOM tree and "print it out". But this reasonable task has run into many difficulties.

1. PROBLEM: the LOM it isn't a tree - in fact, there are cycles.

SOLUTION: The documentation warns you of this. Just keep track of which IDs you've seen already and don't recurse into them a second time.


2. PROBLEM: live.children is SNAFUED for objects that don't have children.

If you go to a path location that has an object of type 'list' or 'tuple', live.children is NOT empty - instead it's a two element array looking like ['No', 'children'] (!).

SOLUTION: (Hack) look at types and don't recurse into bad types.


3. PROBLEM: calling live.children consistently crashes Live for reproducible values of live.path.

Code: Select all

function run() {
  var live = new LiveAPI(this.patcher);

  live.path = [ 'this_device',
                'canonical_parent',
                'canonical_parent',
                'master_track',
                'mixer_device',
                'cue_volume',
                'canonical_parent',
                'canonical_parent',  // Doesn't crash if this line is commented out.
                ];

  outlet(0, live.id);
  for (var i = 0; i < live.children.length; ++i) {
    outlet(0, live.children[i]);
  }
};
SOLUTION: (Hack)

The "canonical_parent" child seems a bit... dodgy anyway. I disallow following those children.


4. How do I find the list of parameters on an object? I don't see any form of reflection.

SOLUTION (proposed horror): keep massive tables from object type (e.g. DeviceParameter) to the parameters.
there must be a better way...?


5. How do I iterate through or index things that are of type "list" or "tuple" - like "control_surfaces"?

SOLUTION: ??


6. Many parameters I'd want to set seem to be read only...

What's the Javascript equivalent of live.remote?

Thanks in advance, great Max For Live gurus!

TomSwirly
Posts: 109
Joined: Wed Apr 30, 2008 7:55 pm

Re: Walking the LOM tree in Javascript

Post by TomSwirly » Fri Jan 15, 2010 7:12 pm

Just refreshing this to put this at the top of people's stacks.

These questions are sort of fundamental to using Max for Live in Javascript - if we knew all the answers, we could do "anything we liked".

Plus, as an additional bonus, I can finish this "LOM pretty-printer" which is supposed to dump an image of your current LOM to a text file for your edification.

My medium-term plan is to be able to store and load LOM descriptions as JSON files so you should be able to recall any setting of your faders, effects, and etc. with "the touch of a button".

halley
Posts: 40
Joined: Sun Jun 28, 2009 8:48 am

Re: Walking the LOM tree in Javascript

Post by halley » Sat Jan 16, 2010 12:37 am

for question 5.

I loop on control_surfaces like this:

Code: Select all

var api = new LiveAPI(this.patcher);
	
ctrlSurfCount = api.getcount("control_surfaces");
for(var c = 0; c < ctrlSurfCount; c++)
{
    api.path = ["control_surfaces",  c];
    
    // here you can check/use current control surface properties
    // for instance:
    if(api.type == "APC40")
    {
        apcIndex = c;
    }
}

TomSwirly
Posts: 109
Joined: Wed Apr 30, 2008 7:55 pm

Re: Walking the LOM tree in Javascript

Post by TomSwirly » Sun Jan 17, 2010 12:07 am

Interesting! I wonder if this also works for tuples...? I'll try it out!

mdk
Posts: 914
Joined: Sun Jul 31, 2005 3:51 pm
Location: Skopje, Macedonia
Contact:

Re: Walking the LOM tree in Javascript

Post by mdk » Sun Jan 17, 2010 11:41 am

4. How do I find the list of parameters on an object? I don't see any form of reflection.
take a look at the output from pathObj.info(), not sure how easy or hard it is to parse.
Pr0k Records - Bandcamp Facebook Twitter

Post Reply