Use URL icon, add Tip: prefix, increase lower margin
[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": "https://developer.blender.org/T31475",
30     "category": "3D View"}
31
32
33 import bpy
34 from bpy.props import (FloatProperty, BoolProperty,
35 FloatVectorProperty, StringProperty, EnumProperty)
36
37 #  Generic function to toggle across 3 different model resolutions
38 def SetProxyResolution(elem,target_resolution):
39
40     obj = bpy.data.objects[elem.name]
41
42     try:
43        dupgroup_name = obj.dupli_group.name
44     except:
45         return
46
47     root = dupgroup_name[:-3]
48     ext = dupgroup_name[-3:]
49     new_group = root + target_resolution
50
51     if ext in {'_hi', '_lo', '_me'}:
52         try:
53             obj.dupli_group = bpy.data.groups[new_group]
54             #print("PowerLib: CHANGE " + str(elem) + " to " + new_group)
55         except:
56             print ("Group %s not found" % new_group.upper())
57
58
59 class PowerlibPanel(bpy.types.Panel):
60     bl_label = "Powerlib"
61     bl_idname = "SCENE_PT_powerlib"
62     bl_context = "scene"
63     bl_space_type = 'VIEW_3D'
64     bl_region_type = 'UI'
65
66     def draw(self, context):
67         layout = self.layout
68         object = bpy.context.active_object
69         scene = context.scene
70         active_subgroup = scene.ActiveSubgroup
71
72         if len(active_subgroup) > 0:
73             ob = bpy.data.objects[active_subgroup]
74         else:
75             ob = bpy.context.active_object
76
77         if ob.dupli_type == 'GROUP':
78             group = ob.dupli_group
79             group_name = group.name  # set variable for group toggle
80             group_objs = bpy.data.groups[group.name].objects
81             total_groups = 0
82
83             row = layout.row()
84             row.label(" GROUP: " + group.name, icon = 'GROUP')
85             active_subgroup = scene.ActiveSubgroup
86             if len(active_subgroup) > 0:
87                 subgroup = row.operator("powerlib.display_subgroup_content",
88                 text="Back to subgroup", icon='BACK')
89                 subgroup.item_name = ''
90
91             box = layout.box()
92
93             for elem in group_objs:
94
95                 if elem.dupli_group != None:
96                     row = box.row()
97                     col=row.row()
98
99                     total_groups += 1
100
101                     if (elem.dupli_type == 'GROUP'):
102                         subgroup = col.operator("powerlib.toggle_subgroup",
103                         text="", icon='RESTRICT_VIEW_OFF', emboss=False)
104                         subgroup.display = "NONE"
105                         subgroup.item_name = elem.name
106                         subgroup.group_name = group.name
107                         col.label(elem.name)
108                     else:
109                         subgroup = col.operator("powerlib.toggle_subgroup",
110                         text="", icon='RESTRICT_VIEW_ON', emboss=False)
111                         subgroup.display = "GROUP"
112                         subgroup.item_name = elem.name
113                         subgroup.group_name = group.name
114                         col.label(elem.name)
115
116                     if len(bpy.data.groups[elem.dupli_group.name].objects.items()) > 1:
117                         subgroup = col.operator("powerlib.display_subgroup_content",
118                         text="Explore", icon='GROUP')
119                         subgroup.item_name = elem.name
120                     else:
121                         col.label(text="")
122
123                     resolution = str(elem.dupli_group.name)[-3:]
124                     if resolution in {'_hi', '_lo', '_me'}:
125                         res = resolution[-2:].upper()
126
127                         subgroup = col.operator("powerlib.toggle_subgroup_res",
128                         text=res, icon='FILE_REFRESH')
129                         subgroup.item_name = elem.name
130                         subgroup.group_name = group.name
131                     else:
132                         col.label(text="")
133                 else:
134                     pass
135
136             if total_groups == 0 :
137                 box.label(" No subgroups found in this group",icon="LAYER_USED")
138                 resolution = str(object.dupli_group.name)[-3:]
139                 if resolution in {'_hi', '_lo', '_me'}:
140
141                     res = resolution[-2:].upper()
142
143                     subgroup = box.operator("powerlib.toggle_subgroup_res",
144                     text=res, icon='FILE_REFRESH')
145                     subgroup.item_name = bpy.context.active_object.name
146                     subgroup.group_name = group.name
147             else:
148                 row = layout.row(align=True)
149                 row.label("Total groups: " + str(total_groups))
150                 box = layout.box()
151                 row = box.row(align=True)
152                 group = row.operator("powerlib.toggle_group",
153                 text="Show All", icon='RESTRICT_VIEW_OFF')
154                 group.display = "showall"
155                 group.group_name = group_name
156
157                 group = row.operator("powerlib.toggle_group",
158                 text="Hide All", icon='RESTRICT_VIEW_ON')
159                 group.display = "hideall"
160                 group.group_name = group_name
161
162                 row = box.row()
163
164                 row.label(text="Set all subgroups to: ")
165
166                 row = box.row(align=True)
167
168                 group = row.operator("powerlib.toggle_group",
169                 text="Low", icon='MESH_CIRCLE')
170                 group.display = "low"
171                 group.group_name = group_name
172
173                 group = row.operator("powerlib.toggle_group",
174                 text="Medium", icon='MESH_UVSPHERE')
175                 group.display = "medium"
176                 group.group_name = group_name
177
178                 group = row.operator("powerlib.toggle_group",
179                 text="High", icon='MESH_ICOSPHERE')
180                 group.display = "high"
181                 group.group_name = group_name
182
183         else:
184             layout.label(" Select a group")
185
186
187 class ToggleSubgroupResolution(bpy.types.Operator):
188     bl_idname = "powerlib.toggle_subgroup_res"
189     bl_label = "Powerlib Toggle Soubgroup Res"
190     bl_description = "Change the resolution of a subgroup"
191     item_name = bpy.props.StringProperty()
192     group_name = bpy.props.StringProperty()
193
194     def execute(self, context):
195
196         group_name = self.group_name
197         item_name = self.item_name
198
199         obj = bpy.data.objects[item_name]
200
201         dupgroup = obj.dupli_group
202         dupgroup_name = obj.dupli_group.name
203
204         root = dupgroup_name[:-2]
205         ext = dupgroup_name[-2:]
206
207         if (root + 'me') in bpy.data.groups:
208             if ext == 'hi':
209                 new_group = root + "me"
210             elif ext == 'me':
211                 new_group = root + "lo"
212             elif ext == 'lo':
213                 new_group = root + "hi"
214             else:
215                 new_group = dupgroup  # if error, do not change dupligroup
216         else:
217             if ext == 'hi':
218                 new_group = root + "lo"
219             elif ext == 'lo':
220                 new_group = root + "hi"
221             else:
222                 new_group = dupgroup  # if error, do not change dupligroup
223
224         if bpy.data.groups[dupgroup_name].library:
225             # link needed object
226             filepath = bpy.data.groups[dupgroup_name].library.filepath
227
228             print(filepath)
229             with bpy.data.libraries.load(filepath,
230             link=True) as (data_from, data_to):
231                 data_to.groups.append(new_group)
232
233         try:
234             obj.dupli_group = bpy.data.groups[new_group]
235             print("PowerLib: CHANGE " + str(item_name) + " to " + new_group)
236         except:
237             self.report({'WARNING'}, "Group %s not found" % new_group.upper())
238
239         return {'FINISHED'}
240
241
242 class ToggleAllSubgroups(bpy.types.Operator):
243     bl_idname = "powerlib.toggle_group"
244     bl_label = "Powerlib Toggle Group"
245     bl_description = "Toggle a property for all subgroups"
246     display = bpy.props.StringProperty()
247     group_name = bpy.props.StringProperty()
248
249     def execute(self, context):
250
251         display = self.display
252         grp_name = self.group_name
253         group_objs = bpy.data.groups[grp_name].objects
254
255         for elem in group_objs:
256             if display == 'showall':
257                 elem.dupli_type = "GROUP"
258                 #print("Powerlib: SHOW " + elem.name)
259             elif display == 'hideall':
260                 elem.dupli_type = "NONE"
261                 #print("Powerlib: HIDE " + elem.name)
262             if display == 'low':
263                 #print("Powerlib: ALL LOW " + elem.name)
264                 SetProxyResolution(elem,'_lo')
265             elif display == 'medium':
266                 #print("Powerlib: ALL MEDIUM " + elem.name)
267                 SetProxyResolution(elem,'_me')
268             elif display == 'high':
269                 #print("Powerlib: ALL HIGH " + elem.name)
270                 SetProxyResolution(elem,'_hi')
271             else:
272                 print("nothing")
273
274         return {'FINISHED'}
275
276
277 class ToggleSubgroupDisplay(bpy.types.Operator):
278     bl_idname = "powerlib.toggle_subgroup"
279     bl_label = "Powelib Toggle Subgroup"
280     bl_description = "Toggle the display of a subgroup"
281     display = bpy.props.StringProperty()
282     item_name = bpy.props.StringProperty()
283     group_name = bpy.props.StringProperty()
284
285     def execute(self, context):
286
287         display = self.display
288         obj_name = self.item_name
289         grp_name = self.group_name
290
291         print("Powerlib: " + obj_name + " is being set to " + display)
292
293         bpy.data.groups[grp_name].objects[obj_name].dupli_type = display
294         return {'FINISHED'}
295
296
297 class DisplaySubgroupContent(bpy.types.Operator):
298     bl_idname = "powerlib.display_subgroup_content"
299     bl_label = "Powerlib Display Subgroup Content"
300     bl_description = "Display the content of a subgroup"
301
302     item_name = bpy.props.StringProperty()
303
304     def execute(self, context):
305         scene = context.scene
306         scene.ActiveSubgroup = self.item_name
307         return {'FINISHED'}
308
309
310 def register():
311     bpy.types.Scene.ActiveSubgroup = StringProperty(
312             name="Commit untracked",
313             default="",
314             description="Add untracked files into svn and commit all of them")
315     bpy.utils.register_class(DisplaySubgroupContent)
316     bpy.utils.register_class(ToggleSubgroupResolution)
317     bpy.utils.register_class(ToggleAllSubgroups)
318     bpy.utils.register_class(ToggleSubgroupDisplay)
319     bpy.utils.register_class(PowerlibPanel)
320
321 def unregister():
322     del bpy.types.Scene.ActiveSubgroup
323     bpy.utils.unregister_class(DisplaySubgroupContent)
324     bpy.utils.unregister_class(ToggleSubgroupResolution)
325     bpy.utils.unregister_class(ToggleAllSubgroups)
326     bpy.utils.unregister_class(ToggleSubgroupDisplay)
327     bpy.utils.unregister_class(PowerlibPanel)
328
329 if __name__ == "__main__":
330     register()