Skip to content

Instantly share code, notes, and snippets.

@d33tah
Created August 29, 2010 23:17
Show Gist options
  • Save d33tah/556801 to your computer and use it in GitHub Desktop.
Save d33tah/556801 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
# -*- coding: utf-8 -*-
from pynk import *
from config import * #might be removed - defines only "login" and "password"
from time import sleep
import math
from PIL import Image
import os
import sys
import direct.directbase.DirectStart
from direct.showbase.DirectObject import DirectObject
from panda3d.core import Point2, Point3, TextNode, BitMask32
from panda3d.core import CollisionRay, CollisionNode, CollisionTraverser, CollisionHandlerQueue, GeomNode
from direct.gui.DirectGui import *
#from panda3d.core import Vec3, Vec4, Point2, Point3, BitMask32
#from direct.showbase.DirectObject import DirectObject
#from panda3d.core import CollisionTraverser,CollisionNode, TextNode
#from panda3d.core import CollisionHandlerQueue,CollisionRay, GeomNode
def photo_to_tex(nk,obj,url):
filename = 'cache/'+url.replace('/','_')
if not os.path.isdir('photos'):
os.makedirs('photos')
if not os.path.exists(filename+'.png'):
open(filename,'w').write(nk.br.open(url).read())
Image.open(filename).save(filename+'.png')
tex = loader.loadTexture(filename+'.png')
obj.setTexture(tex,1)
def loadObject(tex = None, pos = Point2(0,0), depth = 55, scale = 1, transparency = True):
obj = loader.loadModel("square")
obj.reparentTo(camera)
obj.setPos(Point3(pos.getX(), depth, pos.getY()))
obj.setScale(scale)
obj.setBin("unsorted", 0)
obj.setDepthTest(False)
if transparency: obj.setTransparency(1)
if tex:
tex = loader.loadTexture(tex+'.png')
obj.setTexture(tex, 1)
return obj
class World(DirectObject):
def drawProfiles(self,task):
while self.i!=self.n:
x=math.sin(self.i*(360/self.n))*self.r
y=math.cos(self.i*(360/self.n))*self.r
profile_obj = loadObject(pos=Point2(x,y))
profile_obj.node().setIntoCollideMask(BitMask32.bit(1))
profile_obj.node().setTag('number', str(self.i))
profile = self.my_friends[self.i]
url = profile.get_details().avatar_thumb_url
photo_to_tex(self.nk,profile_obj,url)
self.images[self.i] = profile_obj
self.profiles[self.i] = self.my_friends[self.i]
self.i+=1
return task.again
def __init__(self):
self.nameText = OnscreenText("Click some Image!",
fg=(1,1,1,1), pos = (-1.3, .95), align = TextNode.ALeft, mayChange = 1)
self.accept('escape', sys.exit) #Escape quits
self.accept("mouse1", self.onClick)
self.accept("mouse2", self.removeAll)
#camera.setPosHpr ( 0, -8, 2.5, 0, -9, 0 )
base.setBackgroundColor(0,0,0)
self.images = {}
self.profiles = {}
self.center_triangle = loadObject("triangle", pos=Point2(0,0),scale=9)
self.center_triangle.node().setTag('number', str(-1))
self.collisionSetup()
self.nk = PyNK()
self.nk.login(login,password)
self.my_friends = self.nk.my_profile.get_friends()
self.n = len(self.my_friends)
self.r=14;
self.i = 0;
taskMgr.doMethodLater(0.1, self.drawProfiles, 'tickTask')
def collisionSetup(self):
self.pickerNode = CollisionNode('mouseRay')
self.pickerNP = camera.attachNewNode(self.pickerNode)
self.pickerRay = CollisionRay()
self.pickerNode.addSolid(self.pickerRay)
self.pickerNode.setFromCollideMask(GeomNode.getDefaultCollideMask())
self.collisionTraverser = CollisionTraverser()
self.handlerQueue = CollisionHandlerQueue()
self.collisionTraverser.addCollider(self.pickerNP, self.handlerQueue)
def removeAll(self):
for i in range(self.n):
self.images[i].remove()
def onClick(self):
mpos = base.mouseWatcherNode.getMouse()
self.pickerRay.setFromLens(base.camNode, mpos.getX(), mpos.getY())
self.collisionTraverser.traverse(render)
if self.handlerQueue.getNumEntries() > 0:
self.handlerQueue.sortEntries()
pickedObj = self.handlerQueue.getEntry(0).getIntoNodePath()
number = int(pickedObj.findNetTag('number').getTag('number'))
if number==-1: return
profile = self.profiles[number]
url = NK_photo(nk=self.nk,url=profile.get_details().avatar_url).get_image_url()
photo_to_tex(self.nk,self.center_triangle,url)
self.nameText.setText("%s (%s)" % (profile.name, profile.friends_count))
w = World()
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment