forked from aachman98/Sorcar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
183 lines (158 loc) · 6.37 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# Copyright (C) 2019 Punya Aachman
#
# This program 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
# MERCHANTIBILITY 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 <http://www.gnu.org/licenses/>.
bl_info = {
"name": "Sorcar",
"author": "Punya Aachman",
"version": (3, 2, 0),
"blender": (2, 80, 0),
"location": "Node Editor",
"category": "Node",
"description": "Create procedural meshes using Node Editor",
"wiki_url": "https://github.com/aachman98/Sorcar/wiki",
"tracker_url": "https://github.com/aachman98/Sorcar/issues"
}
import bpy
import nodeitems_utils
import addon_utils
import importlib
import os
from bpy.types import NodeTree, Operator, PropertyGroup, AddonPreferences
from bpy.props import BoolProperty, StringProperty, IntProperty
from nodeitems_utils import NodeItem
from .tree.ScNodeCategory import ScNodeCategory
from .tree.ScNodeTree import ScNodeTree
from .helper import update_each_frame, print_log
from . import addon_updater_ops
class SorcarPreferences(AddonPreferences):
bl_idname = __package__
bl_label = "Sorcar Preferences"
auto_check_update: BoolProperty(
name = "Auto-check for Update",
description = "If enabled, auto-check for updates using an interval",
default = False,
)
updater_intrval_months: IntProperty(
name='Months',
description = "Number of months between checking for updates",
default=0,
min=0
)
updater_intrval_days: IntProperty(
name='Days',
description = "Number of days between checking for updates",
default=7,
min=0,
)
updater_intrval_hours: IntProperty(
name='Hours',
description = "Number of hours between checking for updates",
default=0,
min=0,
max=23
)
updater_intrval_minutes: IntProperty(
name='Minutes',
description = "Number of minutes between checking for updates",
default=0,
min=0,
max=59
)
def draw(self, context):
addon_updater_ops.update_settings_ui(self, context)
def import_ops(path="./"):
out = []
for i in bpy.path.module_names(path + "operators"):
out.append(getattr(importlib.import_module(".operators." + i[0], __name__), i[0]))
print_log("IMPORT OP", msg=i[0])
return out
def import_sockets(path="./"):
out = []
for i in bpy.path.module_names(path + "sockets"):
out.append(getattr(importlib.import_module(".sockets." + i[0], __name__), i[0]))
print_log("IMPORT SOCKET", msg=i[0])
return out
def import_ui(path="./"):
out = []
for i in bpy.path.module_names(path + "ui"):
out.append(getattr(importlib.import_module(".ui." + i[0], __name__), i[0]))
print_log("IMPORT UI", msg=i[0])
return out
def import_nodes(path="./"):
out = {}
for cat in [i for i in os.listdir(path + "nodes") if not i.startswith("_")]:
out[cat] = []
for i in bpy.path.module_names(path + "nodes/" + cat):
out[cat].append(getattr(importlib.import_module(".nodes." + cat + "." + i[0], __name__), i[0]))
print_log("IMPORT NODE", bpy.path.display_name(cat), msg=i[0])
return out
def init_keymaps():
kc = bpy.context.window_manager.keyconfigs.addon
km = kc.keymaps.new(name="Node Generic", space_type='NODE_EDITOR')
kmi = [
km.keymap_items.new("sorcar.execute_node", 'E', 'PRESS'),
km.keymap_items.new("sorcar.clear_preview", 'E', 'PRESS', alt=True),
km.keymap_items.new("sorcar.group_nodes", 'G', 'PRESS', ctrl=True),
km.keymap_items.new("sorcar.edit_group", 'TAB', 'PRESS')
]
return km, kmi
all_classes = []
addon_keymaps = []
def register():
print("-------------REGISTER SORCAR-------------")
path = repr([i for i in addon_utils.modules() if i.bl_info['name'] == "Sorcar"][0]).split("from '")[1].split("__init__.py'>")[0]
classes_ops = import_ops(path)
classes_sockets = import_sockets(path)
classes_ui = import_ui(path)
classes_nodes = import_nodes(path)
global all_classes, addon_keymaps
all_classes = [ScNodeTree]
all_classes.extend(classes_ops)
all_classes.extend(classes_sockets)
all_classes.extend(classes_ui)
all_classes.append(SorcarPreferences)
total_nodes = 0
node_categories = []
for cat in classes_nodes:
total_nodes += len(classes_nodes[cat])
node_categories.append(ScNodeCategory(identifier="sc_"+cat, name=bpy.path.display_name(cat), items=[NodeItem(i.bl_idname) for i in classes_nodes[cat]]))
all_classes.extend(classes_nodes[cat])
for i in all_classes:
bpy.utils.register_class(i)
nodeitems_utils.register_node_categories("sc_node_categories", node_categories)
if not (update_each_frame in bpy.app.handlers.frame_change_post):
bpy.app.handlers.frame_change_post.append(update_each_frame)
if (not bpy.app.background):
km, kmi = init_keymaps()
for k in kmi:
k.active = True
addon_keymaps.append((km, k))
addon_updater_ops.register(bl_info)
print_log("REGISTERED", msg="{} operators, {} sockets, {} UI, {} keymaps & {} nodes ({} categories)".format(len(classes_ops), len(classes_sockets), len(classes_ui), len(addon_keymaps), total_nodes, len(classes_nodes)))
def unregister():
print("------------UNREGISTER SORCAR----------------")
global all_classes, addon_keymaps
all_classes.reverse()
for i in all_classes:
bpy.utils.unregister_class(i)
print_log("UNREGISTER", i.bl_idname, None, i.bl_label)
nodeitems_utils.unregister_node_categories("sc_node_categories")
if (update_each_frame in bpy.app.handlers.frame_change_post):
bpy.app.handlers.frame_change_post.remove(update_each_frame)
for km, kmi in addon_keymaps:
km.keymap_items.remove(kmi)
addon_keymaps.clear()
addon_updater_ops.unregister()
print_log("UNREGISTERED", msg=str(len(all_classes)) + " classes")