H3D Training Part3.3 - Python - H3D
Download
Report
Transcript H3D Training Part3.3 - Python - H3D
H3D API Training
Part 3.3: Python – H3D integration
H3D Python
Python module H3DInterface:
◦ C++ module defining core H3D types (Vec2f, Vec4f, Node, etc)
◦ Defining Fields and Field base class
◦ Functions to create nodes and access to bindable nodes
•
from H3DInterface import *
H3DInterface module
H3D types in Python:
◦
◦
◦
◦
◦
◦
◦
Node
Vec2f Vec2d (x, y)
Vec3f Vec3d (x, y, z)
Vec4f Vec4d (x, y, z, w)
Rotation (x, y, z, a)
Quaternion (x, y, z, w)
Matrix3f, Matrix4f, Matrix3d, Matrix4d
H3DInterface module
Functions for creating new nodes from X3D
◦ createX3DNodeFromURL( url )
◦ createX3DNodeFromString( string )
Returns node and dictionary of DEFed nodes
node, dn = createX3DNodeFromString( “test.x3d” )
# Access the node with DEF-name SPHERE in test.x3d
sphere = dn[“SPHERE”]
H3DInterface module
Functions for accessing bindable nodes:
◦
◦
◦
◦
◦
getActiveDeviceInfo()
getActiveViewpoint()
getActiveNavigationInfo()
getActiveStereoInfo()
getActiveBackground()
H3DInterface module
Scene instance access
◦ getCurrentScenes() - returns a list of all currently instantiated
Scene instances.
Special global fields
◦ time - Python access to Scene::time
◦ eventSink - Python access to Scene::eventSink
Special functions
initialize()
◦ Called once upon initialization
traverseSG()
◦ Called once per scene graph loop
H3D Python - Using Fields
Creating field instances:
◦ field_a = SFFloat()
◦ field_b = SFFloat(5) #default value
Routing field instances:
◦ field_a.route( field_b )
H3D Python - Using Fields
Setting field values:
◦ field_a.setValue( 5 )
◦ my_vec.setValue( Vec3f(1,2,3) )
Getting field values:
◦ a = field_b.getValue()
◦ b = my_vec.getValue().x
H3D Python - TypedField
Definition of new fields
◦ TypedField( base, ( input type ) )
◦ Logic in update() function
•
class MyVec3f( TypedField( SFVec3f, SFFloat ) ):
def update(self, event):
f = event.getValue()
return Vec3f( math.cos(f),math.sin(f), 0 )
H3D Python - X3D Files
Can load up external X3D files using the function
createX3DFromURL()
Returns a group node containing the nodes loaded and
a dictionary with the DEFed nodes.
H3D Python - Sphere.x3d
<Group>
<Shape>
<Appearance>
<Material DEF=”MATERIAL” />
</Appearance>
<Sphere radius=”0.1” />
</Shape>
<PythonScript DEF=”PS” url=”sphere.py” />
<MouseSensor DEF=”MS” />
<ROUTE fromNode=”MS” fromField=”leftButton”
toNode=”PS” toField=”color” />
<ROUTE fromNode=”PS” fromField=”color”
toNode=”MATERIAL” toField=”diffuseColor” />
</Group>
H3D Python - Sphere.py
from H3DInterface import *
class Color( TypedField( SFColor, SFBool ) ):
def update( self, event ):
if( event.getValue() ):
return RGB( 1, 0, 0 )
else:
return RGB( 0, 0, 1 )
color = Color()
H3D Python – Sphere2.x3d
GetRoutesIn() example
<Group>
<Shape>
<Appearance>
<Material DEF=”MATERIAL” />
</Appearance>
<Sphere radius=”0.1” />
</Shape>
<PythonScript DEF=”PS” url=”sphere.py” />
<MouseSensor DEF=”MS” />
<ROUTE fromNode=”MS” fromField=”leftButton”
toNode=”PS” toField=”color” />
<ROUTE fromNode=”MS” fromField=”rightButton”
toNode=”PS” toField=”color” />
<ROUTE fromNode=”PS” fromField=”color”
toNode=”MATERIAL” toField=”diffuseColor” />
</Group>
H3D Python – Sphere2.py
from H3DInterface import *
class Color( TypedField( SFColor, (SFBool, SFBool) ) ):
def update( self, event ):
routes_in = self.getRoutesIn()
left_button = routes_in[0].getValue()
right_button = routes_in[1].getValue()
if( left_button and right_button ):
return RGB( 1, 1, 0 )
elif( left_button ):
return RGB( 0, 0, 1 )
elif ( right_button ):
return RGB( 0, 1, 0 )
else:
return RGB( 1, 0, 0 )
color = Color()
Exercise 1
Create a program with a sphere.
◦ The color of the sphere should change from white to red
depending on how hard the user presses on it.
◦ Useful fields on a in geometry nodes are the “force” and
“isTouched” fields.
Exercise 2
Create a program which handles keyboard presses.
E.g.
◦ Add a box to the scene when pressing “b”
◦ Add a cylinder when pressing “c”
◦ Change the background color when pressing “w”