Hi,
I want to reorient my 3D scene’s camera so that it is looking down at the grid (as a bird’s eye view) when the window opens. As of right now, when the program runs, the grid lines up with where the camera is currently, so it looks like a straight line. (When you hold the mouse down and move it, however, you can see the grid better.)
I have tried to use camera.setPos(0, -15, 5) and camera.setHpr(0, -15, 0), but my IDE PyCharm warns me that camera is unresolved, and base.camera doesn’t work either.
Here is my code:
import sys
import cv2
from direct.showbase.ShowBase import ShowBase
from panda3d_viewer import geometry
import numpy as np
from pandac.PandaModules import *
class Application(ShowBase):
LightMask = BitMask32(1)
def __init__(self):
ShowBase.__init__(self)
self._grid = self._make_grid()
self.show_grid(self.config.GetBool('show-grid', True))
self._floor = self._make_floor()
self.show_floor(self.config.GetBool('show-floor', False))
#self.disableMouse()
camera.setPos(0, -15, 5) # move the camera back
camera.setHpr(0, -15, 0) # make the camera look down
def make_grid(num_ticks=10, step=1.0):
ticks = np.arange(-num_ticks // 2, num_ticks // 2 + 1) * step
vformat = GeomVertexFormat.get_v3()
vdata = GeomVertexData('vdata', vformat, Geom.UHStatic)
vdata.uncleanSetNumRows(len(ticks) * 4)
vertex = GeomVertexWriter(vdata, 'vertex')
for t in ticks:
vertex.addData3(t, ticks[0], 0)
vertex.addData3(t, ticks[-1], 0)
vertex.addData3(ticks[0], t, 0)
vertex.addData3(ticks[-1], t, 0)
prim = GeomLines(Geom.UHStatic)
prim.addNextVertices(len(ticks) * 4)
geom = Geom(vdata)
geom.addPrimitive(prim)
return geom
def _make_grid(self):
model = GeomNode('grid')
model.add_geom(geometry.make_grid())
node = self.render.attach_new_node(model)
node.set_light_off()
node.set_render_mode_wireframe()
node.set_antialias(AntialiasAttrib.MLine)
node.hide(self.LightMask)
return node
def show_grid(self, show):
if show:
self._grid.show()
else:
self._grid.hide()
def _make_floor(self):
model = GeomNode('floor')
model.add_geom(geometry.make_plane(size=(10, 10)))
node = self.render.attach_new_node(model)
node.set_color(Vec4(0.3, 0.3, 0.3, 1))
material = Material()
material.set_ambient(Vec4(0, 0, 0, 1))
material.set_diffuse(Vec4(0.3, 0.3, 0.3, 1))
material.set_specular(Vec3(1, 1, 1))
material.set_roughness(0.8)
node.set_material(material, 1)
return node
def make_plane(size=(1.0, 1.0)): # plane size as x,y
vformat = GeomVertexFormat.get_v3n3t2()
vdata = GeomVertexData('vdata', vformat, Geom.UHStatic)
vdata.uncleanSetNumRows(4)
vertex = GeomVertexWriter(vdata, 'vertex')
normal = GeomVertexWriter(vdata, 'normal')
tcoord = GeomVertexWriter(vdata, 'texcoord')
quad = ((0, 0), (1, 0), (0, 1), (1, 1))
for u, v in quad:
vertex.addData3((u - 0.5) * size[0], (v - 0.5) * size[1], 0)
normal.addData3(0, 0, 1)
tcoord.addData2(u, v)
prim = GeomTriangles(Geom.UHStatic)
prim.addVertices(0, 1, 2)
prim.addVertices(2, 1, 3)
geom = Geom(vdata)
geom.addPrimitive(prim)
return geom
# Render the floor
def show_floor(self, show):
if show:
self._floor.show()
else:
self._floor.hide()
def setup_camera(self, pos):
self.setPos()
app = Application()
app.run()