Previous Blog Next Blog
Prev/Next Blog
by date

TSE Python Module - Example Usage

TSE Python Module - Example Usage
Name:Prairie Games
Date Posted:May 10, 2006
Rating:3.6 out of 5
Public:YES
Comments:YES
RSS Feed:GarageGames Blog feedor Subscribe with .
Profile Page:View profile page for Prairie Games

Blog post
I have been refining our Python support with TSE. Below is example usage for anyone interested. I'll most likely put this out as a resource or make the code available in some other way. The will also work in TGE 1.3 and 1.4


#--- TSE Python Module Example ---

#TSE as a standard Python extension (no longer a executable)
import pytse

#initialize pytse, this also executes main.cs and the .cs packages
pytse.initialize()

#example of executing a script file
f = file("myscript.cs","rb")
script = f.read()
f.close()
pytse.evaluate(script)

#or, just generate the cs code right inside Python!
pytse.evaluate("""
new GuiBitmapButtonCtrl(MyButton) {
profile = "GuiButtonProfile";
horizSizing = "right";
vertSizing = "bottom";
position = "404 361";
extent = "285 85";
minExtent = "8 2";
visible = "1";
text = "Button";
groupNum = "-1";
buttonType = "PushButton";
bitmap = "./button";
helpTag = "0";
};""")

#it's easy to grab a reference to the button we created
button = TSEObject("MyButton")

#buttons are kind of worthless without commands. Let's make one:
def OnMyButton(value):
print "Button pushed with value",value

#export the function to the console system in much the same way the C++ system does...
#we also support optional namespaces, usage documentation, and min/max args

pytse.export(OnMyButton,"MyButton","OnButton","Example button command",1,1)

#we can get and set fields (including dynamic fields). We'll set our button's command:
button.command = "MyButton::OnButton(42);"

#we can call console methods on our TSEObjects... So, let's simulate a button click.
#the OnMyButton function will be called with the value 42 :)

button.performClick()

#note that getting an object reference to the button and setting the command like this is
purely for illustration. You can also: command = "MyButton::OnButton(42);" in the evaluated code.


#moving on, we can get and set global variables
pytse.setglobal("$MyVariable",42)
print pytse.getglobal("$MyVariable")
pytse.evaluate('echo ("*** Here is your variable:" @ $MyVariable);')

#the main loop is broken out and can be combined with other frameworks rather easily
while pytse.tick():
pass

#cleanup pytse.. goodbye!
pytse.shutdown()


-Josh Ritter
Prairie Games, Inc

Recent Blog Posts
List:03/29/08 - TGEA 1.7 Build System and Embedded Python
03/14/08 - MegaTerrains - TGEA Update
01/18/08 - Minions of Mirth: Undead Wars Expansion
01/04/08 - Physics Overhaul - Video
12/26/07 - Web Integration - Video
12/21/07 - New MMO Client - Trees - Day/Night Video
12/18/07 - Minions of Mirth - 1.26 - Holiday Edition!
11/28/07 - TGB/TGEA integration first pass

Submit ResourceSubmit your own resources!

James Urquhart   (May 10, 2006 at 21:25 GMT)   Resource Rating: 3
Simple yet seemingly quite powerful :)

Keep up the good work Josh.

Vashner   (May 10, 2006 at 23:46 GMT)   Resource Rating: 5
Awesome...thx.

Johnny Hill   (May 11, 2006 at 18:11 GMT)
Why not just write another torque book using Python Josh :))), Knowledge is in high demand nowadays. Even I can follow that code up thar :)

Paul Malyschko   (May 12, 2006 at 09:45 GMT)
How were you creating the module Josh? I had tried to create a serviceable module using C code, but found it a horrendous experience, and so switched to Pyrex. I've found that to be a whole lot quicker (though it does have its quirks).

Jason McIntosh   (May 12, 2006 at 19:14 GMT)
Yeehaw! Now I might buy TSE. Looking forward to this being released.

Jonathon Stevens   (Jun 14, 2006 at 19:34 GMT)
Did this materialize into a resource every Josh?

Kim Ball   (Aug 22, 2006 at 18:47 GMT)
Another question for you, Josh. Remember way back when you posted on the Daz forums looking for someone to render graphics using a python script you developed that would automatically rotate the camera (or was it the figure) to capture 360 view of the figure and/or animations?

Any chance you could make that a resource or tool we could buy? Especially good for TGB. Would make an awsome addition to TGB or the adventure kit.

Vashner   (Sep 21, 2006 at 20:00 GMT)   Resource Rating: 5
I would pay for this.. it's worth good money.

You must be a member and be logged in to either append comments or rate this resource.