Skip to content

scripts

ScriptItem dataclass

Resolved After Effects script item.

Attributes:

Name Type Description
script_id str

Stable identifier for the configured script.

name str

Display name shown to the user.

path str

Resolved absolute path or unresolved raw path.

auto bool

Whether the script should run automatically on launch.

exists bool

Whether the script can be executed.

error str | None

Validation error when the script is not executable.

Source code in client/ayon_aftereffects/api/scripts.py
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
@dataclass(frozen=True)
class ScriptItem:
    """Resolved After Effects script item.

    Attributes:
        script_id: Stable identifier for the configured script.
        name: Display name shown to the user.
        path: Resolved absolute path or unresolved raw path.
        auto: Whether the script should run automatically on launch.
        exists: Whether the script can be executed.
        error: Validation error when the script is not executable.
    """

    script_id: str
    name: str
    path: str
    auto: bool
    exists: bool
    error: str | None = None

ScriptRunResult dataclass

Execution result for a configured script.

Attributes:

Name Type Description
script_id str

Stable identifier for the configured script.

success bool

Whether the script execution succeeded.

message str

User-facing execution status.

Source code in client/ayon_aftereffects/api/scripts.py
41
42
43
44
45
46
47
48
49
50
51
52
53
@dataclass(frozen=True)
class ScriptRunResult:
    """Execution result for a configured script.

    Attributes:
        script_id: Stable identifier for the configured script.
        success: Whether the script execution succeeded.
        message: User-facing execution status.
    """

    script_id: str
    success: bool
    message: str

ScriptService

Resolve and execute configured After Effects scripts.

Source code in client/ayon_aftereffects/api/scripts.py
 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
class ScriptService:
    """Resolve and execute configured After Effects scripts."""

    def list_items(self, auto: bool | None = None) -> list[ScriptItem]:
        """Return resolved script items from project settings.

        Args:
            auto: Optional auto/manual filter. When `None`, all items are
                returned.

        Returns:
            Resolved script items in settings order.
        """
        project_settings = get_current_project_settings()
        scripts_settings = project_settings["aftereffects"]["scripts"]
        configs = scripts_settings["configs"]

        if not configs:
            log.debug("No scripts found in project settings.")
            return []

        output: list[ScriptItem] = []
        for index, config in enumerate(configs):
            item_auto: bool = config["auto"]
            if auto is not None and item_auto != auto:
                continue

            raw_path: str = config["path"]
            name: str = config["name"]

            error: str | None = None
            exists = False
            if not raw_path:
                resolved_path = raw_path
                error = "Script path is empty."
            else:
                template_result, resolved_path = self.resolve_path(raw_path)
                if not template_result.solved:
                    missing = ", ".join(sorted(template_result.missing_keys))
                    error = f"Unresolved template variables: {missing}"
                elif not self._has_supported_extension(resolved_path):
                    error = "Only .js and .jsx files are supported."
                elif not os.path.isfile(resolved_path):
                    error = "Script file does not exist."
                else:
                    exists = True

            output.append(
                ScriptItem(
                    script_id=f"script_{index}",
                    name=name,
                    path=resolved_path,
                    auto=item_auto,
                    exists=exists,
                    error=error,
                )
            )
        return output

    def list_manual_items(self) -> list[ScriptItem]:
        """Return scripts configured for manual execution.

        Returns:
            Manual script items.
        """
        return self.list_items(auto=False)

    def resolve_scripts(self, auto: bool = True) -> list[str]:
        """Resolve executable script paths.

        Args:
            auto: Auto/manual filter.

        Returns:
            Ordered list of executable script paths.
        """
        return [
            item.path for item in self.list_items(auto=auto) if item.exists
        ]

    def resolve_path(self, path: str) -> tuple:
        """Resolve a templated script path against current AYON context.

        Args:
            path: Raw configured path.

        Returns:
            Tuple of (TemplateResult, resolved path string). When the template
            is not fully solved, the second element is the original raw path.
        """
        template_data = get_current_context_template_data()
        template_data.update(os.environ)

        project_name = template_data["project"]["name"]
        anatomy = Anatomy(project_name)
        template_data["root"] = anatomy.roots

        result = StringTemplate.format_template(path, template_data)
        if result.solved:
            resolved = anatomy.path_remapper(result.normalized())
            return result, resolved

        return result, path

    def run_scripts(self, auto: bool = True) -> None:
        """Run all valid scripts for the requested mode.

        Args:
            auto: Auto/manual filter.
        """
        for item in self.list_items(auto=auto):
            result = self._run_item(item)
            if not result.success:
                log.warning(result.message)
            else:
                log.info(f"Script {item.name} ran successfully.")

    def run_item(self, item: ScriptItem) -> ScriptRunResult:
        """Run a resolved script item directly.
        Args:
            item: Already-resolved script item (e.g. from list_manual_items).
        Returns:
            Script execution result.
        """
        return self._run_item(item)

    def _find_item(
        self,
        script_id: str,
        auto: bool | None = None,
    ) -> ScriptItem | None:
        """Find a resolved script item by identifier.

        Args:
            script_id: Stable script identifier.
            auto: Optional auto/manual filter.

        Returns:
            Matching script item, if found.
        """
        for item in self.list_items(auto=auto):
            if item.script_id == script_id:
                return item
        return None

    def _run_item(self, item: ScriptItem) -> ScriptRunResult:
        """Run a resolved script item.

        Args:
            item: Script item to execute.

        Returns:
            Script execution result.
        """
        if not item.exists:
            return ScriptRunResult(
                script_id=item.script_id,
                success=False,
                message=item.error or "Script is not executable.",
            )

        try:
            stub = get_stub()
        except ConnectionNotEstablishedYet:
            return ScriptRunResult(
                script_id=item.script_id,
                success=False,
                message="After Effects client is not connected.",
            )

        try:
            log.debug("Running script: %s", item.path)
            stub.run_jsx_file(item.path)
        except Exception:
            log.warning("Failed to run script: %s", item.path, exc_info=True)
            return ScriptRunResult(
                script_id=item.script_id,
                success=False,
                message=f"Failed to run script: {item.name}",
            )

        return ScriptRunResult(
            script_id=item.script_id,
            success=True,
            message=f"Executed script: {item.name}",
        )

    def _has_supported_extension(self, path: str) -> bool:
        """Return whether the path has a supported extension.

        Args:
            path: Resolved script path.

        Returns:
            Whether the extension is supported.
        """
        return path.lower().endswith(_SUPPORTED_EXTENSIONS)

list_items(auto=None)

Return resolved script items from project settings.

Parameters:

Name Type Description Default
auto bool | None

Optional auto/manual filter. When None, all items are returned.

None

Returns:

Type Description
list[ScriptItem]

Resolved script items in settings order.

Source code in client/ayon_aftereffects/api/scripts.py
 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
def list_items(self, auto: bool | None = None) -> list[ScriptItem]:
    """Return resolved script items from project settings.

    Args:
        auto: Optional auto/manual filter. When `None`, all items are
            returned.

    Returns:
        Resolved script items in settings order.
    """
    project_settings = get_current_project_settings()
    scripts_settings = project_settings["aftereffects"]["scripts"]
    configs = scripts_settings["configs"]

    if not configs:
        log.debug("No scripts found in project settings.")
        return []

    output: list[ScriptItem] = []
    for index, config in enumerate(configs):
        item_auto: bool = config["auto"]
        if auto is not None and item_auto != auto:
            continue

        raw_path: str = config["path"]
        name: str = config["name"]

        error: str | None = None
        exists = False
        if not raw_path:
            resolved_path = raw_path
            error = "Script path is empty."
        else:
            template_result, resolved_path = self.resolve_path(raw_path)
            if not template_result.solved:
                missing = ", ".join(sorted(template_result.missing_keys))
                error = f"Unresolved template variables: {missing}"
            elif not self._has_supported_extension(resolved_path):
                error = "Only .js and .jsx files are supported."
            elif not os.path.isfile(resolved_path):
                error = "Script file does not exist."
            else:
                exists = True

        output.append(
            ScriptItem(
                script_id=f"script_{index}",
                name=name,
                path=resolved_path,
                auto=item_auto,
                exists=exists,
                error=error,
            )
        )
    return output

list_manual_items()

Return scripts configured for manual execution.

Returns:

Type Description
list[ScriptItem]

Manual script items.

Source code in client/ayon_aftereffects/api/scripts.py
115
116
117
118
119
120
121
def list_manual_items(self) -> list[ScriptItem]:
    """Return scripts configured for manual execution.

    Returns:
        Manual script items.
    """
    return self.list_items(auto=False)

resolve_path(path)

Resolve a templated script path against current AYON context.

Parameters:

Name Type Description Default
path str

Raw configured path.

required

Returns:

Type Description
tuple

Tuple of (TemplateResult, resolved path string). When the template

tuple

is not fully solved, the second element is the original raw path.

Source code in client/ayon_aftereffects/api/scripts.py
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
def resolve_path(self, path: str) -> tuple:
    """Resolve a templated script path against current AYON context.

    Args:
        path: Raw configured path.

    Returns:
        Tuple of (TemplateResult, resolved path string). When the template
        is not fully solved, the second element is the original raw path.
    """
    template_data = get_current_context_template_data()
    template_data.update(os.environ)

    project_name = template_data["project"]["name"]
    anatomy = Anatomy(project_name)
    template_data["root"] = anatomy.roots

    result = StringTemplate.format_template(path, template_data)
    if result.solved:
        resolved = anatomy.path_remapper(result.normalized())
        return result, resolved

    return result, path

resolve_scripts(auto=True)

Resolve executable script paths.

Parameters:

Name Type Description Default
auto bool

Auto/manual filter.

True

Returns:

Type Description
list[str]

Ordered list of executable script paths.

Source code in client/ayon_aftereffects/api/scripts.py
123
124
125
126
127
128
129
130
131
132
133
134
def resolve_scripts(self, auto: bool = True) -> list[str]:
    """Resolve executable script paths.

    Args:
        auto: Auto/manual filter.

    Returns:
        Ordered list of executable script paths.
    """
    return [
        item.path for item in self.list_items(auto=auto) if item.exists
    ]

run_item(item)

Run a resolved script item directly. Args: item: Already-resolved script item (e.g. from list_manual_items). Returns: Script execution result.

Source code in client/ayon_aftereffects/api/scripts.py
173
174
175
176
177
178
179
180
def run_item(self, item: ScriptItem) -> ScriptRunResult:
    """Run a resolved script item directly.
    Args:
        item: Already-resolved script item (e.g. from list_manual_items).
    Returns:
        Script execution result.
    """
    return self._run_item(item)

run_scripts(auto=True)

Run all valid scripts for the requested mode.

Parameters:

Name Type Description Default
auto bool

Auto/manual filter.

True
Source code in client/ayon_aftereffects/api/scripts.py
160
161
162
163
164
165
166
167
168
169
170
171
def run_scripts(self, auto: bool = True) -> None:
    """Run all valid scripts for the requested mode.

    Args:
        auto: Auto/manual filter.
    """
    for item in self.list_items(auto=auto):
        result = self._run_item(item)
        if not result.success:
            log.warning(result.message)
        else:
            log.info(f"Script {item.name} ran successfully.")

get_script_service()

Return the singleton script service.

Returns:

Type Description
ScriptService

Shared script service instance.

Source code in client/ayon_aftereffects/api/scripts.py
258
259
260
261
262
263
264
def get_script_service() -> ScriptService:
    """Return the singleton script service.

    Returns:
        Shared script service instance.
    """
    return _SCRIPT_SERVICE

resolve_path(path)

Resolve a templated script path against current AYON context.

Parameters:

Name Type Description Default
path str

Raw configured path.

required

Returns:

Type Description
str

Resolved path, or the original path if resolution failed.

Source code in client/ayon_aftereffects/api/scripts.py
279
280
281
282
283
284
285
286
287
288
289
def resolve_path(path: str) -> str:
    """Resolve a templated script path against current AYON context.

    Args:
        path: Raw configured path.

    Returns:
        Resolved path, or the original path if resolution failed.
    """
    _result, resolved = get_script_service().resolve_path(path)
    return resolved

resolve_scripts(auto=True)

Resolve active script paths from project settings.

Parameters:

Name Type Description Default
auto bool

Auto/manual filter.

True

Returns:

Type Description
list[str]

Ordered list of executable script paths.

Source code in client/ayon_aftereffects/api/scripts.py
267
268
269
270
271
272
273
274
275
276
def resolve_scripts(auto: bool = True) -> list[str]:
    """Resolve active script paths from project settings.

    Args:
        auto: Auto/manual filter.

    Returns:
        Ordered list of executable script paths.
    """
    return get_script_service().resolve_scripts(auto=auto)

run_scripts(auto=True)

Run configured After Effects scripts.

Parameters:

Name Type Description Default
auto bool

Auto/manual filter.

True
Source code in client/ayon_aftereffects/api/scripts.py
292
293
294
295
296
297
298
def run_scripts(auto: bool = True) -> None:
    """Run configured After Effects scripts.

    Args:
        auto: Auto/manual filter.
    """
    get_script_service().run_scripts(auto=auto)