pyNode Example

This example uses a Python script to dynamically change the background color. It does this by generating Ygdrasil "skyColor" messages for the Environment node. The scene file uses the call() function with an assignment statement to tell the script the name of the Environment node that it is to use. Because the autoreset flag is set, you can modify pyExample1.py while Ygdrasil is running, and see the new results instantly.

The script also includes a ping() function that prints out various bits of information that are available to the Python code. It is called by a wand trigger in the scene.

These files are included in the pyg.tgz tar file.


The scene file - pyExample1.scene:

    light (position(0 0 1))
    
    Environment sky ()
    
    pyNode mypython (script(pyExample1.py), call(skyname='sky'), autoreset)
        {
        wandTrigger aTrigger (when(button1, mypython.call(ping())))
        }

The Python script - pyExample1.py:

import math

class pyExample1(ygNode):
    def __init__(self):
        self.skyname = ''
        print 'object of class pyExample1 constructed'
    def app(self):
        if len(self.skyname) > 0:
            r = (math.sin(ygdrasil.frameTime()) + 1.0) / 2.0
            g = (math.sin(ygdrasil.frameTime()/4) + 1.0) / 2.0
            b = (math.sin(ygdrasil.frameTime()*2) + 1.0) / 2.0
            ygdrasil.sendMessage('%s.skyColor(%f %f %f)' % (self.skyname, r, g, b))
    def ping(self):
        print name(), 'pinged'
        print 'time=',ygdrasil.frameTime(), 'deltaTime=', ygdrasil.frameDeltaTime()
        print 'parent=',self.parentName()
        print 'numChildren=',self.numChildren(), 'child0=',self.childName(0)
        print 'global origin=',self.origin(), 'local origin=',self.origin(self.parentName())




Last updated 13 September 2002.
home page