Content Scripting Python First Script
From RexWiki
First script, how to define a python script for a prim
NOTE: Functionality different / still missing from Naali
As a very first taste of python scripting lets define a script for a prim and use a ready sample script, set it to a prim and see it working in the world. This tutorial assumes that you know how to do some basic things like creating prims to your world.
- Start rex server. The server is running when the log writing stops and there is a blue text "Startup complete, serving 1 region(s)".
- Start rex client and login to the world
- Go to build mode and create a normal cube prim to the world
- Click the reX tab in the build menu's tabs, the prim should be still selected
- Click the "Extended properties" on which enables the tab page
- As class name type sampleobject.TestActor and hit enter.
- If you now momentarily switch from viewer to the server, you'll notice that the log has "TestActor EventCreated" entry in it.
- Close the build menu and touch the prim by clicking it with your left mouse button. You can't touch objects while building so close the build menu before touching the prim.
- The prim should change rotation, scale, it should say something to you and the text on top of the prim changes.
- Congratulations. You have successfully defined a python script for a prim. The script which is executed when the prim is clicked can be found from file ScriptEngines\PythonScript\Samples\sampleobject.py
Changing the python script for a prim
NOTE: Functionality different / still missing from Naali
Continuing from the last mini-tutorial, changing what python class a prim uses is very easy.
- Right click the prim created in the last mini-tutorial and choose edit
- Go to reX tab page, replace the old Class Name with sampleclientscripting.ClientScripting and press enter
- Close build menu and touch the object again. The prim uses now the the new class and demos the client scripting capabilities. Touch the object many times to go through all the demos of clientscripting.
Creating your own python script
It's recommended that people who develop python scripts create new folders to the PythonScript folder to hold their own scripts. So your own folder contains all your own scripts or all script of a particular module. This way the name conflicts should be minimum, the scripts are organized in a nice way and you can find your own scripts quickly. So here we go, let's start creating our own scripts.
- Make sure the server is down and the client is down. This is because the server doesn't load new script folders automatically.
- Create a new folder named "MyScripts" to ScriptEngines\PythonScript folder
- Create a new file named FirstScript.py to the MyScripts folder.
- Open file FirstScript.py and add the following python code to it:
import rxactor
import rxavatar
import sys
class SayHello(rxactor.Actor):
def GetScriptClassName():
return "FirstScript.SayHello"
def EventTouch(self,vAvatar):
str = self.llGetObjectName() + " was touched in region "+self.llGetRegionName() + " by " + vAvatar.GetFullName()
self.llShout(0,str)
- Create a new file named __init__.py to the MyScripts folder
- Open __init__.py and add the following code to it:
print "Python:MyScripts init"
import FirstScript
- Now follow the "How to define a python script for a prim" mini-tutorial on how to define python script for a prim. Basically start the system, create a prim and as it's Class name put "FirstScript.SayHello". Then close the build menu and touch the object. The object should shout you back some text.
Modifying script
Modifying script and reloading it is very easy if you have access to the server's console. As an example, let's change the text that the object says in continuing from the previous mini-tutorial.
- While server and client are still running, open FirstScript.py and modify the str = ... line to the following:
str = vAvatar.GetFullName() + ", why did you touch me?"
- Then to the server's log type "python restart" and press enter which will restart the script engine. The log should read "Python: Started script read" when the script engine has restarted.
- Back in the client touch the prim again. The prim should shout the new text to you.
|