TSE Python Module - Example Usage
by Prairie Games · 05/10/2006 (11:51 am) · 8 comments
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
-Josh Ritter
Prairie Games, Inc
[b]#--- TSE Python Module Example ---[/b]
[b]#TSE as a standard Python extension (no longer a executable)[/b]
import pytse
[b]#initialize pytse, this also executes main.cs and the .cs packages[/b]
pytse.initialize()
[b]#example of executing a script file[/b]
f = file("myscript.cs","rb")
script = f.read()
f.close()
pytse.evaluate(script)
[b]#or, just generate the cs code right inside Python![/b]
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";
};""")
[b]#it's easy to grab a reference to the button we created[/b]
button = TSEObject("MyButton")
[b]#buttons are kind of worthless without commands. Let's make one:[/b]
def OnMyButton(value):
print "Button pushed with value",value
[b]#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[/b]
pytse.export(OnMyButton,"MyButton","OnButton","Example button command",1,1)
[b]#we can get and set fields (including dynamic fields). We'll set our button's command:[/b]
button.command = "MyButton::OnButton(42);"
[b]#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 :)[/b]
button.performClick()
[i]#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.[/i]
[b]#moving on, we can get and set global variables[/b]
pytse.setglobal("$MyVariable",42)
print pytse.getglobal("$MyVariable")
pytse.evaluate('echo ("*** Here is your variable:" @ $MyVariable);')
[b]#the main loop is broken out and can be combined with other frameworks rather easily[/b]
while pytse.tick():
pass
[b]#cleanup pytse.. goodbye![/b]
pytse.shutdown()-Josh Ritter
Prairie Games, Inc
#2
05/10/2006 (4:46 pm)
Awesome...thx.
#3
05/11/2006 (11:11 am)
Why not just write another torque book using Python Josh :))), Knowledge is in high demand nowadays. Even I can follow that code up thar :)
#4
05/12/2006 (2:45 am)
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).
#5
05/12/2006 (12:14 pm)
Yeehaw! Now I might buy TSE. Looking forward to this being released.
#6
06/14/2006 (12:34 pm)
Did this materialize into a resource every Josh?
#7
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.
08/22/2006 (11:47 am)
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.
#8
09/21/2006 (1:00 pm)
I would pay for this.. it's worth good money.
Associate James Urquhart
Keep up the good work Josh.