Creating and naming objects

Dec 10, 2009 at 5:11pm

Creating and naming objects

hi,

I’m an aged, grizzled max user, who’s trying to get his head around JS and struggling, so please bear with me!

here’s what I want to do:

In an existing patch there are a couple of sub patchers (saved & loaded as objects), each with a fixed number of outlets (8 and 10).
I want to create [sends] and connect them to the outlets of the two existing objects.

Here’s the reason I want to use scripting. I want to name each of the sends based on arguments to the containing patcher.
Two arguments – start number, and range. The sends should be named (variable)ctrlval -
So with arguments 12 3 I would end up with 12ctrlval, 13ctrlval, 14 ctrlval
In fact the arguments would be X 18 , where X is the start value, and then there are 18 sends to create.

I’m sure this must be ridiculously easy, but after plodding through the tutorials I don’t really know how to go about doing this. I think I’m missing basic underlying programming concepts that the tutorials assume.

Any help much appreciated!

#47141
Dec 10, 2009 at 11:08pm

I am no scripting expert by any stretch of the imagination, but maybe this will get you started…
This will look for outlets in your patch and then create a send object below each outlet and name it. There is also applydeep() – for access to subpatches I believe – but I haven’t used that one yet…
hope this helps.

autowatch = 1;
var name = 0;

function bang() {
this.patcher.apply(sendIt);
}
/************************************************/
function sendIt(obj) {
if (obj.maxclass == “outlet”) {

this.patcher.newdefault(obj.rect[0], obj.rect[1] + 50, “send”, name+”string”);

name = name+1;
}
}

#169480
Dec 10, 2009 at 11:18pm

Have a look at the code I’ve pasted below. You need to give the scripting names of your two target objects as arguments ie [js createsends eight ten] if they were called “eight” and “ten”. Then when you send it a number it will use this as the offset and create all your [send] instances. At the moment they’re all created in the same place but if you do want to space them out evenly this is possible too. It’s hard-coded to do the eight first then the ten but you can switch that easily if you want to.

lh


function msg_int(offset) {
    target_a = this.patcher.getnamed(jsarguments[1]);
    target_b = this.patcher.getnamed(jsarguments[2]);
    for (i=0; i<8; i++) {
        name = i+offset+"ctrlval";
        obj = this.patcher.newdefault(0,0,"send",name);
        this.patcher.connect(target_a,i,obj,0);
    }
    for (i=0; i<10; i++) {
        name = i+8+offset+"ctrlval";
        obj = this.patcher.newdefault(0,0,"send",name);
        this.patcher.connect(target_b,i,obj,0);
    }
}
#169481
Dec 12, 2009 at 11:17am

Brilliant – thanks for that Luke.
I’m going to firstly figure out what’s going on in the script – which I’ll probably post back here to make sure I’ve understood. And then see if I can figure out from the tutorial how to distribute the created objects in the patch.

thanks again

#169482
Dec 12, 2009 at 12:27pm

ok. I’ve gone through this trying to figure out what it’s doing. Am I close? :-)

//function sets up what to look for in the first inlet – in this case it’s an integer. Presumably the “offset” in the brackets sets up the name for the input variable.

function msg_int(offset) {
target_a = this.patcher.getnamed(jsarguments[1]);

// creates a function (do I mean function?) called target_a, which is to find the object within the patcher that is named by the first argument to the js object

target_b = this.patcher.getnamed(jsarguments[2]);
// as target_a

//start loop
for (i=0; i<6; i++) {
// i=0 is the start value, i<6 is the range , and i++ adds "i" to the results of the previous iteration of this loop as long as i<6. If i >=6, it exits the loop.

name = i+offset+”ctrlval”.
// creates a local variable (function?) called name which is made up of the input number added to the offset followed by the symbol ctrlsend

obj = this.patcher.newdefault(0,0,”send”,name);
// creates a local variable (function?) called obj, which creates an object at the top left of the enclosing patcher (it’s the 0,0 I need to increment to place the sends in different locations), called send, followed by the variable name.
// does defining “obj” also create the object? (ie run the function?) I’m confused about when things happen!

this.patcher.connect(target_a,i,obj,0);
//connects target_a, outlet i, to the first inlet of the object created by the previous line
}
//loop 2, which does the same process for the second named object in the containing patcher
for (i=0; i<10; i++) {
name = i+6+offset+”ctrlval”;
obj = this.patcher.newdefault(0,0,”send”,name);
this.patcher.connect(target_b,i,obj,0);
}
}

#169483
Dec 12, 2009 at 12:53pm

And this is one (maybe not the most elegant) way of distributing the sends …

function msg_int(offset) {
target_a = this.patcher.getnamed(jsarguments[1]);
target_b = this.patcher.getnamed(jsarguments[2]);
for (i=0; i<6; i++) {
name = i+offset+”ctrlval”;
y = 300+(i*15);
obj = this.patcher.newdefault(300,y,”send”,name);
this.patcher.connect(target_a,i,obj,0);
}
for (i=0; i<10; i++) {
name = i+6+offset+”ctrlval”;
y = 300+(i*15);
obj = this.patcher.newdefault(500,y,”send”,name);
this.patcher.connect(target_b,i,obj,0);
}
}

#169484
Dec 13, 2009 at 4:28am

Just to make a few things clear, I’ll try to answer each of your comments one by one:

function msg_int(offset)
This looks for any integer received, floats will be truncated. There is only one inlet so in this script we don’t have to check which inlet was the receiver. The number sent to the object is assigned to the variable “offset”.

target_a
This is a variable not a function. The “=” is used to assign something to a variable to refer to it later in the code.

for (i=0; i<6; i++)
Here i++ means "add 1 to i each loop". It is the same as i += 1 and i = i+1

name = i+offset+"ctrlval"
This can be a bit confusing, the two +plus+ signs have different meanings. Both "i" and "ctrlval" are variables set as numbers so +plus+ adds them together. Then, because "ctrlval" is a string the +plus+ concatenates the previous sum and this sting.

obj = this.patcher.newdefault(0,0,"send",name)
Here the part following the =assignment= is used to create the [send] but "obj" is used as a variable to refer to it when making the patchcord connections in the next step. In javascript this is called its maxobj, you can't refer to objects specifically by scripting name or by class (object type) you have to get the maxobj which is what we do right at the beginning with the getnamed() function.

Apart from that you are spot on. If I can help any more let me know.

lh

#169485
Dec 13, 2009 at 12:19pm

Thanks again Luke, that all helps. I’ve just ordered a basic javascript book, which isn’t totally focussed on js for browsers (Javascript in 24 hours). I’m hoping that that will also help me get my head around the underlying concepts. The Max docs and tutorials are pretty good, but to me they assume that you’re already comfortable with the programming (with text) mind set – which I’ve never really acquired. Is there anything _you’d recommend for someone like me, or might that book do it? (A class would of course be great, but I don’t know of anyone doing js for max classes in London).

One immediate question – presumably with “for … ” , the “then” is included/implied? And the curly brackets set the limits of the loop?

thanks

David

#169486
Dec 13, 2009 at 4:56pm
#169487
Dec 13, 2009 at 6:06pm

The for loop doesn’t have a then. You might be thinking of the if conditional check which can have one. And yes, the {braces} set the block of code to be executed each iteration of the loop.

I don’t know of any courses in London specifically dealing with javascript in max specifically but for reference material the MDC javascript page is usually the first place I go when I get stuck, I think they explain everything really clearly. This is a good reference for different data types too, check out the numbers/strings/arrays/functions links at the top of the page.

I’m in London so if you ever feel you need something cleared up and reading about it just isn’t helping then get in touch.

lh

#169488

You must be logged in to reply to this topic.