forked from sambler/myblendercontrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
3dview_synchronize_views.py
203 lines (157 loc) · 6.1 KB
/
3dview_synchronize_views.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
# ##### BEGIN GPL LICENSE BLOCK #####
#
# 3dview_synchronize_views.py
# 3D Views synchronizator
# Copyright (C) 2015 Quentin Wenger
#
# 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 #####
bl_info = {"name": "Synchronize 3D Views",
"description": "Make orientations of 3D Views dependent of each other",
"author": "Quentin Wenger (Matpi)",
"version": (1, 0),
"blender": (2, 74, 0),
"location": "3D View(s) -> Properties -> Views Syncing",
"warning": "",
"wiki_url": "",
"tracker_url": "",
"category": "3D View"
}
import bpy
def getCurrentWindowRegion(context):
for region in context.area.regions:
if region.type == 'WINDOW':
return region
"""
def getWindowRegions(context):
regions = []
for area in context.screen.areas:
if area.type == 'VIEW_3D':
for region in area.regions:
if region.type == 'WINDOW':
regions.append(region)
break
return regions
"""
def getWindowRegionsAndRegions3D(context):
regions = []
for area in context.screen.areas:
if area.type == 'VIEW_3D':
region = None
region_3d = None
for item in area.regions:
if item.type == 'WINDOW':
region = item
break
for item in area.spaces:
if item.type == 'VIEW_3D':
region_3d = item.region_3d
regions.append((region, region_3d))
return regions
def getWindowRegionsIDs(context):
ids = []
for area in context.screen.areas:
if area.type == 'VIEW_3D':
for region in area.regions:
if region.type == 'WINDOW':
ids.append(region.id)
break
return ids
def syncViews(dummy):
children_regions_3d = []
parent_region_3d = None
#for region in getWindowRegions(bpy.context):
data = getWindowRegionsAndRegions3D(bpy.context)
data_dict = dict((region.id, region_3d)
for region, region_3d in data)
for prop in bpy.context.screen.view_sync_coll:
region_3d = data_dict[prop.region_id]
if prop.items == 'RECEIVE':
children_regions_3d.append(region_3d)
elif prop.items == 'SEND':
parent_region_3d = region_3d
if parent_region_3d is not None:
for region_3d in children_regions_3d:
region_3d.view_matrix = parent_region_3d.view_matrix
def updateSyncingStatus(self, context):
# check for outdated region props and remove them
ids = getWindowRegionsIDs(context)
for prop in context.screen.view_sync_coll:
if prop.region_id not in ids:
context.screen.view_sync_coll.remove(prop)
if self.items == 'SEND':
# deactivate all other senders
for prop in context.screen.view_sync_coll:
if prop.region_id != self.region_id:
if prop.items == 'SEND':
prop.items = 'NONE'
# launch sync if needed
if not syncViews in bpy.app.handlers.scene_update_pre:
bpy.app.handlers.scene_update_pre.append(syncViews)
else:
# check for senders; if none found, stop sync
# this is maybe not needed,
# but could help a bit keeping good perfs
for prop in context.screen.view_sync_coll:
if prop.items == 'SEND':
break
else:
if syncViews in bpy.app.handlers.scene_update_pre:
bpy.app.handlers.scene_update_pre.remove(syncViews)
class ViewSyncingCollectionGroup(bpy.types.PropertyGroup):
items = bpy.props.EnumProperty(
items=[('NONE', "None", "No sync"),
('SEND', "Send", "Act as parent"),
('RECEIVE', "Receive", "Act as child")],
default='NONE',
update=updateSyncingStatus)
region_id = bpy.props.IntProperty()
class ViewSyncingPanel(bpy.types.Panel):
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_label = "Views Syncing"
def draw(self, context):
layout = self.layout
layout.label(text="Views Syncing:")
region_id = getCurrentWindowRegion(context).id
index = None
for i, item in enumerate(context.screen.view_sync_coll):
if item.region_id == region_id:
index = i
layout.prop(
context.screen.view_sync_coll[index],
"items",
expand=True)
break
if index is None:
prop = context.screen.view_sync_coll.add()
prop.region_id = region_id
layout.prop(
prop,
"items",
expand=True)
def register():
bpy.utils.register_module(__name__)
bpy.types.Screen.view_sync_coll = bpy.props.CollectionProperty(
type=ViewSyncingCollectionGroup)
def unregister():
del bpy.types.Screen.view_sync_coll
# to be sure...
if syncViews in bpy.app.handlers.scene_update_pre:
bpy.app.handlers.scene_update_pre.remove(syncViews)
bpy.utils.unregister_module(__name__)
if __name__ == "__main__":
register()