forked from z64tools/io_export_objex_2.5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblender_version_compatibility.py
120 lines (106 loc) · 4.35 KB
/
blender_version_compatibility.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
# Copyright 2020-2021 Dragorn421, ElTipejoLoco
#
# This objex2 addon 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 objex2 addon 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 objex2 addon. If not, see <https://www.gnu.org/licenses/>.
import bpy
import inspect
import operator
from .logging_util import getLogger
# thanks to https://theduckcow.com/2019/update-addons-both-blender-28-and-27-support/
def get_preferences(context):
if hasattr(context, 'user_preferences'):
return context.user_preferences
else:
return context.preferences
def make_annotations(clazz):
"""Converts class fields to annotations if running with Blender 2.8"""
if bpy.app.version < (2, 80):
return clazz
elif bpy.app.version < (2, 93):
isPropertyDeferred = lambda v: (isinstance(v, tuple) and len(v) == 2 and inspect.isbuiltin(v[0]) and isinstance(v[1], dict))
else:
isPropertyDeferred = lambda v: isinstance(v, bpy.props._PropertyDeferred)
for cls in clazz.__mro__:
bl_props = {k: v for k, v in cls.__dict__.items() if isPropertyDeferred(v)}
if bl_props:
if '__annotations__' not in cls.__dict__:
setattr(cls, '__annotations__', {})
annotations = cls.__dict__['__annotations__']
for k, v in bl_props.items():
annotations[k] = v
delattr(cls, k)
return clazz
def matmul(a, b):
if bpy.app.version < (2, 80):
return a * b
else:
return operator.matmul(a, b) # a @ b
def get_active_object(context):
if not hasattr(context, 'view_layer'): # < 2.80
return context.scene.objects.active
else: # 2.80+
return context.view_layer.objects.active
def set_active_object(context, object):
if not hasattr(context, 'view_layer'): # < 2.80
context.scene.objects.active = object
else: # 2.80+
context.view_layer.objects.active = object
def get_object_select(object):
if hasattr(object, 'select'): # < 2.80
return object.select
else: # 2.80+
return object.select_get()
def set_object_select(object, select):
if hasattr(object, 'select'): # < 2.80
object.select = select
else: # 2.80+
object.select_set(select)
# when PointerProperty didn't support ID
no_ID_PointerProperty = bpy.app.version < (2, 79, 0)
def adapt_ID_PointerProperty(clazz):
log = getLogger('blender_version_compatibility')
if not no_ID_PointerProperty:
return
for attr in dir(clazz):
val = getattr(clazz, attr)
if not (isinstance(val, tuple) and len(val) >= 1):
continue
if val[0] is not bpy.props.PointerProperty:
continue
params = val[1]
type = params.get('type')
if not (type and bpy.types.ID in type.mro()):
continue
del params['type']
poll = params.get('poll')
if poll:
if type not in (bpy.types.Object,):
raise TypeError('unsupported ID type {!r}'.format(type))
log.debug('{!r} {!r} wrap poll which expects ID', clazz, attr)
del params['poll']
def poll_wrapper_with_update_factory():
_attr = attr
_poll = poll
_type = type
def poll_wrapper_with_update(self, context):
name = getattr(self, _attr)
if name:
if _type is bpy.types.Object:
id_data = bpy.data.objects.get(name)
if not _poll(self, id_data):
setattr(self, _attr, '')
return poll_wrapper_with_update
params['update'] = poll_wrapper_with_update_factory()
log.debug('PointerProperty -> StringProperty {!r} {!r}', clazz, attr)
setattr(clazz, attr, bpy.props.StringProperty(**params))
has_per_material_backface_culling = bpy.app.version >= (2, 80, 0)