I noticed that a separate script that I'm developing wouldn't always update to the correct track. I made a mini script following similar logic (I'm not a programmer, so I may be off basis in my execution... if so, my apologies).
Here's the basic script:
Code:
function Msg(parameter)
reaper.ShowConsoleMsg(tostring(parameter).."\n")
end
function Main()
projState = reaper.GetProjectStateChangeCount(0)
-- if the project has changed then
if (projState ~= oldProjState) then
-- get active midi
editor = reaper.MIDIEditor_GetActive()
-- get take active in midi editor
itemTake = reaper.MIDIEditor_GetTake(editor)
-- get track of take in midi editor
track = reaper.GetMediaItemTake_Track(itemTake)
-- display results
Msg("---------------------------")
Msg("Editor: " .. tostring(editor))
Msg("Item Take: " .. tostring(itemTake))
Msg("Track: ".. tostring(track))
Msg("---------------------------")
end
oldProjState = projState
reaper.defer(Main)
end
--Create window
gfx.init("Test", 300, 300, dockState, 50, 50)
--Get initial project state
projState = reaper.GetProjectStateChangeCount(0)
oldProjState = -1
Main()
The intent of the code is to reference the track being edited in the MIDI editor whenever a change is made in the Reaper project. This action updates GUI options in my other script, but the overall logic is similar for track selection here.
I have the following MIDI editor settings:
Using one MIDI editor per project
Behavior for "open items in built-in MIDI editor" (Open all MIDI in the project)
Active MIDI item follows selection changes in arrange view (selected)
Media item selection is linked to visibility (selected)
Selection is linked to editability (selected)
Close editor when the active item is deleted in the arrange view (not selected)
Steps to test: For the test, I created two tracks in an otherwise blank project and added blank MIDI items of one measure to both tracks.
First, I tried toggling between media items in arrange view by clicking on alternate items, the itemTake that was returned did not always match the selection:
Code:
-- ARRANGE VIEW
-- Click on media item in track 1
---------------------------
Editor: userdata: 00000000011A07E2
Item Take: userdata: 00000000079229F0
Track: userdata: 00000000030A0080
---------------------------
-- Click on media item in track 2 (Item Take is different as expected)
---------------------------
Editor: userdata: 00000000011A07E2
Item Take: userdata: 00000000079219F0
Track: userdata: 0000000003140910
---------------------------
-- Click on media item in track 1 (Item Take has not changed)
---------------------------
Editor: userdata: 00000000011A07E2
Item Take: userdata: 00000000079219F0
Track: userdata: 0000000003140910
---------------------------
-- Click on media item in track 2
---------------------------
Editor: userdata: 00000000011A07E2
Item Take: userdata: 00000000079219F0
Track: userdata: 0000000003140910
---------------------------
Next, I tried toggling between tracks in the MIDI editor. This worked as expected.
Code:
-- MIDI EDITOR
-- Click on track 1 in MIDI editor track list
---------------------------
Editor: userdata: 00000000011A07E2
Item Take: userdata: 00000000079229F0
Track: userdata: 00000000030A0080
---------------------------
-- Click on track 2 in MIDI editor track list
---------------------------
Editor: userdata: 00000000011A07E2
Item Take: userdata: 00000000079219F0
Track: userdata: 0000000003140910
---------------------------
-- Click on track 1 in MIDI editor track list
---------------------------
Editor: userdata: 00000000011A07E2
Item Take: userdata: 00000000079229F0
Track: userdata: 00000000030A0080
---------------------------
-- Click on track 2 in MIDI editor track list
---------------------------
Editor: userdata: 00000000011A07E2
Item Take: userdata: 00000000079219F0
Track: userdata: 0000000003140910
---------------------------
Expected behavior: With the "Active MIDI item follows selection changes in arrange view" option selected, I would expect that toggling between MIDI items in both Arrange View and MIDI Editor would generate the same results from the script provided.