Merge branch 'master' into blender2.8
[blender-addons.git] / space_view3d_display_tools / modifier_tools.py
blobaf2667dc2a509a641a0eec5665110354b454578d
1 # space_view_3d_display_tools.py Copyright (C) 2014, Jordi Vall-llovera
2 # Multiple display tools for fast navigate/interact with the viewport
4 # ***** BEGIN GPL LICENSE BLOCK *****
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software Foundation,
18 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 # ***** END GPL LICENCE BLOCK *****
22 """
23 Additional links:
24 Author Site: http://www.jordiart.com
25 """
27 import bpy
28 from bpy.types import Operator
29 from bpy.props import (
30 IntProperty,
31 BoolProperty,
35 # function taken from space_view3d_modifier_tools.py
36 class DisplayApplyModifiersView(Operator):
37 bl_idname = "view3d.toggle_apply_modifiers_view"
38 bl_label = "Hide Viewport"
39 bl_description = "Shows/Hide modifiers of the active / selected object(s) in 3d View"
41 @classmethod
42 def poll(cls, context):
43 return context.active_object is not None
45 def execute(self, context):
46 is_apply = True
47 message_a = ""
48 for mod in context.active_object.modifiers:
49 if mod.show_viewport:
50 is_apply = False
51 break
53 # active object - no selection
54 for mod in context.active_object.modifiers:
55 mod.show_viewport = is_apply
57 for obj in context.selected_objects:
58 for mod in obj.modifiers:
59 mod.show_viewport = is_apply
61 if is_apply:
62 message_a = "Displaying modifiers in the 3d View"
63 else:
64 message_a = "Hiding modifiers in the 3d View"
66 self.report(type={"INFO"}, message=message_a)
68 return {'FINISHED'}
71 # define base dummy class for inheritance
72 class BasePollCheck:
73 @classmethod
74 def poll(cls, context):
75 return True
78 # Set Render Settings
79 def set_render_settings(context):
80 scene = context.scene
81 render = scene.render
82 render.simplify_subdivision = 0
83 render.simplify_shadow_samples = 0
84 render.simplify_child_particles = 0
85 render.simplify_ao_sss = 0
88 # Display Modifiers Render Switch
89 class DisplayModifiersRenderSwitch(Operator, BasePollCheck):
90 bl_idname = "view3d.display_modifiers_render_switch"
91 bl_label = "On/Off"
92 bl_description = "Display/Hide modifiers on render"
94 mod_render = BoolProperty(default=True)
96 def execute(self, context):
97 try:
98 if self.mod_render:
99 scene = context.scene.display_tools
100 scene.Simplify = 1
102 selection = context.selected_objects
104 if not selection:
105 for obj in bpy.data.objects:
106 for mod in obj.modifiers:
107 mod.show_render = self.mod_render
108 else:
109 for obj in selection:
110 for mod in obj.modifiers:
111 mod.show_render = self.mod_render
112 except:
113 self.report({'ERROR'}, "Display/Hide all modifiers for render failed")
114 return {'CANCELLED'}
116 return {'FINISHED'}
119 # Display Modifiers Viewport switch
120 class DisplayModifiersViewportSwitch(Operator, BasePollCheck):
121 bl_idname = "view3d.display_modifiers_viewport_switch"
122 bl_label = "On/Off"
123 bl_description = "Display/Hide modifiers in the viewport"
125 mod_switch = BoolProperty(default=True)
127 def execute(self, context):
128 try:
129 selection = context.selected_objects
131 if not(selection):
132 for obj in bpy.data.objects:
133 for mod in obj.modifiers:
134 mod.show_viewport = self.mod_switch
135 else:
136 for obj in selection:
137 for mod in obj.modifiers:
138 mod.show_viewport = self.mod_switch
139 except:
140 self.report({'ERROR'}, "Display/Hide modifiers in the viewport failed")
141 return {'CANCELLED'}
143 return {'FINISHED'}
146 # Display Modifiers Edit Switch
147 class DisplayModifiersEditSwitch(Operator, BasePollCheck):
148 bl_idname = "view3d.display_modifiers_edit_switch"
149 bl_label = "On/Off"
150 bl_description = "Display/Hide modifiers during edit mode"
152 mod_edit = BoolProperty(default=True)
154 def execute(self, context):
155 try:
156 selection = context.selected_objects
158 if not(selection):
159 for obj in bpy.data.objects:
160 for mod in obj.modifiers:
161 mod.show_in_editmode = self.mod_edit
162 else:
163 for obj in selection:
164 for mod in obj.modifiers:
165 mod.show_in_editmode = self.mod_edit
166 except:
167 self.report({'ERROR'}, "Display/Hide all modifiers failed")
168 return {'CANCELLED'}
170 return {'FINISHED'}
173 class DisplayModifiersCageSet(Operator, BasePollCheck):
174 bl_idname = "view3d.display_modifiers_cage_set"
175 bl_label = "On/Off"
176 bl_description = "Display modifiers editing cage during edit mode"
178 set_cage = BoolProperty(default=True)
180 def execute(self, context):
181 selection = context.selected_objects
182 try:
183 if not selection:
184 for obj in bpy.data.objects:
185 for mod in obj.modifiers:
186 mod.show_on_cage = self.set_cage
187 else:
188 for obj in selection:
189 for mod in obj.modifiers:
190 mod.show_on_cage = self.set_cage
191 except:
192 self.report({'ERROR'}, "Setting Editing Cage all modifiers failed")
193 return {'CANCELLED'}
195 return {'FINISHED'}
198 class ModifiersSubsurfLevel_Set(Operator, BasePollCheck):
199 bl_idname = "view3d.modifiers_subsurf_level_set"
200 bl_label = "Set Subsurf level"
201 bl_description = "Change subsurf modifier level"
203 level = IntProperty(
204 name="Subsurf Level",
205 description="Change subsurf modifier level",
206 default=1,
207 min=0,
208 max=10,
209 soft_min=0,
210 soft_max=6
213 def execute(self, context):
214 selection = context.selected_objects
215 try:
216 if not selection:
217 for obj in bpy.data.objects:
218 context.scene.objects.active = obj
219 bpy.ops.object.modifier_add(type='SUBSURF')
220 value = 0
221 for mod in obj.modifiers:
222 if mod.type == 'SUBSURF':
223 value = value + 1
224 mod.levels = self.level
225 if value > 1:
226 bpy.ops.object.modifier_remove(modifier="Subsurf")
227 else:
228 for obj in selection:
229 bpy.ops.object.subdivision_set(level=self.level, relative=False)
230 for mod in obj.modifiers:
231 if mod.type == 'SUBSURF':
232 mod.levels = self.level
233 except:
234 self.report({'ERROR'}, "Setting the Subsurf level could not be applied")
235 return {'CANCELLED'}
237 return {'FINISHED'}
240 # Register
241 def register():
242 bpy.utils.register_module(__name__)
245 def unregister():
246 bpy.utils.unregister_module(__name__)
249 if __name__ == "__main__":
250 register()