Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
derfies committed Nov 1, 2012
1 parent deb52f7 commit a683cce
Show file tree
Hide file tree
Showing 118 changed files with 7,948 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.pyc
Binary file added src/data/images/arrow-curve-flip.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/data/images/arrow-curve.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/data/images/disk-black-pencil.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/data/images/disk-black.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/data/images/document.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/data/images/folder-horizontal-open.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from panda3d.core import loadPrcFileData
loadPrcFileData( 'startup', 'window-type none' )

import pandaEditor


app = pandaEditor.App( redirect=False )
run()
32 changes: 32 additions & 0 deletions src/p3d/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from pandac.PandaModules import Vec3


P3D_VERSION = '0.1'


__version__ = P3D_VERSION


from constants import *
from functions import *
import commonUtils

from object import Object
from singleTask import SingleTask
from nodePathObject import NodePathObject
from pandaObject import *
from pandaBehaviour import PandaBehaviour
from pandaManager import PandaManager

from marquee import Marquee
from camera import *
from editorCamera import EditorCamera
from frameRate import FrameRate
from displayShading import DisplayShading
from mouse import *
from mousePicker import MousePicker
import geometry
try:
import wxPanda as wx
except:
print 'Failed to find wx module'
202 changes: 202 additions & 0 deletions src/p3d/camera.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
import math

from pandac.PandaModules import Camera as PCamera, Vec3, Quat, NodePath, LineSegs, PerspectiveLens

import p3d


CAM_USE_DEFAULT = 1
CAM_DEFAULT_STYLE = 2
CAM_VIEWPORT_AXES = 4


class Camera( NodePath, p3d.SingleTask ):

"""Class representing a camera."""

class Target( NodePath ):

"""Class representing the camera's point of interest."""

def __init__( self, pos=Vec3( 0, 0, 0 ) ):
NodePath.__init__( self, 'target' )

self.defaultPos = pos

class Axes( NodePath ):

"""Class representing the viewport camera axes."""

def __Create( self, thickness, length ):

# Build line segments
ls = LineSegs()
ls.setThickness( thickness )

# X Axis - Red
ls.setColor( 1.0, 0.0, 0.0, 1.0 )
ls.moveTo( 0.0, 0.0, 0.0 )
ls.drawTo( length, 0.0, 0.0 )

# Y Axis - Green
ls.setColor( 0.0, 1.0, 0.0, 1.0 )
ls.moveTo( 0.0,0.0,0.0 )
ls.drawTo( 0.0, length, 0.0 )

# Z Axis - Blue
ls.setColor( 0.0, 0.0, 1.0, 1.0 )
ls.moveTo( 0.0,0.0,0.0 )
ls.drawTo( 0.0, 0.0, length )

return ls.create()

def __init__( self, thickness=1, length=25 ):
ls = self.__Create( thickness, length )
NodePath.__init__( self, ls )

def __init__( self, name='camera', *args, **kwargs ):
pos = kwargs.pop( 'pos', (0, 0, 0) )
targetPos = kwargs.pop( 'targetPos', (0, 0, 0) )
style = kwargs.pop( 'style', CAM_DEFAULT_STYLE )
p3d.SingleTask.__init__( self, name, *args, **kwargs )

self.zoomLevel = 2
self.defaultPos = pos
self.style = style

# Use Panda's default camera
if self.style & CAM_USE_DEFAULT:
self.cam = getBase().cam
#self.camNode = getBase().camNode

# Otherwise create a new one
else:

# Create camera
self.cam = NodePath( PCamera( name ) )

# Create lens
lens = PerspectiveLens()
lens.setAspectRatio( 800.0 / 300.0 )
self.cam.node().setLens( lens )

# Wrap the camera in this node path class
NodePath.__init__( self, self.cam )

# Create camera styles
if self.style & CAM_VIEWPORT_AXES:
self.axes = self.Axes()
self.axes.reparentTo( self.rootP2d )

# Create camera target
self.target = self.Target( pos=targetPos )

self.Reset()

def Reset( self ):

# Reset camera and target back to default positions
self.target.setPos( self.target.defaultPos )
self.setPos( self.defaultPos )

# Set camera to look at target
self.lookAt( self.target.getPos() )
self.target.setQuat( self.getQuat() )

def Move( self, moveVec ):

# Modify the move vector by the distance to the target, so the further
# away the camera is the faster it moves
cameraVec = self.getPos() - self.target.getPos()
cameraDist = cameraVec.length()
moveVec *= cameraDist / 300

# Move the camera
self.setPos( self, moveVec )

# Move the target so it stays with the camera
self.target.setQuat( self.getQuat() )
test = Vec3( moveVec.getX(), 0, moveVec.getZ() )
self.target.setPos( self.target, test )

def Orbit( self, delta ):

# Get new hpr
newHpr = Vec3()
newHpr.setX( self.getH() + delta.getX() )
newHpr.setY( self.getP() + delta.getY() )
newHpr.setZ( self.getR() )

# Set camera to new hpr
self.setHpr( newHpr )

# Get the H and P in radians
radX = newHpr.getX() * ( math.pi / 180.0 )
radY = newHpr.getY() * ( math.pi / 180.0 )

# Get distance from camera to target
cameraVec = self.getPos() - self.target.getPos()
cameraDist = cameraVec.length()

# Get new camera pos
newPos = Vec3()
newPos.setX( cameraDist * math.sin( radX ) * math.cos( radY ) )
newPos.setY( -cameraDist * math.cos( radX ) * math.cos( radY ) )
newPos.setZ( -cameraDist * math.sin( radY ) )
newPos += self.target.getPos()

# Set camera to new pos
self.setPos( newPos )

def Frame( self, nps ):

# Create a bounding volume which includes all node paths
parent = render.attachNewNode( 'parent' )
dupeNps = []
for np in nps:
dupeNp = np.copyTo( parent )
dupeNp.setTransform( np.getTransform( parent ) )
dupeNps.append( dupeNp )
bounds = parent.getBounds()
parent.removeNode()

# Bail if the bounds are infinite
if bounds.isInfinite():
return

# Move the camera and the target the the bounding center
self.target.setPos( bounds.getCenter() )
self.setPos( bounds.getCenter() )

# Now move the camera back so the view accomodates all object(s)
# Default the bounding radius to something reasonable if the object
# has no size
fov = self.GetLens().getFov()
radius = bounds.getRadius()
if not radius:
radius = 0.5
distance = radius / math.tan( math.radians( min( fov[0], fov[1] ) * 0.5 ) )
self.setY( self, -distance )

def cameraMovement( self, task ):
x,y,z = self.cube.getPos()
#smoothly follow the cube...
self.setX( self.getX() - ( ( self.getX() - x - 18 * self.zoomLevel ) * 5 * globalClock.getDt() ) )
self.setY( self.getY() - ( ( self.getY() - y + 5 * self.zoomLevel ) * 5 * globalClock.getDt() ) )
self.setZ( 15 * self.ZOOMLEVEL )
self.setHpr( 75, -37, 0 )

return task.cont

def OnUpdate( self, task ):

# Position axes 30 pixels from top left corner
self.axes.setPos( Vec3( 30, 0, -30 ) )

# Set rotation to inverse of camera rotation
cameraQuat = Quat( self.getQuat() )
cameraQuat.invertInPlace()
self.axes.setQuat( cameraQuat )

def GetLens( self ):
return self.cam.node().getLens()
71 changes: 71 additions & 0 deletions src/p3d/commonUtils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import pandac.PandaModules as pm

import os


def Relpath( target, base=os.curdir ):
"""
Return a relative path to the target from either the current directory or
an optional base directory. Base can be a directory specified either as
absolute or relative to current directory.
"""
base_list = ( os.path.abspath( base ) ).split( os.sep )
target_list = (os.path.abspath( target ) ).split( os.sep )

# If the base was just a drive sometimes we get an empty string at the end
# of the list which messes up the join operation at the end.
if not base_list[-1]:
base_list = base_list[:-1]

# On the windows platform the target may be on a completely different
# drive from the base
if os.name in ['nt','dos','os2'] and base_list[0].lower() <> target_list[0].lower():
raise OSError, ''.join( ['Target is on a different drive to base. Target: ', target_list[0], ', base: ', base_list[0]] )

# Starting from the filepath root, work out how much of the filepath is
# shared by base and target
for i in range( min( len( base_list ), len( target_list ) ) ):
if base_list[i].lower() <> target_list[i].lower():
break
else:

# If we broke out of the loop, i is pointing to the first differing
# path elements. If we didn't break out of the loop, i is pointing to
# identical path elements. Increment i so that in all cases it points
# to the first differing path elements.
i += 1

rel_list = [os.pardir] * ( len( base_list ) - i ) + target_list[i:]
return os.path.join( *rel_list )


def GetTrsMatrices( xform ):
"""
Return translation, rotation and scale matrices back for the specified
transform.
"""
# Get translation and rotation matrices
rotMat = pm.Mat4()
xform.getQuat().extractToMatrix( rotMat )
transMat = pm.Mat4().translateMat( xform.getPos() )

# More care must be taken to get the scale matrix as simply calling
# Mat4().scaleMat( xform.getScale() ) won't account for shearing or other
# weird scaling. To get this matrix simply remove the translation and
# rotation components from the xform.
invRotMat = pm.Mat4()
invRotMat.invertFrom( rotMat )
invTransMat = pm.Mat4()
invTransMat.invertFrom( transMat )
scaleMat = xform.getMat() * invTransMat * invRotMat

return transMat, rotMat, scaleMat


def GetInvertedMatrix( mat ):
"""
Invert the indicated matrix, sending back a new matrix.
"""
invMat = pm.Mat4()
invMat.invertFrom( mat )
return invMat
6 changes: 6 additions & 0 deletions src/p3d/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import pandac.PandaModules as pm


X_AXIS = pm.Vec3(1, 0, 0)
Y_AXIS = pm.Vec3(0, 1, 0)
Z_AXIS = pm.Vec3(0, 0, 1)
30 changes: 30 additions & 0 deletions src/p3d/displayShading.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from direct.showbase.DirectObject import DirectObject


class DisplayShading( DirectObject ):

"""Toggles display shading."""

def SetWireframe( self, value ):
if value:
base.wireframeOn()
else:
base.wireframeOff()

def SetTexture( self, value ):
if value:
base.textureOn()
else:
base.textureOff()

def Wireframe( self ):
self.SetWireframe( True )
self.SetTexture( False )

def Shade( self ):
self.SetWireframe( False )
self.SetTexture( False )

def Texture( self ):
self.SetWireframe( False )
self.SetTexture( True )
Loading

0 comments on commit a683cce

Please sign in to comment.