1#!/usr/bin/env python
2
3###
4# Copyright (c) 2002-2007 Systems in Motion
5#
6# Permission to use, copy, modify, and distribute this software for any
7# purpose with or without fee is hereby granted, provided that the above
8# copyright notice and this permission notice appear in all copies.
9#
10# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17#
18
19###
20# This is an example from the Inventor Mentor.
21# chapter 14, example 1.
22#
23# Use SoShapeKits to create two 3-D words, "NICE" and "HAPPY"
24# Use nodekit methods to access the fields of the "material"
25# and "transform" parts.
26# Use a calculator engine and an elapsed time engine to make
27# the words change color and fly about the screen.
28#
29
30import sys
31
32from pivy.coin import *
33from pivy.sogui import *
34
35def main():
36    # Initialize Inventor and Qt
37    myWindow = SoGui.init(sys.argv[0])
38    if myWindow == None: sys.exit(1)
39
40    root = SoSeparator()
41
42    # Create shape kits with the words "HAPPY" and "NICE"
43    happyKit = SoShapeKit()
44    root.addChild(happyKit)
45    happyKit.setPart("shape", SoText3())
46    happyKit.set("shape { parts ALL string \"HAPPY\"}")
47    happyKit.set("font { size 2}")
48
49    niceKit = SoShapeKit()
50    root.addChild(niceKit)
51    niceKit.setPart("shape", SoText3())
52    niceKit.set("shape { parts ALL string \"NICE\"}")
53    niceKit.set("font { size 2}")
54
55    # Create the Elapsed Time engine
56    myTimer = SoElapsedTime()
57
58    # Create two calculator - one for HAPPY, one for NICE.
59    happyCalc = SoCalculator()
60    happyCalc.a.connectFrom(myTimer.timeOut)
61    happyCalc.expression = """ta=cos(2*a); tb=sin(2*a);
62                              oA = vec3f(3*pow(ta,3),3*pow(tb,3),1);
63                              oB = vec3f(fabs(ta)+.1,fabs(.5*fabs(tb))+.1,1);
64                              oC = vec3f(fabs(ta),fabs(tb),.5)"""
65
66    # The second calculator uses different arguments to
67    # sin() and cos(), so it moves out of phase.
68    niceCalc = SoCalculator()
69    niceCalc.a.connectFrom(myTimer.timeOut)
70    niceCalc.expression = """ta=cos(2*a+2); tb=sin(2*a+2);
71                             oA = vec3f(3*pow(ta,3),3*pow(tb,3),1);
72                             oB = vec3f(fabs(ta)+.1,fabs(.5*fabs(tb))+.1,1);
73                             oC = vec3f(fabs(ta),fabs(tb),.5)"""
74
75    # Connect the transforms from the calculators...
76    happyXf = happyKit.getPart("transform",TRUE)
77    happyXf.translation.connectFrom(happyCalc.oA)
78    happyXf.scaleFactor.connectFrom(happyCalc.oB)
79    niceXf = niceKit.getPart("transform",TRUE)
80    niceXf.translation.connectFrom(niceCalc.oA)
81    niceXf.scaleFactor.connectFrom(niceCalc.oB)
82
83    # Connect the materials from the calculators...
84    happyMtl = happyKit.getPart("material",TRUE)
85    happyMtl.diffuseColor.connectFrom(happyCalc.oC)
86    niceMtl = niceKit.getPart("material",TRUE)
87    niceMtl.diffuseColor.connectFrom(niceCalc.oC)
88
89    myViewer = SoGuiExaminerViewer(myWindow)
90    myViewer.setSceneGraph(root)
91    myViewer.setTitle("Frolicking Words")
92    myViewer.viewAll()
93    myViewer.show()
94
95    SoGui.show(myWindow)
96    SoGui.mainLoop()
97
98if __name__ == "__main__":
99    main()
100