Send message to bpatcher inlets

BasS's icon

Hi,

I would like to know if it is possible to send a message to an inlet of a bpatcher.
I'm trying to do this with sendbox, but can't get it working.

I've tried to send this to thispatcher:

"script sendbox ObjectName 1 setitem 0 3"

to input "setitem 0 3" in inlet 1 of "ObjectName".

Kind regards,
Bas

BasS's icon

Anybody?

Luke Hall's icon

Here's an example that uses javascript. It would be possible to do this with messages to [thispatcher] although in my opinion this would be a bit unwieldy.

The problem seems to stem from the fact that you don't really want to connect to the [bpatcher] you want to connect to the patch that is loaded into the [bpatcher]. I'm not sure if this is the best way to do it but here goes.

The patch lets you send the message "script " to the js and it will work with objects including [bpatcher]s.

The usual method is to use message() but this won't work with [bpatcher]s. The workaround is to connect to the target object, send the message and then disconnect all in the blink of an eye. It would be possible to use this workaround method exclusively but I assume it is better to use message() where you can. It would also be possible to specify the inlet that you want to connect to if you do it this way.

I hope it helps.

lh

// sendto.js

function script()
{
var a = arrayfromargs(arguments);
dest = a.splice(0,1);
var myobj = this.patcher.getnamed(dest[0]);
if (myobj == null)
{
post("object named "+dest[0]+" could not be foundn");
} else {
if (myobj.maxclass == "patcher")
{
this.patcher.connect(this.box,0,myobj,0);
outlet(0,a);
this.patcher.disconnect(this.box,0,myobj,0);
} else {
myobj.message(a);
}
post("target: "+dest[0]+"nmessage: "+a+"n");
}
}

// EOF

BasS's icon

Hello LH,

*Better late than never, sorry for my late response* Thank you very much for this javascript, and for the other tips!

It works great. I can send offset messages to the bpatcher, and also messages that go to the (first) input.

But is it also possible to send messages to other inputs?
Otherwise I can always route the info of course.

btw, I don't get what you mean with using message(), you could explain this further?

Kind regards,
Bas

Luke Hall's icon

Well here's the new and improved version. Just change the format of the message you send it to "script " and remember that the first inlet is 0 not 1.

The message() thing is a way to send an object a message in javascript. I'm not using this method because for some reason it doesn't work with [bpather]s (or I'm doing something wrong). So, my workaround is to script a patchcord between the [js] and the object whose scripting name you specify, send the message from the outlet of the [js] and then delete the patchcord. This works just fine and you can specify the inlet you want to connect send to. I hope this explains it a bit better. I could send you a fully commented version of the code, just ask if you want one.

lh

// sendto.js

function script()
{
a = arrayfromargs(arguments);
dest = a.splice(0,1);
thein = a.splice(0,1)
myobj = this.patcher.getnamed(dest);;
if (myobj == null)
{
post("object named "+dest[0]+" could not be foundn");
} else {
this.patcher.connect(this.box,0,myobj,thein);
outlet(0,a);
this.patcher.disconnect(this.box,0,myobj,thein);
}
post("target: "+dest+"ninlet: "+thein+"nmessage: "+a+"n");
}

// EOF

BasS's icon

Wow, that's a fast improvement!

Thank you very much!

Kind regards,
Bas

Luke Hall's icon

No problem at all. I've only been delving into javascript in max in the last few weeks but it's fairly versatile and useful as you can see!

lh

Dan Laüt's icon

I am developing a program to explore complex measures. From 2 up to 32 bpatchers, each containing a pitch an velocity value, a comment indicating the beat number and a firing pin, are created via thispatcher. I want to set the comment of each (and other the other values at a later stage) via thispatcher. Problem: one cannot address the objects inside a bpatcher directly via thispatcher.
I found a solution posted on the forum:
1) create a message in the patcher containing the bpatchers,
2) connect it via thispatcher to the inlet of the bpatcher,
3) set the message and bang it via thispatcher, and
4) disconnect the message from the bpatcher.

Implementation:
(The 'nr' routes it in the bpatcher to the comment)

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