personal blog

IronPython

OpenMetaverse (formerly known as libsecondlife) Bot in IronPython

Posted by on Feb 8, 2009 in IronPython, Python, SecondLife | 0 comments

The OpenMetaverse documentation seems very dire in any up to date examples, especially in mono and non-existent for IronPython.

After few minutes of playing around with it let me share with you an example how to start off with making your own bot in SecondLife using OpenMetaverse lib and IronPython:

First download and install Mono and OpenMetaverse( just follow the official instructions here )

When you finish this step you should be able to go to the bin/ directory within the folder where you’ve unpacked OpenMetaverse and see many .exe files. If you’re a Windows user just try to play around with them, if you’re on MacOSX or Linux try running them from the console by typing: mono program_name.exe

That’s the basic requirement. If you’re able to get pass that point you’ll need to install IronPython.

Try running command ipy from your console, you should see:

$ ipy IronPython 1.1 (1.1) on .NET2.0.50727.42 Copyright (c) Microsoft Corporation. All rights reserved.
>>>

Now the fun bit. Writing the actual bot.

Let’s follow the official example which logs into the SL grid, says hello and exits.

import clr
from System import *
from System.Text import *
from System.Collections.Generic import *

clr.AddReferenceToFile("OpenMetaverse.dll")
from OpenMetaverse import *

first_name = "John"
last_name = "Doe"
password = "pwd1234"

client = GridClient()

def Network_OnConnected(sender):
   print "I'm connected to the simulator...\n" # or Console.WriteLine("I'm connected to the simulator...")
   client.Self.Chat("Hello Grid!", 0, ChatType.Normal)
   Console.WriteLine("Now I am going to logout of SL.. Goodbye!") # or print "Now I am going to logout of SL.. Goodbye!\n"
   client.Network.Logout()

client.Network.OnConnected += NetworkManager.ConnectedCallback(Network_OnConnected)

if( client.Network.Login(first_name, last_name, password, "My First Bot", "Your Name")):
   print "Logged in\n"
else:
   print "I couldn't log in, here's why: %s" % client.Network.LoginMessage


Read More