forked from aachman98/Sorcar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.py
262 lines (246 loc) · 9.15 KB
/
helper.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import bpy
from mathutils import Vector
def sc_poll_op(context):
space = context.space_data
if hasattr(space, "node_tree"):
if (space.node_tree):
return space.tree_type == "ScNodeTree"
return False
def sc_poll_mesh(self, object):
return object.type == "MESH"
def sc_poll_curve(self, object):
return object.type == "CURVE"
def sc_poll_curve_font(self, object):
return object.type in ["CURVE", "FONT"]
def sc_poll_lattice(self, object):
return object.type == "LATTICE"
def focus_on_object(obj, edit=False):
if (bpy.ops.object.mode_set.poll()):
bpy.ops.object.mode_set(mode="OBJECT")
bpy.ops.object.select_all(action="DESELECT")
if (obj):
if (obj.name in bpy.context.view_layer.objects):
bpy.context.view_layer.objects.active = obj
obj.select_set(True)
if (edit):
bpy.ops.object.mode_set(mode="EDIT")
def remove_object(obj):
if (obj):
try:
data = obj.data
type = obj.type
except:
return
bpy.data.objects.remove(obj, do_unlink=True, do_id_user=True)
if hasattr(data, "users"):
if data.users == 0:
if (type == 'MESH'):
bpy.data.meshes.remove(data, do_unlink=True, do_id_user=True)
elif (type in ['CURVE', 'FONT']):
bpy.data.curves.remove(data, do_unlink=True, do_id_user=True)
def apply_all_modifiers(object):
if (object):
if (object.name in bpy.context.view_layer.objects):
bpy.context.view_layer.objects.active = object
while(object.modifiers):
bpy.ops.object.modifier_apply(modifier=object.modifiers[0].name)
def get_override(active=None, edit=False, selected=[], type='VIEW_3D'):
override = bpy.context.copy()
if (type == 'VIEW_3D'):
override["active_object"] = active
if (edit):
override["edit_object"] = active
if (active not in selected):
selected.append(active)
override["selected_object"] = selected
flag = False
for window in bpy.data.window_managers[0].windows:
for area in window.screen.areas:
if area.type == type:
override["area"] = area
override["region"] = [i for i in area.regions if i.type == 'WINDOW'][0]
flag = True
break
if (flag):
break
return override
def print_log(parent=None, child=None, func=None, msg=""):
log = "SORCAR: "
if (parent):
log += parent + ": "
if (child):
log += child + ": "
if (func):
log += func + "(): "
log += msg
print(log)
def update_each_frame(scene):
for i in bpy.data.node_groups:
if (i.bl_idname == "ScNodeTree"):
if (i.prop_realtime):
i.execute_node()
def convert_data(data, from_type=None, to_type=None):
if (data == None or from_type == None or to_type == None):
return False, None
try:
if (to_type == "NUMBER"):
if (from_type == "NUMBER"):
val = data
elif (from_type == "BOOL"):
val = float(data)
elif (from_type == "STRING"):
val = float(eval(data))
elif (from_type == "VECTOR"):
val = Vector(data).magnitude
elif (from_type == "OBJECT"):
val = bpy.data.objects.find(data.name)
elif (from_type == "CURVE"):
val = bpy.data.curves.find(data.name)
elif (from_type == "ARRAY"):
val = len(eval(data))
elif (from_type == "SELECTION_TYPE"):
return False, None
elif (to_type == "BOOL"):
if (from_type == "NUMBER"):
val = bool(data)
elif (from_type == "BOOL"):
val = data
elif (from_type == "STRING"):
val = bool(data)
elif (from_type == "VECTOR"):
val = bool(data)
elif (from_type == "OBJECT"):
val = bool(data)
elif (from_type == "CURVE"):
val = bool(data)
elif (from_type == "ARRAY"):
val = bool(eval(data))
elif (from_type == "SELECTION_TYPE"):
val = len(data) != 0
elif (to_type == "STRING"):
if (from_type == "NUMBER"):
val = str(data)
elif (from_type == "BOOL"):
val = str(data)
elif (from_type == "STRING"):
val = data
elif (from_type == "VECTOR"):
val = str(Vector(data).to_tuple())
elif (from_type == "OBJECT"):
val = str(repr(data))
elif (from_type == "CURVE"):
val = str(repr(data))
elif (from_type == "ARRAY"):
val = data
elif (from_type == "SELECTION_TYPE"):
val = str(data)
elif (to_type == "VECTOR"):
if (from_type == "NUMBER"):
val = (data, data, data)
elif (from_type == "BOOL"):
val = (float(data), float(data), float(data))
elif (from_type == "STRING"):
val = Vector(eval(data)).to_tuple()
elif (from_type == "VECTOR"):
val = data
elif (from_type == "OBJECT"):
return False, None
elif (from_type == "CURVE"):
return False, None
elif (from_type == "ARRAY"):
val = Vector((eval(data)[0], eval(data)[1], eval(data)[2])).to_tuple()
elif (from_type == "SELECTION_TYPE"):
val = Vector(float("VERT" in data), float("EDGE" in data), float("FACE" in data)).to_tuple()
elif (to_type == "OBJECT"):
if (from_type == "NUMBER"):
val = bpy.data.objects[data]
elif (from_type == "BOOL"):
return False, None
elif (from_type == "STRING"):
val = bpy.data.objects[eval(data).name]
elif (from_type == "VECTOR"):
return False, None
elif (from_type == "OBJECT"):
val = data
elif (from_type == "CURVE"):
return False, None
elif (from_type == "ARRAY"):
return False, None
elif (from_type == "SELECTION_TYPE"):
return False, None
elif (to_type == "CURVE"):
if (from_type == "NUMBER"):
return False, None
elif (from_type == "BOOL"):
return False, None
elif (from_type == "STRING"):
return False, None
elif (from_type == "VECTOR"):
return False, None
elif (from_type == "OBJECT"):
return False, None
elif (from_type == "CURVE"):
val = data
elif (from_type == "ARRAY"):
return False, None
elif (from_type == "SELECTION_TYPE"):
return False, None
elif (to_type == "ARRAY"):
if (from_type == "NUMBER"):
val = "[" + str(data) + "]"
elif (from_type == "BOOL"):
val = "[" + str(data) + "]"
elif (from_type == "STRING"):
val = str(list(eval(data)))
elif (from_type == "VECTOR"):
val = str(list(data))
elif (from_type == "OBJECT"):
val = "[" + repr(data) + "]"
elif (from_type == "CURVE"):
val = "[" + repr(data) + "]"
elif (from_type == "ARRAY"):
val = data
elif (from_type == "SELECTION_TYPE"):
val = str(list(data))
elif (to_type == "SELECTION_TYPE"):
if (from_type == "NUMBER"):
return False, None
elif (from_type == "BOOL"):
return False, None
elif (from_type == "STRING"):
val = eval(data)
elif (from_type == "VECTOR"):
val = set()
if bool(data[0]):
val.add("VERT")
if bool(data[1]):
val.add("EDGE")
if bool(data[2]):
val.add("FACE")
elif (from_type == "OBJECT"):
return False, None
elif (from_type == "CURVE"):
return False, None
elif (from_type == "ARRAY"): # Allows you to take in a boolean array
data_eval = eval(data)
val = set()
if bool(data_eval[0]):
val.add("VERT")
if bool(data_eval[1]):
val.add("EDGE")
if bool(data_eval[2]):
val.add("FACE")
elif (from_type == "SELECTION_TYPE"):
val = data
return True, val
except:
return False, None
def selection_type_to_string(sel_type):
out = []
if "VERT" in sel_type:
out.append("Vertex")
if "EDGE" in sel_type:
out.append("Edge")
if "FACE" in sel_type:
out.append("Face")
return " + ".join(out)