How to include User Data in JSON export?

Hi, how can I export a frame index and layer index along with each associated User Data string (right click on cell → cel properties → User Data field) in JSON format? I’ve got a ton of frames that I’d like to do some manipulation with in a separate program based on the user data. Is there a way to do this in one CLI command or perhaps a Lua script that can do this?

I’ve done some digging but this is the only mention of user data I’ve been able to find and it doesn’t address my use case:

Ok, nevermind. I think I basically got it:

local sprite = app.activeSprite

-- Check constraints
if sprite == nil then
  app.alert("No Sprite...")
  return
end

local function tablelength(T)
  local count = 0
  for _ in ipairs(T) do count = count + 1 end
  return count
end

local function exportData()
	io.write("{\n")
	
	local layerCount = tablelength(sprite.layers)
	local frameCount = tablelength(sprite.frames)	
	local userData = ""
	
	for col = 1,frameCount,1 do
		io.write("    {\n")
		io.write("        \"frames\": [")
		userData = ""
		for row = layerCount,1,-1 do				
			local cel = sprite.layers[row]:cel(col)
			if cel then										
				io.write(col .. ",")
				if cel.data ~= "" then
					userData = userData .. cel.data .. ","
				else
					userData = userData .. "-1,"
				end
			else
				userData = userData .. "-1,"
				io.write("-1,")
			end
		end
		io.write("],\n")
		io.write("        \"userData\": [" .. userData .. "]\n")
		io.write("    }\n")
	end
	io.write("}\n")
end

local dlg = Dialog()
dlg:file{ id="exportFile",
          label="File",
          title="Export Index",
          open=false,
          save=true,
          filetypes={"json"}}

dlg:button{ id="ok", text="OK" }
dlg:button{ id="cancel", text="Cancel" }
dlg:show()
local data = dlg.data
if data.ok then
	local f = io.open(data.exportFile, "w")
	io.output(f)
	exportData()
	io.close(f)
end
1 Like