Merge branch 'master' into blender2.8
[blender-addons.git] / space_view3d_display_tools / display.py
blob6c1c67965f46ce9c67d16197644fc7a8737e8bff
1 # space_view_3d_display_tools.py Copyright (C) 2014, Jordi Vall-llovera
2 # Multiple display tools for fast navigate/interact with the viewport
3 # wire tools by Lapineige
5 # ***** BEGIN GPL LICENSE BLOCK *****
7 # This program is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU General Public License
9 # as published by the Free Software Foundation; either version 2
10 # of the License, or (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software Foundation,
19 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 # ***** END GPL LICENCE BLOCK *****
24 import bpy
25 from bpy.types import Operator
26 from bpy.props import (
27 BoolProperty,
28 EnumProperty,
32 # define base dummy class for inheritance
33 class BasePollCheck:
34 @classmethod
35 def poll(cls, context):
36 return True
39 class View3D_AF_Wire_All(Operator):
40 bl_idname = "af_ops.wire_all"
41 bl_label = "Wire on All Objects"
42 bl_description = "Toggle Wire on all objects in the scene"
44 @classmethod
45 def poll(cls, context):
46 return (context.active_object is not None and
47 not context.scene.display_tools.WT_handler_enable)
49 def execute(self, context):
51 for obj in bpy.data.objects:
52 if obj.show_wire:
53 obj.show_wire = False
54 else:
55 obj.show_wire = True
57 return {'FINISHED'}
60 # Change draw type
61 class DisplayDrawChange(Operator, BasePollCheck):
62 bl_idname = "view3d.display_draw_change"
63 bl_label = "Draw Type"
64 bl_description = "Change Display objects' mode"
66 drawing = EnumProperty(
67 items=[('TEXTURED', 'Texture', 'Texture display mode'),
68 ('SOLID', 'Solid', 'Solid display mode'),
69 ('WIRE', 'Wire', 'Wire display mode'),
70 ('BOUNDS', 'Bounds', 'Bounds display mode'),
72 name="Draw Type",
73 default='SOLID'
76 def execute(self, context):
77 try:
78 view = context.space_data
79 view.viewport_shade = 'TEXTURED'
80 context.scene.game_settings.material_mode = 'GLSL'
81 selection = context.selected_objects
83 if not selection:
84 for obj in bpy.data.objects:
85 obj.display_type = self.drawing
86 else:
87 for obj in selection:
88 obj.display_type = self.drawing
89 except:
90 self.report({'ERROR'}, "Setting Draw Type could not be applied")
91 return {'CANCELLED'}
93 return {'FINISHED'}
96 # Bounds switch
97 class DisplayBoundsSwitch(Operator, BasePollCheck):
98 bl_idname = "view3d.display_bounds_switch"
99 bl_label = "On/Off"
100 bl_description = "Display/Hide Bounding box overlay"
102 bounds = BoolProperty(default=False)
104 def execute(self, context):
105 try:
106 scene = context.scene.display_tools
107 selection = context.selected_objects
109 if not selection:
110 for obj in bpy.data.objects:
111 obj.show_bounds = self.bounds
112 if self.bounds:
113 obj.display_bounds_type = scene.BoundingMode
114 else:
115 for obj in selection:
116 obj.show_bounds = self.bounds
117 if self.bounds:
118 obj.display_bounds_type = scene.BoundingMode
119 except:
120 self.report({'ERROR'}, "Display/Hide Bounding box overlay failed")
121 return {'CANCELLED'}
123 return {'FINISHED'}
126 # Double Sided switch
127 class DisplayDoubleSidedSwitch(Operator, BasePollCheck):
128 bl_idname = "view3d.display_double_sided_switch"
129 bl_label = "On/Off"
130 bl_description = "Turn on/off face double shaded mode"
132 double_side = BoolProperty(default=False)
134 def execute(self, context):
135 try:
136 selection = bpy.context.selected_objects
138 if not selection:
139 for mesh in bpy.data.meshes:
140 mesh.show_double_sided = self.double_side
141 else:
142 for sel in selection:
143 if sel.type == 'MESH':
144 mesh = sel.data
145 mesh.show_double_sided = self.double_side
146 except:
147 self.report({'ERROR'}, "Turn on/off face double shaded mode failed")
148 return {'CANCELLED'}
150 return {'FINISHED'}
153 # XRay switch
154 class DisplayXRayOn(Operator, BasePollCheck):
155 bl_idname = "view3d.display_x_ray_switch"
156 bl_label = "On"
157 bl_description = "X-Ray display on/off"
159 xrays = BoolProperty(default=False)
161 def execute(self, context):
162 try:
163 selection = context.selected_objects
165 if not selection:
166 for obj in bpy.data.objects:
167 obj.show_in_front = self.xrays
168 else:
169 for obj in selection:
170 obj.show_in_front = self.xrays
171 except:
172 self.report({'ERROR'}, "Turn on/off X-ray mode failed")
173 return {'CANCELLED'}
175 return {'FINISHED'}
178 # wire tools by Lapineige
179 class WT_HideAllWire(Operator):
180 bl_idname = "object.wt_hide_all_wire"
181 bl_label = "Hide Wire And Edges"
182 bl_description = "Hide All Objects' wire and edges"
184 @classmethod
185 def poll(cls, context):
186 return not context.scene.display_tools.WT_handler_enable
188 def execute(self, context):
189 for obj in bpy.data.objects:
190 if hasattr(obj, "show_wire"):
191 obj.show_wire, obj.show_all_edges = False, False
192 return {'FINISHED'}
195 class WT_SelectionHandlerToggle(Operator):
196 bl_idname = "object.wt_selection_handler_toggle"
197 bl_label = "Wire Selection (auto)"
198 bl_description = "Display the wire of the selection, auto update when selecting another object"
199 bl_options = {'INTERNAL'}
201 def execute(self, context):
202 display_tools = context.scene.display_tools
203 if display_tools.WT_handler_enable:
204 try:
205 bpy.app.handlers.scene_update_post.remove(wire_on_selection_handler)
206 except:
207 self.report({'INFO'},
208 "Wire Selection: auto mode exit seems to have failed. If True, reload the file")
210 display_tools.WT_handler_enable = False
211 if hasattr(context.object, "show_wire"):
212 context.object.show_wire, context.object.show_all_edges = False, False
213 else:
214 bpy.app.handlers.scene_update_post.append(wire_on_selection_handler)
215 display_tools.WT_handler_enable = True
216 if hasattr(context.object, "show_wire"):
217 context.object.show_wire, context.object.show_all_edges = True, True
218 return {'FINISHED'}
221 # handler
222 def wire_on_selection_handler(scene):
223 obj = bpy.context.object
225 if not scene.display_tools.WT_handler_previous_object:
226 if hasattr(obj, "show_wire"):
227 obj.show_wire, obj.show_all_edges = True, True
228 scene.display_tools.WT_handler_previous_object = obj.name
229 else:
230 if scene.display_tools.WT_handler_previous_object != obj.name:
231 previous_obj = bpy.data.objects[scene.display_tools.WT_handler_previous_object]
232 if hasattr(previous_obj, "show_wire"):
233 previous_obj.show_wire, previous_obj.show_all_edges = False, False
235 scene.display_tools.WT_handler_previous_object = obj.name
237 if hasattr(obj, "show_wire"):
238 obj.show_wire, obj.show_all_edges = True, True
241 # Register
242 def register():
243 bpy.utils.register_module(__name__)
246 def unregister():
247 bpy.utils.unregister_module(__name__)
250 if __name__ == "__main__":
251 register()