How to use getnamed() to seek in subpatchers, not only in main patcher?

Roald Baudoux's icon

Hello,

I'm trying to find named objects in my patcher but the patcher.getnamed() function doesn't seem to look in subpatchers. Is there a way to seek into all subpatchers automatically?

Thank you.

TFL's icon

If you know the location of the object you can do something like this:

let my_obj = this.patcher.getnamed('subpatcher_varname').subpatcher().getnamed('object_varname');

Replace subpatcher_varname and object_varname by the subpatchers and objects scripting names respectively.

If you don't know the path and need to search through all subpatchers, it gets a bit more tricky:

let my_obj = null;

function loadbang() {
	this.patcher.applydeepif(function(maxobj) {
		my_obj = maxobj;
		post(my_obj.getvalueof());
		post();
	}, function(maxobj) {
		return maxobj.varname == 'my_object_varname';
	});
}

It relies on this.patcher.applydeepif() which has to be called with two functions: the first one is what to do with some objects matching a given condition, and the second fonction should return true when the object meets the desired condition.

This code will, at loadbang, assign the object named 'my_object_varname' to the 'my_obj' js object and print its value.

Max Patch
Copy patch and select New From Clipboard in Max.

Roald Baudoux's icon

Thanks a lot TFL!

TFL's icon

It's worth mentioning that the first function-argument of applydeepif() will run for every object that have 'my_object_varname' as a scripting name, so if you have multiple instances of the same abstraction/bpatcher, or simply use that same scripting name in several subpatchers, you might need to adapt the logic, as in my example my_obj will only refer to the last found object.