minor code cleanup
[blender-addons-contrib.git] / object_powerlib.py
1 # ##### BEGIN GPL LICENSE BLOCK #####
2 #
3 #  This program is free software; you can redistribute it and/or
4 #  modify it under the terms of the GNU General Public License
5 #  as published by the Free Software Foundation; either version 2
6 #  of the License, or (at your option) any later version.
7 #
8 #  This program is distributed in the hope that it will be useful,
9 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 #  GNU General Public License for more details.
12 #
13 #  You should have received a copy of the GNU General Public License
14 #  along with this program; if not, write to the Free Software Foundation,
15 #  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 #
17 # ##### END GPL LICENSE BLOCK #####
18
19 bl_info = {
20     "name": "Powerlib",
21     "description": "Control panel for managing "
22     "groups contained in linked libraries",
23     "author": "Olivier Amrein, Francesco Siddi",
24     "version": (0, 5),
25     "blender": (2, 53, 0),
26     "location": "Properties Panel",
27     "warning": "",  # used for warning icon and text in addons panel
28     "wiki_url": "http://wiki.blender.org/index.php/Extensions:2.6/Py/Scripts/Object/PowerLib",
29     "tracker_url": "http://projects.blender.org/tracker/index.php?func=detail&aid=31475&group_id=153&atid=467",
30     "category": "3D View"}
31
32 import bpy
33 from bpy.props import (FloatProperty, BoolProperty, 
34 FloatVectorProperty, StringProperty, EnumProperty)
35
36 #  Generic function to toggle across 3 different model resolutions
37 def SetProxyResolution(elem,target_resolution):
38
39     obj = bpy.data.objects[elem.name]
40
41     try: 
42        dupgroup_name = obj.dupli_group.name
43     except: 
44         return
45     
46     root = dupgroup_name[:-3]
47     ext = dupgroup_name[-3:]
48     new_group = root + target_resolution
49
50     if ext in {'_hi', '_lo', '_me'}:
51         try: 
52             obj.dupli_group = bpy.data.groups[new_group]
53             #print("PowerLib: CHANGE " + str(elem) + " to " + new_group)
54         except:
55             print ("Group %s not found" % new_group.upper())
56             
57             
58 class PowerlibPanel(bpy.types.Panel):
59     bl_label = "Powerlib"
60     bl_idname = "SCENE_PT_powerlib"
61     bl_context = "scene"
62     bl_space_type = 'VIEW_3D'
63     bl_region_type = 'UI'
64
65     def draw(self, context):
66         layout = self.layout
67         object = bpy.context.active_object
68         scene = context.scene
69         active_subgroup = scene.ActiveSubgroup
70         
71         if len(active_subgroup) > 0:
72             ob = bpy.data.objects[active_subgroup]
73         else:
74             ob = bpy.context.active_object
75
76         if ob.dupli_type == 'GROUP':
77             group = ob.dupli_group
78             group_name = group.name  # set variable for group toggle
79             group_objs = bpy.data.groups[group.name].objects
80             total_groups = 0
81
82             row = layout.row()
83             row.label(" GROUP: " + group.name, icon = 'GROUP')
84             active_subgroup = scene.ActiveSubgroup
85             if len(active_subgroup) > 0:
86                 subgroup = row.operator("powerlib.display_subgroup_content",
87                 text="Back to subgroup", icon='BACK')
88                 subgroup.item_name = ''
89             
90             box = layout.box()         
91
92             for elem in group_objs:
93
94                 if elem.dupli_group != None:
95                     row = box.row()   
96                     col=row.row()
97                                      
98                     total_groups += 1
99                     
100                     if (elem.dupli_type == 'GROUP'):
101                         subgroup = col.operator("powerlib.toggle_subgroup",
102                         text="", icon='RESTRICT_VIEW_OFF', emboss=False)
103                         subgroup.display = "NONE"
104                         subgroup.item_name = elem.name
105                         subgroup.group_name = group.name
106                         col.label(elem.name)
107                     else:
108                         subgroup = col.operator("powerlib.toggle_subgroup",
109                         text="", icon='RESTRICT_VIEW_ON', emboss=False)
110                         subgroup.display = "GROUP"
111                         subgroup.item_name = elem.name
112                         subgroup.group_name = group.name
113                         col.label(elem.name)
114                         
115                     if len(bpy.data.groups[elem.dupli_group.name].objects.items()) > 1:
116                         subgroup = col.operator("powerlib.display_subgroup_content",
117                         text="Explore", icon='GROUP')
118                         subgroup.item_name = elem.name
119                     else:
120                         col.label(text="")
121                        
122                     resolution = str(elem.dupli_group.name)[-3:]
123                     if resolution in {'_hi', '_lo', '_me'}:
124                         res = resolution[-2:].upper()
125
126                         subgroup = col.operator("powerlib.toggle_subgroup_res",
127                         text=res, icon='FILE_REFRESH')
128                         subgroup.item_name = elem.name
129                         subgroup.group_name = group.name
130                     else:
131                         col.label(text="")
132                 else:
133                     pass   
134         
135             if total_groups == 0 :
136                 box.label(" No subgroups found in this group",icon="LAYER_USED")
137                 resolution = str(object.dupli_group.name)[-3:]
138                 if resolution in {'_hi', '_lo', '_me'}:
139
140                     res = resolution[-2:].upper()
141
142                     subgroup = box.operator("powerlib.toggle_subgroup_res",
143                     text=res, icon='FILE_REFRESH')
144                     subgroup.item_name = bpy.context.active_object.name
145                     subgroup.group_name = group.name
146             else:
147                 row = layout.row(align=True)
148                 row.label("Total groups: " + str(total_groups))
149                 box = layout.box()
150                 row = box.row(align=True)
151                 group = row.operator("powerlib.toggle_group",
152                 text="Show All", icon='RESTRICT_VIEW_OFF')
153                 group.display = "showall"
154                 group.group_name = group_name
155     
156                 group = row.operator("powerlib.toggle_group",
157                 text="Hide All", icon='RESTRICT_VIEW_ON')
158                 group.display = "hideall"
159                 group.group_name = group_name
160
161                 row = box.row()
162                 
163                 row.label(text="Set all subgroups to: ")
164
165                 row = box.row(align=True)
166
167                 group = row.operator("powerlib.toggle_group",
168                 text="Low", icon='MESH_CIRCLE')
169                 group.display = "low"
170                 group.group_name = group_name
171                 
172                 group = row.operator("powerlib.toggle_group",
173                 text="Medium", icon='MESH_UVSPHERE')
174                 group.display = "medium"
175                 group.group_name = group_name
176                 
177                 group = row.operator("powerlib.toggle_group",
178                 text="High", icon='MESH_ICOSPHERE')
179                 group.display = "high"
180                 group.group_name = group_name
181                         
182         else:
183             layout.label(" Select a group")            
184
185
186 class ToggleSubgroupResolution(bpy.types.Operator):
187     bl_idname = "powerlib.toggle_subgroup_res"
188     bl_label = "Powerlib Toggle Soubgroup Res"
189     bl_description = "Change the resolution of a subgroup"
190     item_name = bpy.props.StringProperty()
191     group_name = bpy.props.StringProperty()
192
193     def execute(self, context):
194
195         group_name = self.group_name
196         item_name = self.item_name
197
198         obj = bpy.data.objects[item_name]
199
200         dupgroup = obj.dupli_group
201         dupgroup_name = obj.dupli_group.name
202
203         root = dupgroup_name[:-2]
204         ext = dupgroup_name[-2:]
205         
206         if (root + 'me') in bpy.data.groups:
207             if ext == 'hi':
208                 new_group = root + "me"
209             elif ext == 'me':
210                 new_group = root + "lo"
211             elif ext == 'lo':
212                 new_group = root + "hi"
213             else:
214                 new_group = dupgroup  # if error, do not change dupligroup
215         else:
216             if ext == 'hi':
217                 new_group = root + "lo"
218             elif ext == 'lo':
219                 new_group = root + "hi"
220             else:
221                 new_group = dupgroup  # if error, do not change dupligroup
222
223         if bpy.data.groups[dupgroup_name].library:
224             # link needed object
225             filepath = bpy.data.groups[dupgroup_name].library.filepath
226
227             print(filepath)
228             with bpy.data.libraries.load(filepath, 
229             link=True) as (data_from, data_to):
230                 data_to.groups.append(new_group)
231
232         try: 
233             obj.dupli_group = bpy.data.groups[new_group]
234             print("PowerLib: CHANGE " + str(item_name) + " to " + new_group)
235         except:
236             self.report({'WARNING'}, "Group %s not found" % new_group.upper())
237
238         return {'FINISHED'}
239
240
241 class ToggleAllSubgroups(bpy.types.Operator):
242     bl_idname = "powerlib.toggle_group"
243     bl_label = "Powerlib Toggle Group"
244     bl_description = "Toggle a property for all subgroups"
245     display = bpy.props.StringProperty()
246     group_name = bpy.props.StringProperty()
247
248     def execute(self, context):
249
250         display = self.display
251         grp_name = self.group_name
252         group_objs = bpy.data.groups[grp_name].objects
253
254         for elem in group_objs:
255             if display == 'showall':
256                 elem.dupli_type = "GROUP"
257                 #print("Powerlib: SHOW " + elem.name)
258             elif display == 'hideall':
259                 elem.dupli_type = "NONE"
260                 #print("Powerlib: HIDE " + elem.name)
261             if display == 'low':
262                 #print("Powerlib: ALL LOW " + elem.name)
263                 SetProxyResolution(elem,'_lo')
264             elif display == 'medium':
265                 #print("Powerlib: ALL MEDIUM " + elem.name)
266                 SetProxyResolution(elem,'_me')
267             elif display == 'high':
268                 #print("Powerlib: ALL HIGH " + elem.name)
269                 SetProxyResolution(elem,'_hi')
270             else:
271                 print("nothing")
272
273         return {'FINISHED'}
274     
275     
276 class ToggleSubgroupDisplay(bpy.types.Operator):
277     bl_idname = "powerlib.toggle_subgroup"
278     bl_label = "Powelib Toggle Subgroup"
279     bl_description = "Toggle the display of a subgroup"
280     display = bpy.props.StringProperty()
281     item_name = bpy.props.StringProperty()
282     group_name = bpy.props.StringProperty()
283     
284     def execute(self, context):
285
286         display = self.display
287         obj_name = self.item_name
288         grp_name = self.group_name
289
290         print("Powerlib: " + obj_name + " is being set to " + display)
291
292         bpy.data.groups[grp_name].objects[obj_name].dupli_type = display
293         return {'FINISHED'}
294     
295     
296 class DisplaySubgroupContent(bpy.types.Operator):
297     bl_idname = "powerlib.display_subgroup_content"
298     bl_label = "Powerlib Display Subgroup Content"
299     bl_description = "Display the content of a subgroup"
300
301     item_name = bpy.props.StringProperty()
302     
303     def execute(self, context):
304         scene = context.scene
305         scene.ActiveSubgroup = self.item_name
306         return {'FINISHED'}
307     
308
309 def register():
310     bpy.types.Scene.ActiveSubgroup = StringProperty(
311             name="Commit untracked",
312             default="",
313             description="Add untracked files into svn and commit all of them")
314     bpy.utils.register_class(DisplaySubgroupContent)
315     bpy.utils.register_class(ToggleSubgroupResolution)
316     bpy.utils.register_class(ToggleAllSubgroups)
317     bpy.utils.register_class(ToggleSubgroupDisplay)
318     bpy.utils.register_class(PowerlibPanel)
319     
320 def unregister():
321     del bpy.types.Scene.ActiveSubgroup
322     bpy.utils.unregister_class(DisplaySubgroupContent)
323     bpy.utils.unregister_class(ToggleSubgroupResolution)
324     bpy.utils.unregister_class(ToggleAllSubgroups)
325     bpy.utils.unregister_class(ToggleSubgroupDisplay)
326     bpy.utils.unregister_class(PowerlibPanel)
327     
328 if __name__ == "__main__":
329     register()