5 Replies Latest reply on Feb 24, 2009 8:38 PM by escouten

    Problem iterating through LR property table

    Level 1
      I'm having some difficulty and I'm not 100% sure whether it's a shortcoming in my understanding of Lua or of Lightroom's use of Lua.

      I'm writing an export service provider. I have all of my dialog controls' values bound and named with a common prefix for my provider, e.g.:

      value = bind 'myplugin_param1'
      value = bind 'myplugin_param2'
      ...

      In my processRenderedPhotos(), I need to write the values for my controls to a file. I want to iterate through the exportContext.propertyTable and if I see a key that begins with my prefix, I write the value otherwise I ignore it. Something like this:

      for k, v in pairs( exportContext.propertyTable ) do
      if (string.sub(k, 1, 9) == 'myplugin_') then
      io.write( k .. ': ' .. v .. '\n' )
      end
      end

      But this doesn't work. Digging deeper, I find that propertyTable has just two keys in it. One of them is a string "< contents >" with a value that is the table of property values I'd expect. So, changing the first line in the above code snippet to this makes it all work:

      for k, v in pairs( exportContext.propertyTable["< contents >"] ) do

      But if that's the case, I'm confused why I *can* access fields by name directly, i.e. this works correctly:

      foo = exportContext.propertyTable["myplugin_param1"]

      So, is this a Lua thing or a LR/Lua thing? Is LR playing tricks with this table's metatable to make this happen (maybe override the __index method?) ?

      I'd rather not access my properties directly by name since if I add/remove them, this is one more place in the code I need to maintain. With the code change I made above, everything works the way I prefer but it's undocumented so I'd like to know if there's a better way.

      Dave