创建Python类并添加osgb文件

时间:2014-01-20 15:51:08

标签: python oop vizard

我正在尝试在python中为一个星球创建一个类。它只是想将osgb文件附加到此类。

目前我正在将我的模型添加到这个程序......

    import viz
import vizact
import vizmat
import vizcam
import Planet

viz.setMultiSample(10)
viz.fov(80)
viz.go()
viz.collision(viz.ON)
viz.phys.enable()

#mercury
mercury = viz.addChild('planets/mercury.osgb')
mercury.setPosition([20,0,10])
mercury.setAxisAngle([1,0,0,90])

#venus
venus = viz.addChild('planets/venus.osgb')
venus.setPosition([20,0,20])
venus.setAxisAngle([1,0,0,90])

#earth
earth = viz.addChild('planets/earth.osgb')
earth.setPosition([20,0,30])
earth.setAxisAngle([1,0,0,90])

#mars
mars = viz.addChild('planets/mars.osgb')
mars.setPosition([20,0,40])
mars.setAxisAngle([1,0,0,90])

#jupiter
jupiter = viz.addChild('planets/jupiter.osgb')
jupiter.setPosition([20,0,50])
jupiter.setAxisAngle([1,0,0,90])

#saturn
saturn = viz.addChild('planets/saturn.osgb')
saturn.setPosition([100,0,60])
saturn.setAxisAngle([1,0,0,0])

#uranus
uranus = viz.addChild('planets/uranus.osgb')
uranus.setPosition([-50,0,70])
uranus.setAxisAngle([0,1,0,90])

#neptune
neptune = viz.addChild('planets/neptune.osgb')
neptune.setPosition([20,0,80])
neptune.setAxisAngle([1,0,0,90])

#pluto
pluto = viz.addChild('planets/pluto.osgb')
pluto.setPosition([20,0,90])
pluto.setAxisAngle([1,0,0,90])

BirdEyeWindow = viz.addWindow()
BirdEyeWindow.fov(60)
BirdEyeWindow.visible(0,viz.SCREEN)
BirdEyeView = viz.addView()
BirdEyeWindow.setView(BirdEyeView)
BirdEyeView.setPosition([0,250,0])
BirdEyeView.setEuler([0,90,0]) 


MOVE_SPEED = 50;
TURN_SPEED = 60;

viz.mouse(viz.OFF)

view = viz.MainView;

#Keyboard interaction to move the view point around
def updateCam():
    if viz.key.isDown(viz.KEY_UP):
            view.move([0,0,MOVE_SPEED*viz.elapsed()],viz.BODY_ORI)
    elif viz.key.isDown(viz.KEY_DOWN):
            view.move([0,0,-MOVE_SPEED*viz.elapsed()],viz.BODY_ORI)
    elif viz.key.isDown(viz.KEY_RIGHT):
            view.setEuler([TURN_SPEED*viz.elapsed(),0,0],viz.BODY_ORI,viz.REL_PARENT)
    elif viz.key.isDown(viz.KEY_LEFT):
            view.setEuler([-TURN_SPEED*viz.elapsed(),0,0],viz.BODY_ORI,viz.REL_PARENT) 

vizact.ontimer(0,updateCam)



#selecting Details of planets
#Called when user clicks on an object
def pickEarth():
    print 'Mouse Clicked'
    object = viz.pick() #detects object mouse is currently over
    if object.valid(): 
        print 'Clicked on a planet'
        print object



vizact.onmousedown(viz.MOUSEBUTTON_LEFT, pickEarth)

但是我宁愿拥有一个名为planet的类,并且每个类都链接到osgb模型。

我浏览了一下网络,似乎无法找到有关如何执行此操作的任何资源。我很感激有关如何修改上面代码的任何帮助,以便使用Planet对象的不同实例。

1 个答案:

答案 0 :(得分:0)

我不确定这是否是您想要的,但您可以执行以下操作:

class MyPlanet:
    def __init__(self, osgb_path):
        self.viz_ref = viz.addChild(osgb_path)
        self.osgb = osgb_path
    def setPosition(self, coordinates):
        self.viz_ref.setPosition(coordinates)
    def setAxisAngle(self, coordinates):
        self.viz_ref.setAxisAngle(coordinates)

mercury = MyPlanet('planets/mercury.osgb')
mercury.setPosition([20, 0, 10])
mercury.setAxisAngle([1, 0, 0, 90])

这只是viz.addChild(...)返回的一个包装器,但是,我真的没有看到这个的价值。