Download presentation
Presentation is loading. Please wait.
Published byLynette Terry Modified over 9 years ago
1
H3D API Training Part 3.3: Python – H3D integration
2
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 *
3
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
4
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”]
5
H3DInterface module Functions for accessing bindable nodes: ◦ getActiveDeviceInfo() ◦ getActiveViewpoint() ◦ getActiveNavigationInfo() ◦ getActiveStereoInfo() ◦ getActiveBackground()
6
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
7
Special functions initialize() ◦ Called once upon initialization traverseSG() ◦ Called once per scene graph loop
8
H3D Python - Using Fields Creating field instances: ◦ field_a = SFFloat() ◦ field_b = SFFloat(5) #default value Routing field instances: ◦ field_a.route( field_b )
9
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
10
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 )
11
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.
12
H3D Python - Sphere.x3d <ROUTE fromNode=”MS” fromField=”leftButton” toNode=”PS” toField=”color” /> <ROUTE fromNode=”PS” fromField=”color” toNode=”MATERIAL” toField=”diffuseColor” />
13
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()
14
H3D Python – Sphere2.x3d GetRoutesIn() example <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” />
15
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()
16
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.
17
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”
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.