|
7 | 7 |
|
8 | 8 | from .. import state |
9 | 9 | from ..agent.messages import Phase |
| 10 | +from ..integrations import registry as helpers |
10 | 11 |
|
11 | 12 | PREVIEW_LINES = 18 |
| 13 | +HELPERS_DIALOG_ROWS = 7 |
12 | 14 |
|
13 | 15 |
|
14 | 16 | def _session_pending(call_id: str = "") -> Any: |
@@ -102,7 +104,94 @@ def cancel(self, context: Any) -> None: |
102 | 104 | session.approve(False) |
103 | 105 |
|
104 | 106 |
|
105 | | -CLASSES = (BLENDER_AI_OT_approval_dialog,) |
| 107 | +def _prefs() -> Any: |
| 108 | + from ..preferences import get_prefs |
| 109 | + |
| 110 | + return get_prefs() |
| 111 | + |
| 112 | + |
| 113 | +def open_helpers_prompt() -> None: |
| 114 | + prefs = _prefs() |
| 115 | + if bpy.app.background or prefs is None or prefs.helpers_prompted or not bpy.app.online_access or not helpers.missing(): |
| 116 | + return |
| 117 | + override = _view3d_override() |
| 118 | + if override is not None: |
| 119 | + window = override["window"] |
| 120 | + unit = 20 * bpy.context.preferences.system.ui_scale |
| 121 | + window.cursor_warp(window.width // 2 - round(2.5 * unit), window.height // 2 + round(HELPERS_DIALOG_ROWS * unit / 2)) |
| 122 | + with bpy.context.temp_override(**override): |
| 123 | + bpy.ops.blender_ai.install_helpers("INVOKE_DEFAULT") |
| 124 | + |
| 125 | + |
| 126 | +def open_helpers_prompt_later() -> None: |
| 127 | + if not bpy.app.background: |
| 128 | + bpy.app.timers.register(open_helpers_prompt, first_interval=1.5) |
| 129 | + |
| 130 | + |
| 131 | +def _helpers_finished(missing: list[str]) -> None: |
| 132 | + session = state.get().session |
| 133 | + if session is not None: |
| 134 | + text = f"Couldn't install {', '.join(missing)}; see the Info editor." if missing else "Helper add-ons installed and enabled." |
| 135 | + session.notify(text, error=bool(missing)) |
| 136 | + for window in bpy.context.window_manager.windows: |
| 137 | + for area in window.screen.areas: |
| 138 | + area.tag_redraw() |
| 139 | + |
| 140 | + |
| 141 | +class BLENDER_AI_OT_install_helpers(bpy.types.Operator): |
| 142 | + bl_idname = "blender_ai.install_helpers" |
| 143 | + bl_label = "Install helper add-ons" |
| 144 | + bl_description = "Install and enable the add-ons Blender AI builds with: Rigify, MPFB, LoopTools and Extra Mesh Objects" |
| 145 | + bl_options = {"INTERNAL"} |
| 146 | + |
| 147 | + @classmethod |
| 148 | + def poll(cls, context: Any) -> bool: |
| 149 | + return bool(helpers.missing()) |
| 150 | + |
| 151 | + def invoke(self, context: Any, event: Any) -> set[str]: |
| 152 | + wm = context.window_manager |
| 153 | + try: |
| 154 | + return wm.invoke_props_dialog(self, width=520, title="Install helper add-ons?", confirm_text="Install") |
| 155 | + except TypeError: |
| 156 | + return wm.invoke_props_dialog(self, width=520) |
| 157 | + |
| 158 | + def draw(self, context: Any) -> None: |
| 159 | + layout = self.layout |
| 160 | + layout.label(text="Blender AI builds better models, rigs and characters with these add-ons:", icon="PACKAGE") |
| 161 | + col = layout.box().column(align=True) |
| 162 | + for helper in helpers.HELPERS.values(): |
| 163 | + ready = helpers.enabled_module(helper.id) is not None |
| 164 | + source = "included with Blender" if helper.bundled else f"extensions.blender.org, {helper.size}" |
| 165 | + col.label(text=f"{helper.name}: {'enabled' if ready else source}", icon="CHECKBOX_HLT" if ready else "CHECKBOX_DEHLT") |
| 166 | + col = layout.column(align=True) |
| 167 | + col.label(text="Install downloads the missing ones from extensions.blender.org and enables them all.", icon="INFO") |
| 168 | + col.label(text="Cancel skips this; you can install them later in Blender AI's preferences.") |
| 169 | + |
| 170 | + def execute(self, context: Any) -> set[str]: |
| 171 | + from ..integrations import installer |
| 172 | + |
| 173 | + prefs = _prefs() |
| 174 | + if prefs is not None: |
| 175 | + prefs.helpers_prompted = True |
| 176 | + try: |
| 177 | + enabled, downloading = installer.install(interactive=context.window is not None) |
| 178 | + except installer.InstallError as exc: |
| 179 | + self.report({"ERROR"}, str(exc)) |
| 180 | + return {"CANCELLED"} |
| 181 | + if downloading: |
| 182 | + installer.watch(_helpers_finished) |
| 183 | + self.report({"INFO"}, (f"Enabled {', '.join(enabled)}. " if enabled else "") + f"Installing {', '.join(downloading)}…") |
| 184 | + else: |
| 185 | + _helpers_finished([h.name for h in helpers.missing()]) |
| 186 | + return {"FINISHED"} |
| 187 | + |
| 188 | + def cancel(self, context: Any) -> None: |
| 189 | + prefs = _prefs() |
| 190 | + if prefs is not None: |
| 191 | + prefs.helpers_prompted = True |
| 192 | + |
| 193 | + |
| 194 | +CLASSES = (BLENDER_AI_OT_approval_dialog, BLENDER_AI_OT_install_helpers) |
106 | 195 |
|
107 | 196 |
|
108 | 197 | def register() -> None: |
|
0 commit comments