Skip to content

addon

WorkflowAddon

Bases: AYONAddon, IPluginPaths, ITrayAction

Source code in client/ayon_workflow/addon.py
 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
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
class WorkflowAddon(AYONAddon, IPluginPaths, ITrayAction):

    name = "workflow"
    version = __version__
    label = "Workflow editor"
    _editor_process = None
    _editor_url = None

    def get_plugin_paths(self) -> Dict:
        return {}

    def get_publish_plugin_paths(self, host_name: str) -> list[str]:
        return [
            os.path.join(
                WORKFLOW_ADDON_ROOT,
                "plugins",
                "publish"
            )
        ]

    def on_action_trigger(self) -> None:
        """ Open the editor from tray.
        """
        import webbrowser
        if (
            self._editor_url
            and self._editor_process
            and self._editor_process.poll() is None
        ):
            # Backend is already running,
            # just re-open the browser.
            webbrowser.open(self._editor_url)
            return
        self._open_editor(detached=True)

    def tray_init(self):
        return

    def _cli_execute_in_memory(
        self,
        workflow_path: str,
        inputs_path: Optional[str] = None,
        project: Optional[str] = None,
    ):
        from ayon_workflow.workflow_execution import (
            execute_in_memory,
        )
        execute_in_memory(
            workflow_path,
            inputs_data=inputs_path,
            project=project,
        )

    def _cli_execute_from_backend(
        self,
        graph_path: str,
        backend_dir: str,
        slice_flow_id: str,
        full_flow_id: str,
        project: Optional[str] = None,
    ):
        from ayon_workflow.workflow_execution import (
            execute_from_backend,
        )
        execute_from_backend(
            graph_path,
            backend_dir,
            slice_flow_id,
            full_flow_id=full_flow_id,
            project=project,
        )

    def _cli_submit_workflow_to_farm(
        self,
        workflow_path: str,
        backend_dir: str,
        inputs_path: Optional[str] = None,
        dispatch_graph_name: Optional[str] = None,
        project: Optional[str] = None,
    ):
        from ayon_workflow.workflow_execution import (
            submit_workflow_to_farm,
        )
        submit_workflow_to_farm(
            workflow_path,
            inputs_data=inputs_path,
            backend_dir=backend_dir,
            dispatch_graph_name=dispatch_graph_name,
            project=project,
            log=self.log,
        )

    def _cli_main(self) -> None:
        pass

    def _open_editor(
        self,
        host: Optional[str] = None,
        port: Optional[int] = None,
        detached: bool = False,
    ):
        """ Open the web editor.
        """
        from ayon_workflow.web_editor import (
            DEFAULT_HOST,
            DEFAULT_PORT,
        )
        resolved_port = port or DEFAULT_PORT
        host = host or DEFAULT_HOST

        # If detached, wrap into a subprocess so it
        # does not block the main process.
        if detached:
            import subprocess

            self._editor_url = f"http://{host}:{resolved_port}"
            args = [
                "addon", "workflow", "editor",
                "--host", host,
                "--port", str(resolved_port),
            ]
            if os.getenv("AYON_USE_STAGING") == "1":
                args.insert(0, "--use-staging")
            elif os.getenv("AYON_USE_DEV") == "1":
                args.insert(0, "--use-dev")

            from ayon_core.lib import get_ayon_launcher_args
            args = get_ayon_launcher_args(*args)
            self._editor_process = subprocess.Popen(args)

        else:
            from ayon_workflow.web_editor.__main__ import launch
            launch(host=host, port=int(resolved_port), open_browser=True)

    def cli(self, click_group):
        cli_main = click_wrap.group(
            self._cli_main,
            name=self.name,
            help="Workflow commands",
        )

        cli_main.command(
            self._cli_execute_in_memory,
            name="execute",
            help="Execute a workflow in memory."
        ).option(
            "--workflow-path",
            help="Path to a workflow serialized as JSON file.",
            type=str,
            required=True,
        ).option(
            "--inputs-path",
            help="Full path dictionary of inputs file serialized as JSON.",
            type=str,
            required=False,
        ).option(
            "--project",
            help=(
                "Optional project name associated to the workflow. Resolve "
                "execution from rootless workflow path."
            ),
            type=str,
            required=False,
        )

        cli_main.command(
            self._cli_execute_from_backend,
            name="execute-slice-flow",
            help=(
                "Execute a slice flow (execution graph) "
                "from a full flow and a backend."
            ),
        ).option(
            "-g",
            "--graph-path",
            help="Path to an execution Graph serialized as JSON file.",
            type=str,
            required=True,
        ).option(
            "--backend-dir",
            help="Full path to a directory to pickup backend from.",
            type=str,
            required=True,
        ).option(
            "--slice-flow-id",
            help=(
                "The UUID of the registered slice flow in the backend."
            ),
            type=str,
            required=True,
        ).option(
            "--full-flow-id",
            help=(
                "The UUID of the registered full flow in the backend. "
            ),
            type=str,
            required=True,
        ).option(
            "--project",
            help=(
                "Optional project name associated to the workflow. Resolve "
                "execution from rootless backend directory path."
            ),
            type=str,
            required=False,
        )

        cli_main.command(
            self._cli_submit_workflow_to_farm,
            name="submit",
            help="Submit a workflow to a farm."
        ).option(
            "-w",
            "--workflow-path",
            help="Path to a workflow serialized as JSON file.",
            type=str,
            required=True,
        ).option(
            "--backend-dir",
            help="Full path to a directory to init the backend.",
            type=str,
            required=True,
        ).option(
            "--dispatch-graph-name",
            help=(
                "Name of the dispatch graph to be used for workflow split. "
                "If not provided, default to first one from the workflow."
            ),
            type=str,
            required=False,
        ).option(
            "--inputs-path",
            help="Full path dictionary of inputs file serialized as JSON.",
            type=str,
            required=False,
        ).option(
            "--project",
            help=(
                "Optional project name associated to the workflow. "
                "This enables submit from rootless backend if possible."
            ),
            type=str,
            required=False,
        )

        cli_main.command(
            self._open_editor,
            name="editor",
            help="Start the workflow web editor backend.",
        ).option(
            "--host",
            help="Host to bind the backend server to.",
            type=str,
            required=False,
        ).option(
            "--port",
            help="Port to bind the backend server to (default: auto).",
            type=int,
            required=False,
        )

        click_group.add_command(cli_main.to_click_obj())

on_action_trigger()

Open the editor from tray.

Source code in client/ayon_workflow/addon.py
47
48
49
50
51
52
53
54
55
56
57
58
59
60
def on_action_trigger(self) -> None:
    """ Open the editor from tray.
    """
    import webbrowser
    if (
        self._editor_url
        and self._editor_process
        and self._editor_process.poll() is None
    ):
        # Backend is already running,
        # just re-open the browser.
        webbrowser.open(self._editor_url)
        return
    self._open_editor(detached=True)