Skip to content

Commit

Permalink
Dependencies fix (#17)
Browse files Browse the repository at this point in the history
* Checkpoint adding some changes

* Adding more handling

* Adding fixes
  • Loading branch information
UncleFirefox authored Apr 13, 2021
1 parent a15da71 commit bb6aff4
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 81 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
"python.analysis.extraPaths": [
"blender_autocomplete"
],
"python.pythonPath": "/home/albert/anaconda3/bin/python",
}
11 changes: 10 additions & 1 deletion addon/panel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
from .measures_circular_panel import MEASURES_PT_CIRCULAR_PANEL

classes = [
MEASURES_PT_MAINPANEL,
MEASURES_PT_CIRCULAR_PANEL,
MEASURES_PT_GEODESIC_PANEL
]


def register_main_panel():
from bpy.utils import register_class
register_class(MEASURES_PT_MAINPANEL)


def register_panels():
from bpy.utils import register_class
for cls in classes:
Expand All @@ -19,3 +23,8 @@ def unregister_panels():
from bpy.utils import unregister_class
for cls in reversed(classes):
unregister_class(cls)


def unregister_main_panel():
from bpy.utils import unregister_class
unregister_class(MEASURES_PT_MAINPANEL)
29 changes: 3 additions & 26 deletions addon/panel/measures_geodesic_panel.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from ..register.dependency_flag import are_dependencies_installed
from ..register.dependency_handling import \
are_dependencies_installed, show_no_dependencies_warning

import bpy


Expand All @@ -16,35 +18,10 @@ def draw(self, context):
layout = self.layout
layout.scale_y = 1.2

if are_dependencies_installed():
self.show_operator(layout)
else:
self.show_no_dependencies_warning(layout)

def show_operator(self, layout):
row = layout.row()
row.label(
text="Select a origin and destiny to generate a path",
icon="MOD_TINT"
)
row = layout.row()
row.operator('measures.create_geodesic')

def show_no_dependencies_warning(self, layout):

lines = [f"Please install the missing dependencies for the Measures add-on.",
f"1. Open the preferences (Edit > Preferences > Add-ons).",
f"2. Search for the Measures Library add-on.",
f"3. Open the details section of the add-on.",
f"4. Click on the Install Dependencies button.",
f" This will download and install the missing Python packages, if Blender has the required",
f" permissions.",
f"If you're attempting to run the add-on from the text editor, you won't see the options described",
f"above. Please install the add-on properly through the preferences.",
f"1. Open the add-on preferences (Edit > Preferences > Add-ons).",
f"2. Press the \"Install\" button.",
f"3. Search for the add-on file.",
f"4. Confirm the selection by pressing the \"Install Add-on\" button in the file browser."]

for line in lines:
layout.label(text=line)
6 changes: 6 additions & 0 deletions addon/panel/measures_main_panel.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from ..register.dependency_handling import \
are_dependencies_installed, show_no_dependencies_warning

import bpy


Expand All @@ -15,6 +18,9 @@ def draw(self, context):
layout = self.layout
layout.scale_y = 1.2

if not are_dependencies_installed():
show_no_dependencies_warning(layout)

# row = layout.row()
# row.label(text="Adjust the plane to the Avatar", icon="MOD_TINT")

Expand Down
4 changes: 3 additions & 1 deletion addon/preferences/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from ..register.dependency_handling import MEASURES_OT_Install_Dependencies
from ..preferences.install_dependencies_operator import \
MEASURES_OT_Install_Dependencies

from .addon import MEASURES_Props
from .color import MEASURES_Color
from .settings import MEASURES_Settings
Expand Down
5 changes: 4 additions & 1 deletion addon/preferences/addon.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from ..register.dependency_handling import MEASURES_OT_Install_Dependencies
import bpy

from .install_dependencies_operator import \
MEASURES_OT_Install_Dependencies

from ..utility.addon import addon_name, get_prefs
from bpy.props import PointerProperty

Expand Down
40 changes: 40 additions & 0 deletions addon/preferences/install_dependencies_operator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import subprocess
from ..register.dependency_handling import \
are_dependencies_installed, get_dependencies,\
install_and_import_module, install_pip, set_dependency_installed_flag

from ..register import register_dependent_objects

import bpy


class MEASURES_OT_Install_Dependencies(bpy.types.Operator):
bl_idname = "measures.install_dependencies"
bl_label = "Install dependencies"
bl_description = ("Downloads and installs the required python packages for this add-on. "
"Internet connection is required. Blender may have to be started with "
"elevated permissions in order to install the package")
bl_options = {"REGISTER", "INTERNAL"}

@classmethod
def poll(self, context):
# Deactivate when dependencies have been installed
return not are_dependencies_installed()

def execute(self, context):
try:
install_pip()
for dependency in get_dependencies():
install_and_import_module(module_name=dependency.module,
package_name=dependency.package,
global_name=dependency.name)
except (subprocess.CalledProcessError, ImportError) as err:
self.report({"ERROR"}, str(err))
return {"CANCELLED"}

set_dependency_installed_flag(True)

# Register the panels, operators, etc. since dependencies are installed
register_dependent_objects()

return {"FINISHED"}
46 changes: 31 additions & 15 deletions addon/register/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from ..register.dependency_handling import \
are_dependencies_installed, \
import_dependencies, \
set_dependency_installed_flag

Expand All @@ -11,27 +12,18 @@ def register_addon():
from ..preferences import register_preferences
register_preferences()

# Main panel
from ..panel import register_main_panel
register_main_panel()

try:
import_dependencies()
set_dependency_installed_flag(True)
except ModuleNotFoundError:
print("Dependencies were not installed...")
return

# Menus
# from ..menu import register_menus
# register_menus()

# Panels
from ..panel import register_panels
register_panels()

# Operators
from ..operator import register_operators
register_operators()

# Keymaps
# from .keymap import register_keymap
# register_keymap()
register_dependent_objects()


def unregister_addon():
Expand All @@ -40,6 +32,12 @@ def unregister_addon():
from ..preferences import unregister_preferences
unregister_preferences()

from ..panel import unregister_main_panel
unregister_main_panel()

if not are_dependencies_installed():
return

# Menus
# from ..menu import unregister_menus
# unregister_menus()
Expand All @@ -55,3 +53,21 @@ def unregister_addon():
# Keymaps
# from .keymap import unregister_keymap
# unregister_keymap()


def register_dependent_objects():
# Menus
# from ..menu import register_menus
# register_menus()

# Panels
from ..panel import register_panels
register_panels()

# Operators
from ..operator import register_operators
register_operators()

# Keymaps
# from .keymap import register_keymap
# register_keymap()
11 changes: 0 additions & 11 deletions addon/register/dependency_flag.py

This file was deleted.

57 changes: 31 additions & 26 deletions addon/register/dependency_handling.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from .dependency_flag import are_dependencies_installed, set_dependency_installed_flag
from collections import namedtuple
from typing import Tuple
import bpy
import subprocess
import sys
import os
Expand All @@ -17,34 +15,21 @@
dependencies: Tuple[Dependency] = (Dependency(module="potpourri3d",
package=None, name=None),)

global dependencies_installed

class MEASURES_OT_Install_Dependencies(bpy.types.Operator):
bl_idname = "measures.install_dependencies"
bl_label = "Install dependencies"
bl_description = ("Downloads and installs the required python packages for this add-on. "
"Internet connection is required. Blender may have to be started with "
"elevated permissions in order to install the package")
bl_options = {"REGISTER", "INTERNAL"}

@classmethod
def poll(self, context):
# Deactivate when dependencies have been installed
return not are_dependencies_installed()
def set_dependency_installed_flag(flag: bool):
global dependencies_installed
dependencies_installed = flag

def execute(self, context):
try:
install_pip()
for dependency in dependencies:
install_and_import_module(module_name=dependency.module,
package_name=dependency.package,
global_name=dependency.name)
except (subprocess.CalledProcessError, ImportError) as err:
self.report({"ERROR"}, str(err))
return {"CANCELLED"}

set_dependency_installed_flag(True)
def are_dependencies_installed() -> bool:
global dependencies_installed
return dependencies_installed

return {"FINISHED"}

def get_dependencies():
return dependencies


def import_dependencies():
Expand Down Expand Up @@ -125,4 +110,24 @@ def install_and_import_module(module_name, package_name=None, global_name=None):
subprocess.run([sys.executable, "-m", "pip", "install", package_name], check=True, env=environ_copy)

# The installation succeeded, attempt to import the module again
import_module(module_name, global_name)
import_module(module_name, global_name)


def show_no_dependencies_warning(layout):

lines = [f"Please install the missing dependencies for the Measures add-on.",
f"1. Open the preferences (Edit > Preferences > Add-ons).",
f"2. Search for the Measures Library add-on.",
f"3. Open the details section of the add-on.",
f"4. Click on the Install Dependencies button.",
f" This will download and install the missing Python packages, if Blender has the required",
f" permissions.",
f"If you're attempting to run the add-on from the text editor, you won't see the options described",
f"above. Please install the add-on properly through the preferences.",
f"1. Open the add-on preferences (Edit > Preferences > Add-ons).",
f"2. Press the \"Install\" button.",
f"3. Search for the add-on file.",
f"4. Confirm the selection by pressing the \"Install Add-on\" button in the file browser."]

for line in lines:
layout.label(text=line)

0 comments on commit bb6aff4

Please sign in to comment.