uv_texture_atlas/bake to vcols: moved to contrib: update class names: T63750
[blender-addons-contrib.git] / amaranth / misc / toggle_wire.py
1 #  This program is free software; you can redistribute it and/or
2 #  modify it under the terms of the GNU General Public License
3 #  as published by the Free Software Foundation; either version 2
4 #  of the License, or (at your option) any later version.
5 #
6 #  This program is distributed in the hope that it will be useful,
7 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
8 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9 #  GNU General Public License for more details.
10 #
11 #  You should have received a copy of the GNU General Public License
12 #  along with this program; if not, write to the Free Software Foundation,
13 #  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
14
15 import bpy
16
17
18 # FEATURE: Toggle Wire Display
19 class AMTH_OBJECT_OT_wire_toggle(bpy.types.Operator):
20
21     """Turn on/off wire display on mesh objects"""
22     bl_idname = "object.amth_wire_toggle"
23     bl_label = "Display Wireframe"
24     bl_options = {"REGISTER", "UNDO"}
25
26     clear: bpy.props.BoolProperty(
27         default=False, name="Clear Wireframe",
28         description="Clear Wireframe Display")
29
30     def execute(self, context):
31
32         scene = context.scene
33         is_all_scenes = scene.amth_wire_toggle_scene_all
34         is_selected = scene.amth_wire_toggle_is_selected
35         is_all_edges = scene.amth_wire_toggle_edges_all
36         is_optimal = scene.amth_wire_toggle_optimal
37         clear = self.clear
38
39         if is_all_scenes:
40             which = bpy.data.objects
41         elif is_selected:
42             if not context.selected_objects:
43                 self.report({"INFO"}, "No selected objects")
44             which = context.selected_objects
45         else:
46             which = scene.objects
47
48         if which:
49             for ob in which:
50                 if ob and ob.type in {
51                         "MESH", "EMPTY", "CURVE",
52                         "META", "SURFACE", "FONT"}:
53
54                     ob.show_wire = False if clear else True
55                     ob.show_all_edges = is_all_edges
56
57                     for mo in ob.modifiers:
58                         if mo and mo.type == "SUBSURF":
59                             mo.show_only_control_edges = is_optimal
60
61         return {"FINISHED"}
62
63
64 def ui_object_wire_toggle(self, context):
65
66     scene = context.scene
67
68     self.layout.separator()
69     col = self.layout.column(align=True)
70     col.label(text="Wireframes:")
71     row = col.row(align=True)
72     row.operator(AMTH_OBJECT_OT_wire_toggle.bl_idname,
73                  icon="MOD_WIREFRAME", text="Display").clear = False
74     row.operator(AMTH_OBJECT_OT_wire_toggle.bl_idname,
75                  icon="X", text="Clear").clear = True
76     col.separator()
77     row = col.row(align=True)
78     row.prop(scene, "amth_wire_toggle_edges_all")
79     row.prop(scene, "amth_wire_toggle_optimal")
80     row = col.row(align=True)
81     sub = row.row(align=True)
82     sub.active = not scene.amth_wire_toggle_scene_all
83     sub.prop(scene, "amth_wire_toggle_is_selected")
84     sub = row.row(align=True)
85     sub.active = not scene.amth_wire_toggle_is_selected
86     sub.prop(scene, "amth_wire_toggle_scene_all")
87
88
89 def init_properties():
90     scene = bpy.types.Scene
91     scene.amth_wire_toggle_scene_all = bpy.props.BoolProperty(
92         default=False,
93         name="All Scenes",
94         description="Toggle wire on objects in all scenes")
95     scene.amth_wire_toggle_is_selected = bpy.props.BoolProperty(
96         default=False,
97         name="Only Selected Objects",
98         description="Only toggle wire on selected objects")
99     scene.amth_wire_toggle_edges_all = bpy.props.BoolProperty(
100         default=True,
101         name="Draw All Edges",
102         description="Draw all the edges even on coplanar faces")
103     scene.amth_wire_toggle_optimal = bpy.props.BoolProperty(
104         default=False,
105         name="Subsurf Optimal Display",
106         description="Skip drawing/rendering of interior subdivided edges "
107                     "on meshes with Subdivision Surface modifier")
108
109
110 def clear_properties():
111     props = (
112         'amth_wire_toggle_is_selected',
113         'amth_wire_toggle_scene_all',
114         "amth_wire_toggle_edges_all",
115         "amth_wire_toggle_optimal"
116     )
117     wm = bpy.context.window_manager
118     for p in props:
119         if p in wm:
120             del wm[p]
121
122 # //FEATURE: Toggle Wire Display
123
124
125 def register():
126     init_properties()
127     bpy.utils.register_class(AMTH_OBJECT_OT_wire_toggle)
128     bpy.types.VIEW3D_PT_view3d_properties.append(ui_object_wire_toggle)
129
130
131 def unregister():
132     bpy.utils.unregister_class(AMTH_OBJECT_OT_wire_toggle)
133     bpy.types.VIEW3D_PT_view3d_properties.remove(ui_object_wire_toggle)
134     clear_properties()