4 Replies Latest reply on Jan 28, 2011 5:08 AM by DawMatt

    Getting require() to work

    clvrmnky Level 4

      I have a Util script that I want to have available to other plugin scripts in a plugin via require(). All scripts are in the same directory in the plugin folder.

       

      PluginUtils.lua:

       

      PluginUtils = {}


      function PluginUtils.fnord(arg)

         -- do some stuff, return a fnorded string

      end

       

      OtherFile.lua:

       

      require 'PluginUtils'

      ...

       

      When I make a gesture that runs OtherFile, the plugin fails with an error "Could not load toolkit script: PluginUtils"

       

      It never gets any further than that, and it truly appears to not find the required script (which runs fine when passed to the lua interpreter directly.)

       

      This seems to trivial. What am I missing? The SDK docs have an example that shows passing require the full name of the script, but that doesn't work, either, thought the error message reflects that change.

        • 1. Re: Getting require() to work
          jarnoh Level 1

          Yes, it should work.

           

          One word of caution:  Lightroom seems to remember which script files are in a directory and which are not.  If you are developing a plugin and adding a new required script, it is giving that same message until you restart Lightroom, reloading the plugin does not help.

           

          Jarno

          • 2. Re: Getting require() to work
            clvrmnky Level 4

            jarnoh wrote:

             

            Yes, it should work.

             

            One word of caution:  Lightroom seems to remember which script files are in a directory and which are not.  If you are developing a plugin and adding a new required script, it is giving that same message until you restart Lightroom, reloading the plugin does not help.

            Ah, that explains it, then. I assumed reloading the script would just work, but if Lr only catalogues the plugin contents once...  I'll give that a try once I'm in front of my plugin again.

             

            Is this in the docs and I just missed it?

             

            [Later]

             

            It is not documented (AFAICT) but the solution is to restart Lr with all the scripts present you wish to refer to in a plugin.  Good to know.

            • 3. Re: Getting require() to work
              areohbee Level 5

              Note: 'require' may work differently on Mac than Windows. I use a home-brewed loader instead of 'require' in my plugins, but it works differently on Mac.

               

              Windows sometimes pulls in new modules, whereas Mac does not, or something like that. Not sure exactly, I just know on Mac one sometimes has to restart Lightroom to get modules to reload, whereas a regular "Reload Plugin" works on Windows, when the plugin is of the self-modifying variety (like ExifMeta which rewrites the "Metadata.lua" file when it finds new exif metadata (and the Info.lua file upon upgrade). So it may be due to differences in the underlying lua loader: "dofile". I never figured out exactly, just a "heads up"...

               

              John Ellis may have a better understanding of it...

               

              Rob

              • 4. Re: Getting require() to work
                DawMatt Level 3

                Hi,

                 

                As Rob has mentioned Mac developers/users need to restart Lightroom when new files are added to a plugin.  I've never encountered this issue on Windows so it had me scratching my head for a while the first time I encountered it.

                 

                Matt