How to properly create a named CursorTrack?

Post Reply New Topic
RELATED
PRODUCTS

Post

Hi,

I am trying to create a named CursorTrack using host.createCursorTrack in JavaScript, but it seems I am not able to figure out how to do it right.

I am using the following code in init():

Code: Select all

mct = host.createCursorTrack("mctid", "mctname", 2, 0, false);
mct.name().addValueObserver(function(value) {
println("mct name is: " + value)
});
Indeed mct then seems to be an object of type RootControlSurfaceObject/CursorTrackProxy/, but when later in onMidi0() or in the console I try things like

Code: Select all

mct.selectFirst()
mct.selectNext()
I would expect the named CursorTrack to move from first to second track, causing the observer function to be called and printing the respective track names, but nothing happens.

Interestingly, when I do the same with a non-named cursor, namely

Code: Select all

uct = host.createCursorTrack(2, 0);
uct.name().addValueObserver(function(value) {
println("CursorTrack name is: " + value)
});
this works perfectly, nicely calling the observer functions and printing track names when doing

Code: Select all

uct.selectFirst()
uct.selectNext()
I also tried creating a named cursor that follows the user selection, which seems like it should be exactly like the above unnamed cursor

Code: Select all

mct = host.createCursorTrack("mctid", "mctname", 2, 0, true);
but that does not produce a working named CursorTrack either.

How I can go about creating a named cursor that actually works? Thanks for any help!

Post

Since the named cursor tracks are independent from the Bitwig display "they do not know where they are". Therefore, you also need to create a trackbank and assign the cursor track to it with "followCursorTrack":

Code: Select all

    cursorTrack = host.createCursorTrack ("mctid", "mctname", 2, 0, false);
    trackBank = host.createMainTrackBank (8, 2, 0);
    trackBank.followCursorTrack (cursorTrack);
Just tested that to not tell you something wrong and noticed that Bitwig seems to cache those banks once created and do not change them until you restart Bitwig, which means changing your code has no effect! It even seems to share them among all scripts/extensions which is pretty bad.

Post

Many thanks for the quick reply, I never would have thought of this - to me it seemed that TrackBank.followCursorTrack was controlling the TrackBank and needed a defined cursorTrack to begin with. So then it actually seems to be the other way around ...

But there must be some other magic I am missing, I still cannot get this to work, here is the complete script

Code: Select all

loadAPI(10);
host.setShouldFailOnDeprecatedUse(true);
host.defineController("Test", "NamedCursorTest", "0.1", "92fe056b-4306-4b22-a015-880f42c9b328", "Test");

function init() {
   cursorTrack = host.createCursorTrack ("mctid", "mctname", 2, 0, false);
   trackBank = host.createMainTrackBank (8, 2, 0);
   trackBank.followCursorTrack(cursorTrack);
   cursorTrack.name().addValueObserver(function(value) {
   println("cursorTrack name is: " + value)
});
}

function flush() {}
function exit() {}
which just prints

Code: Select all

Called init()
cursorTrack name is:
in the console. Whereas if instead you create the CursorTrack with

Code: Select all

cursorTrack = host.createCursorTrack (2, 0);
the script will print

Code: Select all

Called init()
cursorTrack name is:
cursorTrack name is: Inst 1
to the console. Both scripts were tested by loading them into a freshly started Bitwig with a "New Project" containing the default two tracks.

Beats me, seems like these named cursors are a bit tough to grasp ... thanks for any additional hints you might have!



PS I did also notice some weird caching. When trying out zillions of ways of initializing the named CursorTrack, in one of my test projects at some point I "succeeded" in making it point to actual tracks. Now every time I load that project, the named CursorTrack is properly instantiated, even though all my trial code was removed from the script. In a fresh project, that same script fails to produce an instantiated named CursorTrack. So it seems Bitwig is even storing things in the project?

Anyways, I have no idea any more how I got into that state, and on top, the created named CursorTrack incorrectly follows the user selection despite being created with shouldFollowSelection=false. Go figure ...

 

Post

kvrsw2424 wrote: Thu Jan 02, 2020 10:40 pm But there must be some other magic I am missing, I still cannot get this to work, here is the complete script
...
Since you set the last parameter in createCursorTrack to false you still need to call cursorTrack.selectFirst() or selectNext() to see an output.

BTW You can simply type that in the JavaScript console!

Post

Bingo, that's it, thanks so much!

I actually had tried cursorTrack.selectFirst() already, but I guess my mistake was to do that in the init() function - it has no effect if you put it there. It works when typing it in the console, or when calling it later as part of onMidi0(), for example (just tested).

So thanks again for your help, and for the benefit of everybody else who might be wondering, here is the complete working script:

Code: Select all

loadAPI(10);
host.setShouldFailOnDeprecatedUse(true);
host.defineController("Test", "NamedCursorTest", "0.1", "92fe056b-4306-4b22-a015-880f42c9b328", "Test");
host.defineMidiPorts(1, 0);

function init() {
host.getMidiInPort(0).setMidiCallback(onMidi0);
cursorTrack = host.createCursorTrack ("mctid", "mctname", 2, 0, false);
//cursorTrack = host.createCursorTrack (2, 0);
trackBank = host.createMainTrackBank (8, 2, 0);
trackBank.followCursorTrack(cursorTrack);
cursorTrack.name().addValueObserver(function(value) {
println("cursorTrack name is: " + value)
});
}

function onMidi0(status, data1, data2) {
cursorTrack.selectFirst();
}

function flush() {}
function exit() {}

Post

kvrsw2424 wrote: Fri Jan 03, 2020 4:14 pm I actually had tried cursorTrack.selectFirst() already, but I guess my mistake was to do that in the init() function - it has no effect if you put it there.
A trick to work around this is to delay it a bit:

Code: Select all

host.schedulaTask (function() { cursorTrack.selectFirst(); }, 1000);

Post

Great, even better, will try, thanks!

Post Reply

Return to “Controller Scripting”