-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
153 lines (112 loc) · 4.61 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
'''
Copyright (C) 2023 Orange Turbine
https://orangeturbine.com
Created by Jason van Gumster
This file is part of an Install Dependencies Example.
This example is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 3
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, see <https://www.gnu.org/licenses/>.
'''
bl_info = {
"name": "Install Dependencies Example",
"author": "Jason van Gumster, Jonathan Denning",
"version": (1, 0),
"blender": (3, 4, 0),
"location": "Preferences",
"description": "A simple example of how to install external Python modules locally",
"warning": "This is only an example!",
"doc_url": "https://github.com/CGCookie/install_deps_example/",
"category": "Example",
}
import bpy
from bpy.types import AddonPreferences, Operator, Panel
import sys
import os
# Local imports
from .dependencies import Dependencies
from .example_operator import EXAMPLE_OT_operate
class EXAMPLE_OT_install_dependencies(Operator):
bl_idname = "preferences.example_install_dependencies"
bl_label = "Install dependencies"
bl_description = ("Downloads and installs the required Python packages for this add-on. "
"Internet connection is required. Packages are installed locally to "
"this add-on, not to the system Python or Blender's Python.")
bl_options = {"REGISTER", "INTERNAL"}
@classmethod
def poll(self, context):
# Deactivate when dependencies have been installed
return not Dependencies.check()
def execute(self, context):
if not Dependencies.install():
return {'CANCELLED'}
# Register any classes that need registering once dependencies are installed
register_classes_with_dependencies()
return {"FINISHED"}
class EXAMPLE_AddonPreferences(AddonPreferences):
""" Preferences for this dummy add-on """
bl_idname = __name__
def draw(self, context):
layout = self.layout
layout.label(text=f"This add-on requires a couple Python packages to be installed:")
for name in Dependencies.requirements():
layout.label(text=f'- {name}')
layout.label(text=f"Click the Install Dependencies button below to install.")
layout.operator(EXAMPLE_OT_install_dependencies.bl_idname, icon="CONSOLE")
class EXAMPLE_PT_Panel(Panel):
bl_label = "Example Panel"
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
bl_category = "Example Tab"
bl_context = "objectmode"
def draw(self, context):
layout = self.layout
if Dependencies.check():
layout.operator("example.operate")
else:
layout.label(text="You need to install some packages before using this tool")
layout.label(text="Click below to go to Preferences and install them.")
layout.operator("screen.userpref_show")
classes_with_dependencies = [
EXAMPLE_OT_operate,
]
registered_classes_with_dependencies = False
classes_example = [
EXAMPLE_OT_install_dependencies,
EXAMPLE_AddonPreferences,
EXAMPLE_PT_Panel,
]
def register_classes_with_dependencies():
global registered_classes_with_dependencies
if registered_classes_with_dependencies:
# Already registered classes
return
if not Dependencies.check(force=True):
# Dependencies are not installed, so cannot register the classes
return
for cls in classes_with_dependencies:
bpy.utils.register_class(cls)
registered_classes_with_dependencies = True
def unregister_classes_with_dependencies():
global registered_classes_with_dependencies
if not registered_classes_with_dependencies:
# No registered classes needing unregistered
return
for cls in reversed(classes_with_dependencies):
bpy.utils.unregister_class(cls)
registered_classes_with_dependencies = False
def register():
for cls in classes_example:
bpy.utils.register_class(cls)
register_classes_with_dependencies()
def unregister():
unregister_classes_with_dependencies()
for cls in reversed(classes_example):
bpy.utils.unregister_class(cls)