Forums > Max For Live

Using the LiveAPI js object to iterate through children of a Live object

April 21, 2012 | 3:05 pm

I’m trying to use the LiveAPI js object to iterate through children of a Live object, e.g. all tracks in a live_set, or all clip_slots in a track. I’m having some problems doing this and I’m wondering whether I’m going about it the wrong way. For example, in my code below, api.children returns an array, one of which is called ‘tracks’, but api.children["tracks"] returns .

Does anyone have an example of how to do this correctly? Thanks.

inlets = 1;
outlets = 1;

post("global post");
post();

function bang()
{
	post("bang");
	post();

	var api = new LiveAPI("live_set");

	if (!api)
	{
		post("no live_set");
		post();
		return;
	}

	post("new api: ", api);
	post();

	post("info: ", api.info);
	post();

	post("children: ", api.children);
	post();

	post("children["tracks"]: ", api.children["tracks"]);
	post();

	var track;

	for(track in api.tracks)
	{
		post("track: ", track);
		post();
	}
}

April 21, 2012 | 4:37 pm

Think I’m getting there slowly…

for(var i = 0; i < api.getcount("tracks"); i++)
	{
		var track = new LiveAPI("live_set tracks " + i);
		post("track " + i + ": " + track.get("name"));
		post();
	}

April 21, 2012 | 5:39 pm

That looks about right. You’ll want to avoid instantiating new instances of the api object inside the loop however:

function test()
{
var api = new LiveAPI(this.patcher, "live_set");
var trackCount = api.getcount("tracks");

for (var index = 0; index < trackCount; index++) {
api.path = "live_set tracks " + index;
post(api.get("name"), ‘n’);
}
}


April 21, 2012 | 6:22 pm

Thanks willyc – but why should I re-use the same instance of the LiveAPI object. Is it because the object takes up a lot of memory? It’s just that I was planning on having very many LiveAPI objects at once, all observing individual paths simultaneously. It seems to be the only way to achieve another goal of mine, described here: http://cycling74.com/forums/topic.php?id=39445


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