Old 08-05-2020, 04:52 PM   #1
jbradbury
Human being with feelings
 
Join Date: Sep 2019
Posts: 25
Default Possible bug with table iteration and track creation

So I have a JSON in this format.

Code:
{
"37" : ["file1.wav", "file2.wav"],
"42" : ["file4.wav", "file3.wav"]
}
which I decode into a table via a JSON decoding library. The table becomes this format:

Code:
{
"37" = {"file1.wav}, "file2.wav"},
"42" = {"file4.wav", "file3.wav"}
}
I am iterating over this table structure with the idea that "37" becomes a track that "file1.wav" and "file2.wav" are appended to via InsertMedia.

With my script I get very strange behaviour where tracks are created outside of the call I've actually made. You can verify this with the fact that track names get created which are taking variables that I haven't even assigned to modifying the track name. This depends on whether you read the array of file names using an iterator (such as pairs) or iterating over the length with something like
Code:
for i=1, #tab do
My code is here.

Code:
local info = debug.getinfo(1,'S');
local script_path = info.source:match[[^@?(.*[\/])[^\/]-$]]
package.path = package.path .. ";" .. script_path .. "?.lua"
loadfile(script_path .. "ReaCoMa/lib/reacoma.lua")()

local json = require 'json'

local confirm, user_input = reaper.GetUserInputs('Provide JSON info', 1, 'JSON Path:, extrawidth=100', '')

if confirm then
    reaper.SetEditCurPos(0.0, false, false) -- go from the beginning my dudes


    -- Parse user input --
    local fields = reacoma.utils.commasplit(user_input)
    local json_path = fields[1]

    -- Do json parsing --
    local file_in = assert(io.open(json_path, "r"))
    local content = file_in:read("*all")
    local cluster_data = json.decode(content)
    -- count = 0
    local numtracks = reaper.GetNumTracks()-1
    for k, p in pairs(cluster_data) do
        reaper.SetEditCurPos(0.0, false, false)
        reaper.InsertTrackAtIndex(numtracks, true)
        numtracks = reaper.GetNumTracks()-1
        tr = reaper.GetTrack(0, numtracks)
        reaper.GetSetMediaTrackInfo_String(tr, "P_NAME", tostring(k), true)
        
        -- depending on which one you run the behaviour can be different...
        for _, y in pairs(p) do
            reaper.InsertMedia(y, 0)
        end
        -- for i=1, #p do 
        --     reaper.InsertMedia(p[i], 0) 
        -- end
    end
end
I can verify that my code is valid and when I test it in a lua REPL the parsing makes sense, I get a 'track', i.e one of the keys of the table and iterating over the values of that key give me file names but the ordering seems massively screwed in REAPER.

Is there an easier way to do what I want to do because i feel like right now I might start constructing .rpr files in Python
jbradbury is offline   Reply With Quote
Old 08-05-2020, 05:27 PM   #2
heda
Human being with feelings
 
heda's Avatar
 
Join Date: Jun 2012
Location: Spain
Posts: 6,026
Default

pairs doesn't iterate in order. that how lua works. you need to create an indexed table and use ipairs to get them all in order.
heda is offline   Reply With Quote
Old 08-06-2020, 05:58 AM   #3
jbradbury
Human being with feelings
 
Join Date: Sep 2019
Posts: 25
Default

Quote:
Originally Posted by heda View Post
pairs doesn't iterate in order. that how lua works. you need to create an indexed table and use ipairs to get them all in order.
The order isn't important. It's the fact that tracks are being created before the contents of a key are iterated over.
jbradbury is offline   Reply With Quote
Old 08-06-2020, 06:41 AM   #4
jbradbury
Human being with feelings
 
Join Date: Sep 2019
Posts: 25
Default

import json
confirm, title, num_inputs, _, json_path, _ = RPR_GetUserInputs(
"Provide JSON info",
1,
'JSON Path:, extrawidth=100',
'',
4096)
Code:
if confirm:
    data = {}
    with open(json_path, "r") as f:
        data = json.load(f)

    numtracks = RPR_GetNumTracks()
    # for k, v in data.items():
    for k in data:
        RPR_SetEditCurPos(0.0, False, False)
        RPR_InsertTrackAtIndex(numtracks, True)
        tr = RPR_GetTrack(0, numtracks)
        numtracks = RPR_GetNumTracks()
        RPR_GetSetMediaTrackInfo_String(tr, "P_NAME", str(k), True)
        for f in data[k]:
            RPR_InsertMedia(f, 0)
Also testing with this I get the same problem. If you remove the iteration
Code:
for f in data[k]
tracks are made sensibly and with the track name being "k" variable. If you iterate over the files names which are a list associated to the key k, tracks with names of the files are made. This makes no sense as I never pass the name of the file to RPR_GetSetMediaTrackInfo_String.
jbradbury is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 08:43 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2020, vBulletin Solutions Inc.