def unproject(winx, winy, winz, modelMatrix, projMatrix, viewport):
 npModelMatrix = numpy.matrix(numpy.array(modelMatrix, numpy.float64).reshape((4, 4)))
 npProjMatrix = numpy.matrix(numpy.array(projMatrix, numpy.float64).reshape((4, 4)))
 finalMatrix = (npModelMatrix * npProjMatrix)
 finalMatrix = numpy.linalg.inv(finalMatrix)
 viewport = map(float, viewport)
 vector = numpy.array([((((winx - viewport[0]) / viewport[2]) * 2.0) - 1.0), ((((winy - viewport[1]) / viewport[3]) * 2.0) - 1.0), ((winz * 2.0) - 1.0), 1]).reshape((1, 4))
 vector = (numpy.matrix(vector) * finalMatrix).getA().flatten()
 ret = (list(vector)[0:3] / vector[3])
 return ret

def unproject ( winx , winy , winz , modelMatrix , projMatrix , viewport ) : npModelMatrix = numpy . matrix ( numpy . array ( modelMatrix , numpy . float64 ) . reshape ( ( 4 , 4 ) ) ) npProjMatrix = numpy . matrix ( numpy . array ( projMatrix , numpy . float64 ) . reshape ( ( 4 , 4 ) ) ) finalMatrix = ( npModelMatrix * npProjMatrix ) finalMatrix = numpy . linalg . inv ( finalMatrix ) viewport = map ( float , viewport ) vector = numpy . array ( [ ( ( ( ( winx - viewport [ 0 ] ) / viewport [ 2 ] ) * 2.0 ) - 1.0 ) , ( ( ( ( winy - viewport [ 1 ] ) / viewport [ 3 ] ) * 2.0 ) - 1.0 ) , ( ( winz * 2.0 ) - 1.0 ) , 1 ] ) . reshape ( ( 1 , 4 ) ) vector = ( numpy . matrix ( vector ) * finalMatrix ) . getA ( ) . flatten ( ) ret = ( list ( vector ) [ 0 : 3 ] / vector [ 3 ] ) return ret
projects window position to 3d space .