~ubuntu-branches/ubuntu/trusty/blender/trusty

« back to all changes in this revision

Viewing changes to release/scripts/addons_contrib/space_view3d_game_props_visualiser.py

  • Committer: Package Import Robot
  • Author(s): Matteo F. Vescovi
  • Date: 2012-05-12 20:02:22 UTC
  • mfrom: (14.2.16 sid)
  • Revision ID: package-import@ubuntu.com-20120512200222-lznjs2cxzaq96wua
Tags: 2.63a-1
* New upstream bugfix release
  + debian/patches/: re-worked since source code changed

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
# <pep8 compliant>
20
 
 
21
 
 
22
 
bl_info = {
23
 
    'name': 'Game Property Visualiser',
24
 
    'author': 'Bartius Crouch/Vilem Novak',
25
 
    'version': (2,5),
26
 
    'blender': (2, 5, 3),
27
 
    'location': 'View3D > Properties panel > Display tab',
28
 
    'description': 'Display the game properties next to selected objects '\
29
 
        'in the 3d-view',
30
 
    'wiki_url': 'http://wiki.blender.org/index.php/Extensions:2.5/Py/Scripts/3D_interaction/Game_Property_Visualiser',
31
 
    'tracker_url': 'http://projects.blender.org/tracker/?func=detail&aid=22607&group_id=153&atid=468',
32
 
    'category': '3D View'}
33
 
 
34
 
"""
35
 
Displays game properties next to selected objects(under name)
36
 
 
37
 
How to use:
38
 
    just toggle the button 'Visualise game props' in the right side panel
39
 
"""
40
 
 
41
 
import bgl
42
 
import blf
43
 
import bpy
44
 
import mathutils
45
 
 
46
 
 
47
 
# calculate locations and store them as ID property in the mesh
48
 
def calc_callback(self, context):
49
 
    # polling
50
 
    if context.mode == 'EDIT_MESH':
51
 
        return
52
 
    
53
 
    # get screen information
54
 
    mid_x = context.region.width/2.0
55
 
    mid_y = context.region.height/2.0
56
 
    width = context.region.width
57
 
    height = context.region.height
58
 
    
59
 
    # get matrices
60
 
    view_mat = context.space_data.region_3d.perspective_matrix
61
 
 
62
 
    ob_mat = context.active_object.matrix_world
63
 
    total_mat = view_mat*ob_mat
64
 
    
65
 
    # calculate location info
66
 
    texts = []
67
 
    
68
 
    # uncomment 2 lines below, to enable live updating of the selection
69
 
    #ob=context.active_object
70
 
    for ob in context.selected_objects:
71
 
        locs = []
72
 
        ob_mat = ob.matrix_world
73
 
        total_mat = view_mat*ob_mat
74
 
 
75
 
        for p in ob.game.properties:
76
 
            d=0.0#{'data':p.name+':'+str(p.value)}
77
 
            # print (d)
78
 
            locs.append([ mathutils.Vector([0,0,0]).resize_4d()])
79
 
    
80
 
    
81
 
        for loc in locs:
82
 
    
83
 
            vec = loc[0]*total_mat # order is important
84
 
            # dehomogenise
85
 
            vec = mathutils.Vector((vec[0]/vec[3],vec[1]/vec[3],vec[2]/vec[3]))
86
 
            x = int(mid_x + vec[0]*width/2.0)
87
 
            y = int(mid_y + vec[1]*height/2.0)
88
 
            texts+=[x, y]
89
 
        
90
 
 
91
 
    # store as ID property in mesh
92
 
    #print (texts)
93
 
    context.scene['GamePropsVisualiser'] = texts
94
 
 
95
 
 
96
 
# draw in 3d-view
97
 
def draw_callback(self, context):
98
 
    # polling
99
 
    if context.mode == 'EDIT_MESH':
100
 
        return
101
 
    # retrieving ID property data
102
 
    try:
103
 
        #print(context.scene['GamePropsVisualiser'])
104
 
        texts = context.scene['GamePropsVisualiser']
105
 
        
106
 
    except:
107
 
        return
108
 
    if not texts:
109
 
        return
110
 
    
111
 
    # draw
112
 
    i=0
113
 
 
114
 
    blf.size(0, 12, 72)
115
 
   
116
 
        
117
 
    bgl.glColor3f(1.0,1.0,1.0)
118
 
    for ob in bpy.context.selected_objects:
119
 
        for pi,p in enumerate(ob.game.properties):
120
 
            blf.position(0, texts[i], texts[i+1]-(pi+1)*14, 0)
121
 
            if p.type=='FLOAT':
122
 
                t=p.name+':  '+ str('%g'% p.value)
123
 
            else:    
124
 
                t=p.name+':  '+ str(p.value)
125
 
            blf.draw(0, t)
126
 
            i+=2
127
 
 
128
 
 
129
 
# operator
130
 
class GamePropertyVisualiser(bpy.types.Operator):
131
 
    bl_idname = "view3d.game_props_visualiser"
132
 
    bl_label = "Game Properties Visualiser"
133
 
    bl_description = "Toggle the visualisation of game properties"
134
 
    
135
 
    @classmethod
136
 
    def poll(cls, context):
137
 
        return context.mode!='EDIT_MESH'
138
 
    
139
 
    def modal(self, context, event):
140
 
        context.area.tag_redraw()
141
 
 
142
 
        # removal of callbacks when operator is called again
143
 
        #print(context.scene.display_game_properties)
144
 
        if context.scene.display_game_properties == -1:
145
 
           # print('deinit2')
146
 
 
147
 
            context.scene.display_game_properties = 0
148
 
            context.region.callback_remove(self.handle1)
149
 
            context.region.callback_remove(self.handle2)
150
 
            context.scene.display_game_properties = 0
151
 
            
152
 
            return {'FINISHED'}
153
 
        
154
 
        return {'PASS_THROUGH'}
155
 
    
156
 
    def invoke(self, context, event):
157
 
        if context.area.type == 'VIEW_3D':
158
 
            print(context.scene.display_game_properties)
159
 
            if context.scene.display_game_properties == 0 or context.scene.display_game_properties == -1:
160
 
                print('init')
161
 
                # operator is called for the first time, start everything
162
 
                context.scene.display_game_properties = 1
163
 
                context.window_manager.modal_handler_add(self)
164
 
                self.handle1 = context.region.callback_add(calc_callback,
165
 
                    (self, context), 'POST_VIEW')
166
 
                self.handle2 = context.region.callback_add(draw_callback,
167
 
                    (self, context), 'POST_PIXEL')
168
 
                return {'RUNNING_MODAL'}
169
 
            else:
170
 
                # operator is called again, stop displaying
171
 
                context.scene.display_game_properties = -1
172
 
                #print(dir(self))
173
 
                #
174
 
                return {'RUNNING_MODAL'}
175
 
        else:
176
 
            self.report({'WARNING'}, "View3D not found, can't run operator")
177
 
            return {'CANCELLED'}
178
 
 
179
 
 
180
 
# defining the panel
181
 
def menu_func(self, context):
182
 
    col = self.layout.column(align=True)
183
 
    col.operator(GamePropertyVisualiser.bl_idname, text="Visualise game props")
184
 
    self.layout.separator()
185
 
 
186
 
 
187
 
def register():
188
 
    bpy.types.Scene.display_game_properties = bpy.props.IntProperty(name='Visualise Game Poperties')
189
 
    bpy.types.VIEW3D_PT_view3d_display.prepend(menu_func)
190
 
 
191
 
def unregister():
192
 
    del bpy.types.Scene.display_game_properties
193
 
    bpy.types.VIEW3D_PT_view3d_display.remove(menu_func)
194
 
 
195
 
if __name__ == "__main__":
196
 
    register()