Basic motion in Python

From Digital Spaces

Jump to: navigation, search

Here we'll show a sample script that finds a scene graph node and moves it.

if 'moveNode' not in locals().keys():
	class NodeMover:
		""" This class takes a node name, or a list of node names, and will move those nodes continuously.
			The motion is hard coded, see PerformHeartbeat.
		"""
		def __init__(self, nodeNames):
			self.sceneNodes = list()
			# Check if the constructor was just passed a string, not a list of strings.
			if isinstance(nodeNames, str ):
				nodeNames = ( nodeNames, )
 

This section queries all the available scene graph managers for nodes of the specified names. There should only be one, but GetFactoriesWithInterface always returns a list. Using every DigitalSpaces::DISGManager (scene graph manager), for every node name provided, check for a scenenode by that name. If found, store the interface to the scene node.

#Rubbish comment added for wiki highlighter
			for scenegraphManager in dss_core.DISGManager.GetFactoriesWithInterface():
				for nodeName in nodeNames:
					sceneNode = scenegraphManager.GetSGNode(nodeName)
					if sceneNode:
						self.sceneNodes.append(sceneNode)
 
		def PerformHeartbeat(self):
			""" This function will be called each run of the script, which is each Heartbeat of Digital Spaces.
			"""
 

For each of the stored scene node objects, retrieve its current position (as a vector). Increase the y (vertical) component of its position. Then apply the updated position to the scene node object.

#Rubbish comment added for wiki highlighter
			for sceneNode in self.sceneNodes:
				x,y,z = sceneNode.GetPosition()
				# Move upward 1/100th a meter per Heartbeat, thus, 1 meter per second.
				y = y + 0.01
				sceneNode.SetPosition( (x,y,z) )
			
	# Create an instance of the NodeMover class, providing it with the name of the scene node to be moved
	moveNode = NodeMover("Omni01")
	
# Each time this script is ran (each Digital Spaces Heartbeat), call NodeMover::PerformHeartbeat
moveNode.PerformHeartbeat()
 

As it is, this script isn't a lot of use. It will continuously move the specified scene node (and any attached render objects) upwards, but there is no upper limit. However it does show how the position of a scene node may be extracted, altered, and applied.

<node name="Omni01">
	<position x="-0.86682" y="144.632" z="-376.857" />
	
	<billboardSet material="sunflare" name="flare1" width="150" height="150">
		<billboard>
			<position x="0" y="0" z="0" />
		</billboard>
	</billboardSet>
</node>
 

If the script was used with a scene graph specification as shown, the sun billboard would move away upward onto infinity.

See the next tutorial Basic rotation in Python, or all the tutorials at Documentation/Tutorials.

Personal tools