[Bf-funboard] sample script for use with getPerspMatrix()

Tom Musgrove tommusgrove__ at hotmail.com
Thu Oct 14 01:56:46 CEST 2004

Here is a sample python script showing the usage of getPerspMatrix()

It gets the screen pixel location of the vertices of an object and prints 

# this script finds the screen pixel locations of the selected object

import Blender
from Blender import Object, NMesh, Window, Mathutils

for win3d in Window.GetScreenInfo(Window.Types.VIEW3D):
	min_x, min_y, max_x, max_y = win3d.get('vertices')
	mid_x = (max_x+min_x)/2		#the x midpoint of the 3dview window
	mid_y = (max_y+min_y)/2		#the y midpoint of the 3dview window
	sx_2 = (max_x-min_x)/2		#scaling in pixels in the x direction
	sy_2 = (max_y-min_y)/2		#scaling in pixels in the y direction

obj_loc = Blender.Object.GetSelected()[0].loc
obj_loc_vec = Mathutils.Vector(list(obj_loc))
myMesh = Blender.Object.GetSelected()[0].getData()
#obj_mat = Blender.Object.GetSelected()[0].getMatrix()
obj_mat = Blender.Object.GetSelected()[0].getMatrix('worldspace')
perspMatrix = Blender.Window.GetPerspMatrix()
for vert in myMesh.verts:
	myVec = Mathutils.Vector(list(vert.co+obj_loc_vec)+[1.0])
	vertGlobalCoords = Blender.Mathutils.MatMultVec(obj_mat,myVec)
	vertRawScreenCoords = 
	print int(mid_x+vertRawScreenCoords[0]*sx_2) #screen x
	print int(mid_y+vertRawScreenCoords[1]*sy_2) #screen y


Get ready for school! Find articles, homework help and more in the Back to 
School Guide! http://special.msn.com/network/04backtoschool.armx

More information about the Bf-funboard mailing list