Welcome to mirror list, hosted at ThFree Co, Russian Federation.

display.py « space_view3d_display_tools - git.blender.org/blender-addons.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d0310ee87eb229c89eaa29c193d959ba8384c0c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# space_view_3d_display_tools.py Copyright (C) 2014, Jordi Vall-llovera
# Multiple display tools for fast navigate/interact with the viewport
# wire tools by Lapineige

# ***** BEGIN GPL LICENSE BLOCK *****
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ***** END GPL LICENCE BLOCK *****


import bpy
from bpy.types import Operator
from bpy.props import (
        BoolProperty,
        EnumProperty,
        )


# define base dummy class for inheritance
class BasePollCheck:
    @classmethod
    def poll(cls, context):
        return True


class View3D_AF_Wire_All(Operator):
    bl_idname = "af_ops.wire_all"
    bl_label = "Wire on All Objects"
    bl_description = "Toggle Wire on all objects in the scene"

    @classmethod
    def poll(cls, context):
        return (context.active_object is not None and
                not context.scene.display_tools.WT_handler_enable)

    def execute(self, context):

        for obj in bpy.data.objects:
            if obj.show_wire:
                obj.show_wire = False
            else:
                obj.show_wire = True

        return {'FINISHED'}


# Change draw type
class DisplayDrawChange(Operator, BasePollCheck):
    bl_idname = "view3d.display_draw_change"
    bl_label = "Draw Type"
    bl_description = "Change Display objects' mode"

    drawing = EnumProperty(
            items=[('TEXTURED', 'Texture', 'Texture display mode'),
                   ('SOLID', 'Solid', 'Solid display mode'),
                   ('WIRE', 'Wire', 'Wire display mode'),
                   ('BOUNDS', 'Bounds', 'Bounds display mode'),
                   ],
            name="Draw Type",
            default='SOLID'
            )

    def execute(self, context):
        try:
            view = context.space_data
            view.viewport_shade = 'TEXTURED'
            context.scene.game_settings.material_mode = 'GLSL'
            selection = context.selected_objects

            if not selection:
                for obj in bpy.data.objects:
                    obj.draw_type = self.drawing
            else:
                for obj in selection:
                    obj.draw_type = self.drawing
        except:
            self.report({'ERROR'}, "Setting Draw Type could not be applied")
            return {'CANCELLED'}

        return {'FINISHED'}


# Bounds switch
class DisplayBoundsSwitch(Operator, BasePollCheck):
    bl_idname = "view3d.display_bounds_switch"
    bl_label = "On/Off"
    bl_description = "Display/Hide Bounding box overlay"

    bounds = BoolProperty(default=False)

    def execute(self, context):
        try:
            scene = context.scene.display_tools
            selection = context.selected_objects

            if not selection:
                for obj in bpy.data.objects:
                    obj.show_bounds = self.bounds
                    if self.bounds:
                        obj.draw_bounds_type = scene.BoundingMode
            else:
                for obj in selection:
                    obj.show_bounds = self.bounds
                    if self.bounds:
                        obj.draw_bounds_type = scene.BoundingMode
        except:
            self.report({'ERROR'}, "Display/Hide Bounding box overlay failed")
            return {'CANCELLED'}

        return {'FINISHED'}


# Double Sided switch
class DisplayDoubleSidedSwitch(Operator, BasePollCheck):
    bl_idname = "view3d.display_double_sided_switch"
    bl_label = "On/Off"
    bl_description = "Turn on/off face double shaded mode"

    double_side = BoolProperty(default=False)

    def execute(self, context):
        try:
            selection = bpy.context.selected_objects

            if not selection:
                for mesh in bpy.data.meshes:
                    mesh.show_double_sided = self.double_side
            else:
                for sel in selection:
                    if sel.type == 'MESH':
                        mesh = sel.data
                        mesh.show_double_sided = self.double_side
        except:
            self.report({'ERROR'}, "Turn on/off face double shaded mode failed")
            return {'CANCELLED'}

        return {'FINISHED'}


# XRay switch
class DisplayXRayOn(Operator, BasePollCheck):
    bl_idname = "view3d.display_x_ray_switch"
    bl_label = "On"
    bl_description = "X-Ray display on/off"

    xrays = BoolProperty(default=False)

    def execute(self, context):
        try:
            selection = context.selected_objects

            if not selection:
                for obj in bpy.data.objects:
                    obj.show_x_ray = self.xrays
            else:
                for obj in selection:
                    obj.show_x_ray = self.xrays
        except:
            self.report({'ERROR'}, "Turn on/off X-ray mode failed")
            return {'CANCELLED'}

        return {'FINISHED'}


# wire tools by Lapineige
class WT_HideAllWire(Operator):
    bl_idname = "object.wt_hide_all_wire"
    bl_label = "Hide Wire And Edges"
    bl_description = "Hide All Objects' wire and edges"

    @classmethod
    def poll(cls, context):
        return not context.scene.display_tools.WT_handler_enable

    def execute(self, context):
        for obj in bpy.data.objects:
            if hasattr(obj, "show_wire"):
                obj.show_wire, obj.show_all_edges = False, False
        return {'FINISHED'}


class WT_SelectionHandlerToggle(Operator):
    bl_idname = "object.wt_selection_handler_toggle"
    bl_label = "Wire Selection (auto)"
    bl_description = "Display the wire of the selection, auto update when selecting another object"
    bl_options = {'INTERNAL'}

    def execute(self, context):
        display_tools = context.scene.display_tools
        if display_tools.WT_handler_enable:
            try:
                bpy.app.handlers.scene_update_post.remove(wire_on_selection_handler)
            except:
                self.report({'INFO'},
                            "Wire Selection: auto mode exit seems to have failed. If True, reload the file")

            display_tools.WT_handler_enable = False
            if hasattr(context.object, "show_wire"):
                context.object.show_wire, context.object.show_all_edges = False, False
        else:
            bpy.app.handlers.scene_update_post.append(wire_on_selection_handler)
            display_tools.WT_handler_enable = True
            if hasattr(context.object, "show_wire"):
                context.object.show_wire, context.object.show_all_edges = True, True
        return {'FINISHED'}


# handler
def wire_on_selection_handler(scene):
    obj = bpy.context.object

    if not scene.display_tools.WT_handler_previous_object:
        if hasattr(obj, "show_wire"):
            obj.show_wire, obj.show_all_edges = True, True
            scene.display_tools.WT_handler_previous_object = obj.name
    else:
        if scene.display_tools.WT_handler_previous_object != obj.name:
            previous_obj = bpy.data.objects[scene.display_tools.WT_handler_previous_object]
            if hasattr(previous_obj, "show_wire"):
                previous_obj.show_wire, previous_obj.show_all_edges = False, False

            scene.display_tools.WT_handler_previous_object = obj.name

            if hasattr(obj, "show_wire"):
                obj.show_wire, obj.show_all_edges = True, True


# Register
def register():
    bpy.utils.register_module(__name__)


def unregister():
    bpy.utils.unregister_module(__name__)


if __name__ == "__main__":
    register()