1 # ##### BEGIN GPL LICENSE BLOCK #####
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.
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.
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.
17 # ##### END GPL LICENSE BLOCK #####
23 'name': 'Game Property Visualizer',
24 'author': 'Bartius Crouch/Vilem Novak',
27 'location': 'View3D > Properties panel > Display tab',
28 'description': 'Display the game properties next to selected objects '\
30 'warning': 'Script is returning errors',
31 'wiki_url': 'http://wiki.blender.org/index.php/Extensions:2.6/Py/Scripts/3D_interaction/Game_Property_Visualiser',
32 'tracker_url': 'http://projects.blender.org/tracker/?func=detail&aid=22607&group_id=153&atid=468',
33 'category': '3D View'}
36 Displays game properties next to selected objects(under name)
39 just toggle the button 'Visualize game props' in the right side panel
48 # calculate locations and store them as ID property in the mesh
49 def calc_callback(self, context):
51 if context.mode == 'EDIT_MESH':
54 # get screen information
55 mid_x = context.region.width/2.0
56 mid_y = context.region.height/2.0
57 width = context.region.width
58 height = context.region.height
61 view_mat = context.space_data.region_3d.perspective_matrix
63 ob_mat = context.active_object.matrix_world
64 total_mat = view_mat*ob_mat
66 # calculate location info
69 # uncomment 2 lines below, to enable live updating of the selection
70 #ob=context.active_object
71 for ob in context.selected_objects:
73 ob_mat = ob.matrix_world
74 total_mat = view_mat*ob_mat
76 for p in ob.game.properties:
77 # d = {'data':p.name+':'+str(p.value)}
79 locs.append([ mathutils.Vector([0,0,0]).resize_4d()])
84 vec = loc[0]*total_mat # order is important
86 vec = mathutils.Vector((vec[0]/vec[3],vec[1]/vec[3],vec[2]/vec[3]))
87 x = int(mid_x + vec[0]*width/2.0)
88 y = int(mid_y + vec[1]*height/2.0)
92 # store as ID property in mesh
94 context.scene['GamePropsVisualizer'] = texts
98 def draw_callback(self, context):
100 if context.mode == 'EDIT_MESH':
102 # retrieving ID property data
104 #print(context.scene['GamePropsVisualizer'])
105 texts = context.scene['GamePropsVisualizer']
118 bgl.glColor3f(1.0,1.0,1.0)
119 for ob in bpy.context.selected_objects:
120 for pi,p in enumerate(ob.game.properties):
121 blf.position(0, texts[i], texts[i+1]-(pi+1)*14, 0)
123 t=p.name+': '+ str('%g'% p.value)
125 t=p.name+': '+ str(p.value)
131 class GamePropertyVisualizer(bpy.types.Operator):
132 bl_idname = "view3d.game_props_visualizer"
133 bl_label = "Game Properties Visualizer"
134 bl_description = "Toggle the visualization of game properties"
137 def poll(cls, context):
138 return context.mode!='EDIT_MESH'
140 def modal(self, context, event):
141 context.area.tag_redraw()
143 # removal of callbacks when operator is called again
144 #print(context.scene.display_game_properties)
145 if context.scene.display_game_properties == -1:
148 context.scene.display_game_properties = 0
149 context.region.callback_remove(self.handle1)
150 context.region.callback_remove(self.handle2)
151 context.scene.display_game_properties = 0
155 return {'PASS_THROUGH'}
157 def invoke(self, context, event):
158 if context.area.type == 'VIEW_3D':
159 print(context.scene.display_game_properties)
160 if context.scene.display_game_properties == 0 or context.scene.display_game_properties == -1:
162 # operator is called for the first time, start everything
163 context.scene.display_game_properties = 1
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')
169 context.window_manager.modal_handler_add(self)
170 return {'RUNNING_MODAL'}
172 # operator is called again, stop displaying
173 context.scene.display_game_properties = -1
176 return {'RUNNING_MODAL'}
178 self.report({'WARNING'}, "View3D not found, can't run operator")
183 def menu_func(self, context):
184 col = self.layout.column(align=True)
185 col.operator(GamePropertyVisualizer.bl_idname, text="Visualize game props")
186 self.layout.separator()
190 bpy.types.Scene.display_game_properties = bpy.props.IntProperty(name='Visualize Game Poperties')
191 bpy.types.VIEW3D_PT_view3d_display.prepend(menu_func)
194 del bpy.types.Scene.display_game_properties
195 bpy.types.VIEW3D_PT_view3d_display.remove(menu_func)
197 if __name__ == "__main__":