Scripting: Is there a way to call the "New Loop Region" method from the scripting API?

I am on Studio v2.02.20 and i have been trying to find out if there is an easy way to add a new loop region for the selected instrument. I can add a new loop region through code just fine, but it would be quite handy if there was like 1 line of code for calling the same method used when doing “Right Click > New Loop Region” on an instrument or in the Track Markers.

Hi,

Unfortunately, there isn’t a single function to do this. I have made a simple script to create a loop region around the first instrument in an event:

/* -------------------------------------------
   FMOD Studio Script Example:
   Add a new loop region to the currently selected events first instrument
   -------------------------------------------
 */

studio.menu.addMenuItem({
    name: "Add Loop Region to currently selected Event",
    isEnabled: function() { var event = studio.window.browserCurrent(); return event && event.isOfExactType("Event"); },
    execute: function() {
        // Retrieve the current selected event
        var event = studio.window.browserCurrent();

        // Retrieve the necessary for a valid loop region
        var firstInstrument = event.timeline.modules[0]
        var start = firstInstrument.start;
        var end = start + firstInstrument.length;
        var timeLine = event.timeline;
        var markerTrack = event.markerTracks[0];

        // Create the new loop region around the first instrument in the event
        var newLoopRegion = studio.project.create("LoopRegion");
        newLoopRegion.position = start;
        newLoopRegion.length = end;
        newLoopRegion.selector = event;
        newLoopRegion.timeline = timeLine;
        newLoopRegion.markerTrack = markerTrack;

        if (newLoopRegion.isValid == false)
        {
            alert("Failed to crete loop region");
            studio.project.deleteObject(newLoopRegion);
        }
        else
        {
            alert("Created loop region");
        }
    }
});

You could expand this to accept instrument indexes to more closely fulfill your needs. Please note there is minimal error handling in the script.

Hope this helps

That’s a shame that its not as simple as there are a couple of quality of life things that are quite nice about that function, but i understand that there are reasons why it’s not available. Though it would be nice in the future :wink:

Thanks for the script example - this is different from the way i was doing it but it definitely gives some good insight into how to make loop regions.

One of the features that im not sure of how to do though is setting the region on the next available MarkerTrack. In this you have used “event.markerTracks[0],” but when doing this i find that it will make regions overlap if there is already one on that marker track.
So how can i make the new region go onto the next available marker track like the “Right Click > New Loop Region” method does?

I have updated the code to add a new marker track so that they do not overlap:

/* -------------------------------------------
   FMOD Studio Script Example:
   Add a new loop region to the currently selected events first instrument
   -------------------------------------------
 */

studio.menu.addMenuItem({
    name: "Add Loop Region to currently selected Event",
    isEnabled: function() { var event = studio.window.browserCurrent(); return event && event.isOfExactType("Event"); },
    execute: function() {
        // Retrieve the current selected event
        var event = studio.window.browserCurrent();

        // Retrieve the necessary for a valid loop region
        var firstInstrument = event.timeline.modules[0]
        var start = firstInstrument.start;
        var end = start + firstInstrument.length;
        var timeLine = event.timeline;

        //Create a new marker track
        var track = studio.project.create("MarkerTrack");
        track.event = event;
        if (track.isValid == false)
        {
            alert("Failed to crete marker track");
            studio.project.deleteObject(track);
            return;
        }

        // Create the new loop region around the first instrument in the event
        var newLoopRegion = studio.project.create("LoopRegion");
        newLoopRegion.position = start;
        newLoopRegion.length = end;
        newLoopRegion.selector = event;
        newLoopRegion.timeline = timeLine;
        newLoopRegion.markerTrack = track;

        if (newLoopRegion.isValid == false)
        {
            alert("Failed to crete loop region");
            studio.project.deleteObject(newLoopRegion);
        }
        else
        {
            alert("Created loop region");
        }
    }
});

Hope this helps!

1 Like

That’s great, thank you very much!

1 Like