Adobe 65007312 Programming Guide - Page 20

Defining function contexts and tasks, Including scripts with require(), SomeFile.lua, Usage of require

Page 20 highlights

CHAPTER 1: Using the Lightroom SDK The Lightroom SDK scripting environment 20 Defining function contexts and tasks Your plug-in can use a function context to create and manage a task, which is a kind of lightweight process that runs cooperatively on Lightroom's main (user interface) thread. If your service defines a lengthy export operation that would block the main Lightroom process, you should run it as a background task, using functions such as LrFunctionContext.postAsyncTaskWithContext(). Some API functions, such as those in the LrHttp namespace, are only available when called from within a background task. The LrFunctionContext object helps you clean up resources following the execution of a function. You can register any number of cleanup handlers to respond to the success or failure of a function. You must create property tables within a function context, so that Lightroom can remove notifications when the table is no longer needed. You do not create instances of LrFunctionContext directly. They are created by the calling functions, and exist only for the lifetime of the function call or task. Access the calling functions, such as LrFunctionContext.postAsyncTaskWithContext(), directly from the imported namespace. A functionContext object is passed as the first parameter of the call, followed by any other parameters you provide. Use the passed object to provide the cleanup handlers for the called function execution. In general, you are responsible for creating tasks when needed. There are some exceptions, however. Many of the plug-in callback functions in the export and publish APIs are called from within tasks that Lightroom starts. These are marked as such in the API reference. For details of the LrFunctionContext and LrTasks functions, see the Lightroom SDK API Reference. Including scripts with require() Lightroom defines a require() function that works in a similar, but more narrowly-defined, fashion from the version that exists in Lua. The require() function takes a single parameter, which is the name of another Lua file in the same plug-in. On the first call, this file is loaded and executed in the context of its plug-in; the return value is saved. If the require() function is called again in the same plug-in, its saved value is used (unless the entire plug-in has been garbage collected, in which case the required file is loaded and executed again). A script to be executed this way typically has the effect of defining a table containing a suite of functions. For example: SomeFile.lua SomeFile = {} -- Typically a file that is required will define a global table whose name -- matches the file name. -- Note that this global is defined in a special function environment for your -- plug-in and does not affect Lightroom as a whole. -- You can give this table any name that does not conflict with built-in names -- and keywords from Lua and Lightroom. In general, avoid names that start with -- Lr to avoid conflicts with future versions of Lightroom. function SomeFile.doSomething( arg ) return tostring( arg ) end Usage of require() require 'SomeFile.lua' -- Causes SomeFile.lua to be executed and the value of SomeFile defined above -- becomes available in the scope of this file.

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204

C
HAPTER
1: Using the Lightroom SDK
The Lightroom SDK scripting environment
20
Defining function contexts and tasks
Your plug-in can use a
function context
to create and manage a
task
, which is a kind of lightweight process
that runs cooperatively on Lightroom's main (user interface) thread. If your service defines a lengthy
export operation that would block the main Lightroom process, you should run it as a background task,
using functions such as
LrFunctionContext.postAsyncTaskWithContext()
. Some API functions, such
as those in the
LrHttp
namespace, are only available when called from within a background task.
The
LrFunctionContext
object helps you clean up resources following the execution of a function. You
can register any number of cleanup handlers to respond to the success or failure of a function. You must
create property tables within a function context, so that Lightroom can remove notifications when the
table is no longer needed.
You do not create instances of
LrFunctionContext
directly. They are created by the calling functions, and
exist only for the lifetime of the function call or task. Access the calling functions, such as
LrFunctionContext.postAsyncTaskWithContext()
, directly from the imported namespace. A
functionContext
object is passed as the first parameter of the call, followed by any other parameters you
provide. Use the passed object to provide the cleanup handlers for the called function execution.
In general, you are responsible for creating tasks when needed. There are some exceptions, however. Many
of the plug-in callback functions in the export and publish APIs are called from within tasks that Lightroom
starts. These are marked as such in the API reference.
For details of the
LrFunctionContext
and
LrTasks
functions, see the
Lightroom SDK API Reference
.
Including scripts with require()
Lightroom defines a
require()
function that works in a similar, but more narrowly-defined, fashion from
the version that exists in Lua. The
require()
function takes a single parameter, which is the name of
another Lua file in the same plug-in. On the first call, this file is loaded and executed in the context of its
plug-in; the return value is saved. If the
require()
function is called again in the same plug-in, its saved
value is used (unless the entire plug-in has been garbage collected, in which case the required file is
loaded and executed again).
A script to be executed this way typically has the effect of defining a table containing a suite of functions.
For example:
SomeFile.lua
SomeFile = {}
-- Typically a file that is required will define a global table whose name
-- matches the file name.
-- Note that this global is defined in a special function environment for your
-- plug-in and does not affect Lightroom as a whole.
-- You can give this table any name that does not conflict with built-in names
-- and keywords from Lua and Lightroom. In general, avoid names that start with
-- Lr to avoid conflicts with future versions of Lightroom.
function SomeFile.doSomething( arg )
return tostring( arg )
end
Usage of require()
require 'SomeFile.lua'
-- Causes SomeFile.lua to be executed and the value of SomeFile defined above
-- becomes available in the scope of this file.