-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIs.py
More file actions
373 lines (312 loc) · 10.5 KB
/
Is.py
File metadata and controls
373 lines (312 loc) · 10.5 KB
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
"Find if an item 'is' a particular thing"
import bpy
from zpy import Get
# -------------------------------------------------------------------------
# region: Python types
def _bool(src):
"""item is True or False"""
return isinstance(src, bool)
def digit(src):
"""item is a number"""
return (Is.float(src) or Is.int(src))
def _float(src):
"""src is a decimal number"""
if Is.string(src):
try:
src = Get.as_float(src)
except:
return False
return isinstance(src, float)
def _int(src):
"""src is a whole number"""
if Is.string(src):
try:
src = Get.as_int(src)
except:
return False
return isinstance(src, int) and not Is.bool(src)
def iterable(src):
"""item can be search like a list"""
import mathutils
return any((
hasattr(src, '__iter__'),
isinstance(src, dict),
isinstance(src, list),
isinstance(src, set),
isinstance(src, tuple),
isinstance(src, mathutils.Euler),
isinstance(src, mathutils.Matrix),
isinstance(src, mathutils.Quaternion),
isinstance(src, mathutils.Vector),
))
def matrix(src):
"""item is a matrix"""
import mathutils
return isinstance(src, mathutils.Matrix)
def string(src):
"""item is a text string"""
return isinstance(src, str)
# endregion
# -------------------------------------------------------------------------
# -------------------------------------------------------------------------
# region: Blender type
def armature(src, is_object=True):
"""
src is a rig/armature \\
is_object means check if it is specifically the armature-object
"""
if Is.object(src):
arm = src.data
else:
if is_object:
return False
arm = src
return isinstance(arm, bpy.types.Armature)
def bone(src):
"""
src is a generic bone\\
The sub.bone of a posebone or a bone from the armature(data).bones
"""
return isinstance(src, bpy.types.Bone)
def camera(src):
"""src is a camera object"""
return bool(Is.object(src) and src.type in {'CAMERA'})
def collection(src):
return isinstance(src, bpy.types.Collection)
def curve(src):
"""src is a curve object"""
return bool(Is.object(src) and src.type in {'CURVE'})
def editbone(src):
"""src is a bone from edit mode"""
return isinstance(src, bpy.types.EditBone)
def empty(src):
"""src is an empty object"""
return bool(Is.object(src) and not src.data)
def gpencil(src):
"""src is a grease pencil object"""
return bool(Is.object(src) and src.type == 'GPENCIL')
def light(src):
"""src is a light object"""
return bool(Is.object(src) and src.type in {'LIGHT', 'LAMP'})
def mesh(src):
"""Item is a mesh object"""
return bool(Is.object(src) and src.type in {'MESH'})
def nla_strip(src):
"""src is a nla strip"""
return isinstance(src, bpy.types.NlaStrip)
def object(src):
"""src is an object"""
return isinstance(src, bpy.types.Object)
def posebone(src):
"""src is a bone from pose mode"""
return isinstance(src, bpy.types.PoseBone)
# endregion
# -------------------------------------------------------------------------
# -------------------------------------------------------------------------
# region: Status
def animation_playing(context):
"""Return if the active screen is running the timeline playback"""
scr = context.screen
if scr:
return (scr.is_animation_playing and not scr.is_scrubbing)
def connected(src, pseudo=True):
"""
Returns if a bone is connected to it's parent\\
or pseudo connected (the head is at the parent's tail)
"""
# if not src.bone.use_connect and (not src.parent or src.bone.head_local != src.bone.parent.tail_local):
if Is.posebone(src):
bone = src.bone
elif Is.bone(src) or Is.editbone(src):
bone = src
src = Get.rig(bpy.context, src).pose.bones.get(src.name)
else:
if not Is.object(src):
assert None, ("Is.connected() checked on invalid item",
src, type(src)
)
return
if bone.parent:
if (bone.use_connect):
return True
elif pseudo:
if Is.bone(src):
connect = (bone.head_local == bone.parent.tail_local)
else:
connect = (bone.head == bone.parent.tail)
if connect:
if src and (not sum(src.lock_location)) and (sum(src.location) >= 0.0001):
# bone wasn't locked and was moved and wasn't locked
return False
else:
# If pose bone doesn't exist, it was just created
return True
return False
def in_dopesheet(context, src):
"""
src is visible in the animation editor (provided it has animation data)
"""
src = getattr(src, 'id_data', None)
space = Get.space(context, 'dopesheet')
if not Is.object(src):
# there's a filter for FCurves, and datablocks can be used too,
# but just ignore them for now
return
poll = not any((
# Isolation
not (Is.visible(context, src) or space.show_hidden),
# 'show_missing_nla': True,
space.show_only_selected and not Is.selected(src),
# Main Filter
not (
space.filter_collection and
src.name in space.filter_collection.objects,
),
# 'filter_fcurve_name': '',
# 'filter_text': '',
# Filters
# 'show_armatures': True,
# 'show_cache_files': True,
# 'show_cameras': True,
# 'show_curves': True,
# 'show_datablock_filters': False,
# 'show_expanded_summary': True,
# 'show_gpencil': True,
# 'show_gpencil_3d_only': False,
# 'show_lattices': True,
# 'show_lights': True,
# 'show_lamps': True, # 27
# 'show_linestyles': True,
# 'show_materials': True,
# 'show_meshes': True,
# 'show_metaballs': True,
# 'show_modifiers': True,
# 'show_nodes': True,
# 'show_only_errors': False,
# 'show_only_matching_fcurves': False, # 27
# 'show_particles': True,
# 'show_scenes': True,
# 'show_shapekeys': True,
# 'show_speakers': True,
# 'show_summary': False,
# 'show_textures': True,
# 'show_transforms': True,
# 'show_worlds': True,
))
return poll
def in_scene(context, src):
"""src is in the active scene"""
return (src in Get.in_scene(context))
def in_view(context, src, *views):
"""src is in the active view layer"""
filters = dict(object=Is.object(src), collection=Is.collection(src))
return (src in Get.in_view(context, *views, **filters))
def in_visible_armature_layers(bone, arm):
"""Bone is on a visibile layer of the armature (data)"""
if Is.posebone(bone):
bone = bone.bone
if Is.armature(arm):
arm = arm.data
return [
i for i, j in zip(bone.layers, arm.layers)
if i and j
]
def linked(src):
"""Find if something is from an external blend file"""
if Is.object(src):
if (src.library or src.proxy):
return True
elif (src.data and src.data.library):
return True
else:
return False
elif Is.armature(src, False):
return bool(src.library)
elif Is.posebone(src) or Is.bone(src):
return Is.linked(src.id_data)
elif Is.editbone(src):
# You can't access edit bones without edit mode
return False
else:
assert None, ("Have not calculated for this data type " + repr(src))
def panel_expanded(context):
"""
Try to find if the sidebar is stretched enough to see button text\\
Returns bool and whether 1 or 2 buttons are visible
"""
from zpy import utils
# Currently this just checks the width,
# we could have different layouts as preferences too.
region = context.region
system = utils.prefs().system
view2d = region.view2d
view2d_scale = (
view2d.region_to_view(1.0, 0.0)[0] -
view2d.region_to_view(0.0, 0.0)[0]
)
width_scale = region.width * view2d_scale / \
getattr(system, 'ui_scale', 1.0)
show_text = (width_scale > 120.0)
columns = (80.0 < width_scale < 120.0)
# print(show_text, columns, width_scale)
return (show_text, columns, width_scale)
def selected(src):
"""Item is selected"""
if Is.object(src):
if hasattr(src, 'select'): # 2.7
return src.select
elif hasattr(src, 'select_get'): # 2.8
return src.select_get()
else:
assert None, ('Object missing attribute to find selection')
elif Is.posebone(src):
return getattr(src.bone, 'select', None)
elif Is.bone(src) or Is.editbone(src):
return src.select
elif src is None:
return
else:
assert None, ('Is.selected() has not accounted for this type', src)
def visible(context, src, viewport=False):
"""
Object not hidden and in a visible layer/collection\\
Bone not hidden and in a visible layer of it's armature
"""
if Is.object(src):
if viewport:
if viewport is True:
space = context.space_data
else:
space = in_viewport
if space.type == 'VIEW_3D':
return src.visible_in_viewport_get(space)
return src.visible_get()
if not (Is.in_scene(context, src) and Is.in_view(context, src)):
# Object not in active scene and/or deleted
return
vl = context.view_layer
if src in vl.objects.values():
res = src.visible_get(view_layer=vl)
return res
elif Is.posebone(src):
if src.bone.hide:
return False
else:
return bool(Get.visible_armature_layers(src, src.id_data.data))
elif Is.bone(src) or Is.editbone(src):
if src.hide:
return False
else:
return bool(Get.visible_armature_layers(src, src.id_data))
elif Is.collection(src):
if src.hide_viewport:
return False
else:
return Is.in_view(context, src)
else:
assert None, ("Is.visible(), has not accounted for this type",
src, type(src)
)
# endregion
# -------------------------------------------------------------------------
Is = type('', (), dict(**globals(), bool=_bool, float=_float, int=_int))