Skip to content

launch_logic

Install host.

To be launched from launch_script.py.

adjust_consts_comfyui_plugin(plugin_path)

Adjust settings for comfyui plugin.

Source code in client/ayon_comfyui/api/launch_logic.py
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
def adjust_consts_comfyui_plugin(plugin_path: Path) -> None:
    """Adjust settings for comfyui plugin."""
    settings, _ = ComfyLocalSettings.pull_committed_settings()
    python = f"AYON_BACKEND_PORT = {settings.port_backend}\n"
    js = f'export const AYON_ORIGIN_ADRESS = "{settings.address_frontend}";'
    py_file = plugin_path / "ayon_menu" / "consts.py"
    js_file = plugin_path / "ayon_menu" / "js" / "lib" / "consts.js"

    # update python consts plugin with port to use.
    # IPC using the proc Popen is not very wise,
    # since we can't reliably get a hold of the entire process tree

    # Since communicating the address to JS isn't possible before establishing
    # a connection with IFrame RPC,
    # we still have to send over the actual address to the plugin.

    Path(py_file).write_text(python, encoding="utf-8")
    Path(js_file).write_text(js, encoding="utf-8")

launch_local(app, settings, profile)

Launch ComfyUI as a subprocess.

Source code in client/ayon_comfyui/api/launch_logic.py
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
def launch_local(
    app: QApplication,
    settings: ComfyLocalSettings,
    profile: ComfyLocalSettings.ComfyLocalProfile,
) -> None:
    """Launch ComfyUI as a subprocess."""
    process = None

    try:
        # Launch comfyUI
        process = _subproc_launch_ComfyUI()
    except BaseException:  # noqa: BLE001
        log.debug("Problems launching ComfyUI:", exc_info=True)

    log.info("Creating QRPCmanager")

    try:
        rpcman = QRPCManager(
            parent=app,
            client_hostname="localhost",
            client_port=settings.port_backend,
            server_port=settings.port_webui,
            static_port=settings.port_static_frontend,
            comfy_url=profile.comfy_local_url,
            use_https=False,
        )

        rpcman.attach_comfyui_process(process=process)

        defer_site_launch_when_available(
            profile.comfy_local_url, settings.address_frontend
        )

        log.info("Created rpc manager")
        rpcman.start_server()
        log.info("called start_server")

        workfile_path = os.getenv("AYON_LAST_WORKFILE")
        if workfile_path and env_value_to_bool("AVALON_OPEN_LAST_WORKFILE"):
            origin = settings.address_frontend
            log.info(f"Scheduling launch workfile load: {workfile_path}")

            def _load_workfile_when_ready() -> None:
                while not get_client_from_origin(origin):
                    time.sleep(0.5)

                safe_load = safe_partial(
                    rpcman.stub.load_workfile, workfile_path
                )

                retries = 30
                while safe_load().is_err and retries > 0:
                    retries -= 1
                    time.sleep(0.5)

            Thread(target=_load_workfile_when_ready, daemon=True).start()
    except BaseException:  # noqa: BLE001
        log.debug("Problems starting server:", exc_info=True)

launch_remote(app, settings, profile)

Launch browser.

Source code in client/ayon_comfyui/api/launch_logic.py
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
def launch_remote(
    app: QApplication,
    settings: ComfyRemoteSettings,  # noqa: ARG001
    profile: ComfyRemoteSettings.ComfyRemoteProfile,
) -> None:
    """Launch browser."""
    log.info("Creating QRPCmanager")

    try:
        rpcman = QRPCManager(
            parent=app,
            client_hostname=profile.netloc_backend,
            client_port=profile.port_backend,
            server_port=profile.port_webui,
            static_port=profile.port_static_frontend,
            comfy_url=profile.comfy_url,
            use_https=profile.is_https,
        )

        if profile.open_browser:
            defer_site_launch_when_available(
                profile.comfy_url, profile.address_frontend
            )

        log.info("Created rpc manager")
        rpcman.start_server()
        log.info("Called start_server")
    except BaseException:  # noqa: BLE001
        log.debug("Problems starting server", exc_info=True)

main(*args)

Main designed to accomodate both local and remote launch profiles.

Source code in client/ayon_comfyui/api/launch_logic.py
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
def main(*args):
    """Main designed to accomodate both local and remote launch profiles."""
    sys.excepthook = safe_excepthook

    from ayon_comfyui.api import ComfyUIHost

    host = ComfyUIHost()
    install_host(host)

    log.info("Installed host")

    app = get_ayon_qt_app()
    app.setQuitOnLastWindowClosed(False)

    log.info("got QT app")

    try:
        project_name = get_current_project_name() or None

        profile_selector = ProfileDialog()
        # initialize local/remote settings
        profile_selector.populate_list(project_name)

        profile_selector.exec()
    except BaseException:  # noqa: BLE001
        log.debug("Error during profile dialog", exc_info=True)

    if (res := profile_selector.dialog_result) == ProfileTypeEnum.UNDECIDED:
        show_message_dialog(
            title="No profile selected!",
            message="You have not selected a profile.\nClosing...",
            level="warning",
        )
        sys.exit(0)
    elif res == ProfileTypeEnum.LOCAL:
        settings, profile = (
            profile_selector.settings_local.pull_committed_settings()
        )
        launch_local(app, settings, profile)
    elif res == ProfileTypeEnum.REMOTE:
        settings, profile = (
            profile_selector.settings_remote.pull_committed_settings()
        )
        launch_remote(app, settings, profile)

    # Launch Qt Thread
    log.info("Launching qt thread")

    try:
        ret = app.exec_()
    except BaseException:  # noqa: BLE001
        log.debug("Problems keeping thread alive", exc_info=True)

    # terminate connection after Qt Thread.
    # This can hang, use os._exit to kill it. We already cleaned up properly
    # in qrpcman

    # sys.exit(ret)
    os._exit(ret)