6 Replies Latest reply on Oct 7, 2013 1:08 PM by johnrellis

    passing LrPhotos in arrays

    victorvodka

      I know in the table-based world of Lua this is probably klutzy, but hear me out. In one part of my code I am creating an Array of Arrays containing information

      about some LrPhotos as well (for good measure) as the actual LrPhotos, and I'm putting it in dialogData.catalog, which is a global property table produced by LrBinding.makePropertyTable.  Here is the code that creates that Array of Arrays:

       

       

       

       

       

       

              --photos is an array of LrPhotos:

              for i=1, #photos do

                  local photo=photos[i]

                  local path= photo:getRawMetadata( 'path' )

                  local dateTimeDigitized= photo:getRawMetadata( 'dateTimeDigitized' )

                  local uuid= photo:getRawMetadata( 'uuid' )

       

                  dialogData.catalog[#dialogData.catalog+1]= {path,dateTimeDigitized, kwImageId, uuid, photo}

       

              end

       

       

       

      None of the items in the inner array are "keyed" or whatever that is called in Lua, and that doesn't seem to be a problem later when I retrieve the data. I get the path, the dateTimeDigitized, and kwImageId (which is later populated with data from a SQLite database).  But no matter what I do, when I try to use the photos (the fifth item in the inner array) later on, it comes back as nil.   There doesn't seem to be any way to use photo placed in an array this way. What am I doing wrong?  This is driving me insane.

        • 1. Re: passing LrPhotos in arrays
          victorvodka Level 1

          To clarify:  I actually can perform a tostring on the LrPhotos in the Array later on, producing a string representation of the LrPhotos, but I cannot do things such as photo:getRawMetadata( 'path' ) with them. perhaps i need to import a library in the places where i need to do this (just guessing).  How do I pass an LrPhoto in an array in a way that allows me to us LrPhoto methods on it?

          • 2. Re: passing LrPhotos in arrays
            johnrellis Most Valuable Participant

            Hard to say what's going on. In general, given the statement:

             

            dialogData.catalog[#dialogData.catalog+1]= {path,dateTimeDigitized, kwImageId, uuid, photo}

             

            you should be able to retrieve the photo with:

             

            dialogData.catalog [i][5]

             

            Are you sure "photo" is non-nil?

             

            I suggest setting a pause point right after that line:

             

            Debug.pause (dialogData.catalog, i, photos)

             

            so you can verify the contents of "dialogData.catalog".  Then set pause points where you're having problems accessing the contents.

             

            If you haven't already, I suggest that you take the time to install the debugging toolkit for Lightroom.  It will take 30 minutes or so to get up to speed, but it will pay off down the road. 

             

            By the way, in Lua terminology, a "table" consists of key/value pairs.  An "array" is just a table whose keys are numbered sequentially from 1 to n.

            • 3. Re: passing LrPhotos in arrays
              victorvodka Level 1

              i can confirm that dialogData.catalog[i][5] is an LrPhoto because when i do something like myLogger:trace(tostring(dialogData.catalog[i][5]))

               

               

              i get

               

              LrPhoto( id "26" )

               

              but when i try to do something like

               

               

              if myPhoto~=nil then

               

                              local myPath =myPhoto:getRawMetadata( 'path' )

               

                              myLogger:trace( "mypath"  ..myPath  )

               

              end

              the code never gets beyond the

              local myPath =myPhoto:getRawMetadata( 'path' )  

              line. it's as if the getRawMetadata method is impossible and is throwing an error, though not one i ever see.

               

               

              thanks for the tip about the debugging tools. debugging in the SDK is a royal bιτch.

               

              • 4. Re: passing LrPhotos in arrays
                victorvodka Level 1

                i think i figured it out: getRawMetadata needs to be executed

                using LrTasks. ARGH!  none of the data is there for me to use when i need to use it!  

                • 5. Re: passing LrPhotos in arrays
                  areohbee Level 5

                  LOTs of things need to run as async task. The solution is to wrap most of your code in an Lr(Async)Task as early as possible (if not already wrapped I mean), then you never have to worry about it. Use recursion guarding if such tasks must not execute simultaneously.

                   

                  In other words, menu handlers, button handlers, ... should be tasks.

                  • 6. Re: passing LrPhotos in arrays
                    johnrellis Most Valuable Participant

                    it's as if the getRawMetadata method is impossible and is throwing an error, though not one i ever see.

                    Yeah, by default, no plugin function called from the SDK has error-trapping enabled -- the errors are silently discarded.   Simply wrapping each such function with Debug.showErrors() will ensure that you see the errors.  And combine that with "require 'strict.lua'" in each of your modules, and you've just saved yourself hours of pain.