-
Notifications
You must be signed in to change notification settings - Fork 50
/
archipack_animation.py
168 lines (142 loc) · 5.31 KB
/
archipack_animation.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
# -*- coding:utf-8 -*-
# ##### BEGIN GPL LICENSE BLOCK #####
#
# 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 2
# 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, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110- 1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
# <pep8 compliant>
# ----------------------------------------------------------
# Author: Stephen Leger (s-leger)
#
# ----------------------------------------------------------
import bpy
from bpy.app.handlers import persistent
from bpy.types import (
Object, Operator
)
from bpy.props import (
EnumProperty, BoolProperty
)
# Store animated objects for current session
# key: object name, value: datablock name
animated = {}
@persistent
def archipack_animation_onload(dummy):
"""
Fill in animated dict on file load
"""
global animated
for o in bpy.data.objects:
if "archipack_animation" in o and o.archipack_animation:
for key in o.data.keys():
if "archipack_" in key:
animated[o.name] = key
if len(animated) > 0:
if archipack_animation_updater not in bpy.app.handlers.frame_change_pre:
bpy.app.handlers.frame_change_pre.append(archipack_animation_updater)
@persistent
def archipack_animation_onunload(dummy):
"""
Cleanup animated dict on file unload
"""
global animated
animated.clear()
if archipack_animation_updater in bpy.app.handlers.frame_change_pre:
bpy.app.handlers.frame_change_pre.remove(archipack_animation_updater)
def archipack_animation_updater(dummy):
global animated
if len(animated) > 0:
context = bpy.context
scene = context.scene
act = scene.objects.active
sel = context.selected_objects[:]
for name in animated:
o = scene.objects.get(name)
if o and o.archipack_animation:
d = getattr(o.data, animated[name])[0]
o.select = True
scene.objects.active = o
d.update(context)
o.select = False
for o in sel:
o.select = True
scene.objects.active = act
class ARCHIPACK_OT_animation(Operator):
bl_idname = "archipack.animation"
bl_label = "Animation"
bl_description = "Manage animation support for object parameters (Add / Remove / Clear)"
bl_options = {'REGISTER', 'UNDO'}
mode = EnumProperty(
items=(
('ENABLE', 'Enable', 'Enable animation support for selected objects'),
('DISABLE', 'Disable', 'Disable animation support for selected objects'),
('CLEAR', 'Cleanup', 'Remove animation support for all objects')
)
)
@classmethod
def poll(cls, context):
return True
def enable_animation(self, sel):
"""
Add animation support on selected objects
"""
global animated
for o in sel:
if o.data:
for key in o.data.keys():
if "archipack_" in key:
d = getattr(o.data, key)[0]
if hasattr(d, "update"):
o.archipack_animation = True
animated[o.name] = key
if len(animated) > 0:
if archipack_animation_updater not in bpy.app.handlers.frame_change_pre:
bpy.app.handlers.frame_change_pre.append(archipack_animation_updater)
def disable_animation(self, sel):
"""
Remove animation support on selected objects
"""
global animated
for o in sel:
if "archipack_animation" in o:
o.archipack_animation = False
if o.name in animated:
del animated[o.name]
def execute(self, context):
if self.mode == 'ENABLE':
sel = context.selected_objects
self.enable_animation(sel)
elif self.mode == 'DISABLE':
sel = context.selected_objects
self.disable_animation(sel)
elif self.mode == 'CLEAR':
sel = context.scene.objects
self.disable_animation(sel)
archipack_animation_onunload(None)
return {'FINISHED'}
def register():
global animated
animated = {}
Object.archipack_animation = BoolProperty(name="Archipack animation", default=False)
bpy.app.handlers.load_pre.append(archipack_animation_onunload)
bpy.app.handlers.load_post.append(archipack_animation_onload)
bpy.utils.register_class(ARCHIPACK_OT_animation)
def unregister():
global animated
animated.clear()
bpy.app.handlers.load_pre.remove(archipack_animation_onunload)
bpy.app.handlers.load_post.remove(archipack_animation_onload)
bpy.utils.unregister_class(ARCHIPACK_OT_animation)
del Object.archipack_animation