From e8544477141cd76feacb3d4ff721815e01187a3c Mon Sep 17 00:00:00 2001 From: Dion Moult Date: Wed, 6 Mar 2024 16:44:32 +1100 Subject: [PATCH] Little debug tool for people to easily install pip packages graphically BBIM is being used in companies as ways to automate various BIM processes, and it ends up being such that Blender Python becomes their dev environment. --- .../blenderbim/bim/module/debug/__init__.py | 5 ++- .../blenderbim/bim/module/debug/operator.py | 37 +++++++++++++++++++ .../blenderbim/bim/module/debug/prop.py | 1 + .../blenderbim/bim/module/debug/ui.py | 4 ++ 4 files changed, 45 insertions(+), 2 deletions(-) diff --git a/src/blenderbim/blenderbim/bim/module/debug/__init__.py b/src/blenderbim/blenderbim/bim/module/debug/__init__.py index a05c74561a..93f3713205 100644 --- a/src/blenderbim/blenderbim/bim/module/debug/__init__.py +++ b/src/blenderbim/blenderbim/bim/module/debug/__init__.py @@ -28,18 +28,19 @@ classes = ( operator.InspectFromStepId, operator.OverrideDisplayType, operator.ParseExpress, + operator.PipInstall, operator.PrintIfcFile, operator.PrintObjectPlacement, + operator.PrintUnusedElementStats, operator.ProfileImportIFC, operator.PurgeHdf5Cache, operator.PurgeIfcLinks, + operator.PurgeUnusedElementsByClass, operator.RewindInspector, operator.SelectExpressFile, operator.SelectHighPolygonMeshes, operator.SelectHighestPolygonMeshes, operator.ValidateIfcFile, - operator.PrintUnusedElementStats, - operator.PurgeUnusedElementsByClass, prop.BIMDebugProperties, ui.BIM_PT_debug, ) diff --git a/src/blenderbim/blenderbim/bim/module/debug/operator.py b/src/blenderbim/blenderbim/bim/module/debug/operator.py index f3d052428a..1a1f80f123 100644 --- a/src/blenderbim/blenderbim/bim/module/debug/operator.py +++ b/src/blenderbim/blenderbim/bim/module/debug/operator.py @@ -17,6 +17,7 @@ # along with BlenderBIM Add-on. If not, see . import os +import sys import bpy import time import logging @@ -623,3 +624,39 @@ class PurgeUnusedElementsByClass(bpy.types.Operator, tool.Ifc.Operator): print("Finished 20 batches. Manually stopping in case of infinite loop.") self.report({"INFO"}, f"Auto purged {total_purged} orphaned elements") tool.Ifc.get().write(self.filepath) + + +class PipInstall(bpy.types.Operator): + bl_idname = "bim.pip_install" + bl_label = "Pip Install" + bl_description = "Installs a package from PyPI" + name: bpy.props.StringProperty() + + def execute(self, context): + import blenderbim.bim + + target = [p for p in sys.path if p.endswith("packages") and "addons" in p][0] + py_exec = str(sys.executable) + + print("Detected executable:", py_exec) + print("Installing to:", target) + + subprocess.call([py_exec, "-m", "ensurepip", "--user"]) + subprocess.call([py_exec, "-m", "pip", "install", "--upgrade", "pip"]) + # Trusted host to make things simpler due to some enterprise network restrictions + subprocess.call( + [ + py_exec, + "-m", + "pip", + "install", + f"--target={target}", + "--upgrade", + self.name, + "--trusted-host", + "pypi.org", + "--trusted-host", + "files.pythonhosted.org", + ] + ) + return {"FINISHED"} diff --git a/src/blenderbim/blenderbim/bim/module/debug/prop.py b/src/blenderbim/blenderbim/bim/module/debug/prop.py index fff62ef57b..456e2ad45c 100644 --- a/src/blenderbim/blenderbim/bim/module/debug/prop.py +++ b/src/blenderbim/blenderbim/bim/module/debug/prop.py @@ -51,3 +51,4 @@ class BIMDebugProperties(PropertyGroup): default="BOUNDS", ) ifc_class_purge: StringProperty(name="Unused Elements IFC Class", default="") + package_name: StringProperty(name="Package Name", default="") diff --git a/src/blenderbim/blenderbim/bim/module/debug/ui.py b/src/blenderbim/blenderbim/bim/module/debug/ui.py index ebe111cce2..21a6e11f80 100644 --- a/src/blenderbim/blenderbim/bim/module/debug/ui.py +++ b/src/blenderbim/blenderbim/bim/module/debug/ui.py @@ -44,6 +44,10 @@ class BIM_PT_debug(Panel): row.operator("bim.parse_express", icon="IMPORT", text="") row.operator("bim.select_express_file", icon="FILE_FOLDER", text="") + row = self.layout.row(align=True) + row.prop(props, "package_name", text="") + row.operator("bim.pip_install", icon="EVENT_PAGEDOWN").name = props.package_name + row = layout.row() row.operator("bim.reload_ifc_file", text="Incrementally Reload Changes")