Notifications
Clear all

[Closed] Python + MXS

Yep thats me!

Ok just wipped up some small examples of using pyro for networking.

First up the server:

"""
   	Simple Pyro Example. 
   	Here is the 'Server' code.
   	
   	Written By David Mackenzie
   	dave@daveandgoliath.com
   	
   """
   
   import Pyro.core
   import socket
   
   
   class some_server(Pyro.core.ObjBase):
   	"""Pyro Server, handles the network transmission"""
   	def __init__(self):
   		Pyro.core.ObjBase.__init__(self)
   		self.ServerName = "CgTalk Example Server"
   	
   	def Ping(self):
   		"""Ping"""
   		return "%s Checking In" % (socket.gethostname())
   	
   	def DoSomeStuff(self, the_int):
   		return the_int*5
   	
   	
   if __name__ ==  "__main__":
   	daemon = Pyro.core.Daemon(port=7756)
   	Pyro.core.initServer()
   	uri = daemon.connect(some_server(), "cgTalk") 
   	daemon.requestLoop()

Now for a simple client to connect to the server:

"""
   	Here is the client code that demonstates connecting to the server and  calling a remote method.
   """
   
   def connectToServ():
   	cgConn = Pyro.core.getProxyForURI("PYROLOC://%s:%s/%s" % ("127.0.0.1", 7756 , "cgTalk"))
   	cgConn._setNewConnectionValidator
   	return cgConn
   
   if __name__ == "__main__":
   	conn = connectToServ()
   	print conn.Ping()
   	print conn.DoSomeStuff(10)

And here is a simple com class that connects to the server:

"""
   	Here is a com example that we could call from max or anywhere that has com access. Please Note I have not tested the following code.
   """
   
   import win32com.server.register
   import Pyro.core 
   
def connectToServ():
   	cgConn = Pyro.core.getProxyForURI("PYROLOC://%s:%s/%s" % ("127.0.0.1", 7756 , "cgTalk"))
   	cgConn._setNewConnectionValidator
   	return cgConn
 
   class cgTalk_Com:
   	_public_methods_ = ['Ping', 'DoSomeStuff']
   	_reg_progid_ = "cgTalk.PyroExample"
   	_reg_clsid_ = '{59EA2ED9-8DAA-4092-8BD4-3A6069D3074B}'
   	
   	def __init__(self):
   		self.Connection = connectToServ()
   	
   	def Ping(self):
   		return self.Connection.Ping()
   	
   	def DoSomeStuff(self, some_int):
   		return self.Connection.DoSomeStuff(10)
   	
   
   if __name__ ==  "__main__":
   	win32com.server.register.UseCommandLine(cgTalk.Com)

Hope that helps, sorry for not getting this up earlier I have been swamped lately… I have tested the server and simple example. I have not tested the com class, if you have any problems just post them up.

Cheers
Dave

Thanks Dave, really appreciated. This should be wikied!

Agreed, I have to get off my @ss and get this stuff written up

Cheers
Dave

1 Reply
(@erilaz)
Joined: 11 months ago

Posts: 0

Nah, I get the busy thing. Look how tardy I am at getting maxscript challenges written up!

Oh, and for those that care, I just made an obscure python joke in this thread:
http://forums.cgsociety.org/showthread.php?p=5185190#post5185190

Hot damn I love xkcd.com

Just found this great converter: XML to Python Object class. Thought I’d share as many of us use xml. I spent hours learning the parsed XML syntax and trying to get the data out… then this happened… I love this python stuff…

xmlobject.py

http://www.freenet.org.nz/python/xmlobject/

Thanks heaps. This will definatly come in handy

there’s also elementtree. Anyone have experience with both?

1 Reply
(@cgbartosz)
Joined: 11 months ago

Posts: 0

cElementTree is the same as elementtree but written in C so it’s fast.
It is about 10 times faster then XMLObject. See attachement.
XMLObject is a wrapper of standard python dom.minidom library.
Lxml or cElementTree libraries are both fast and easy to use.

Hi Guys,

XMLObject is very cool, I love SQLObject to… While we are talking about XML here is XMLClass(s) that I use all the time, I have also recreated it in MXS wrapping the dotnet objects the max and python version are virtually identical.

Cheers
Dave

1 Reply
(@theiviaxx)
Joined: 10 months ago

Posts: 0

I assume this is max 9+

As for SQLObject, i have my reservations about it and SQLALchemy. What benefits does it provide over using SQL?

Great stuff Dave. I hope I can get some time to pull it apart to see how it all works.

Does anyone have a library that makes writing maxscripts pythonic? I’m thinking of starting opensourced work on making a python library for writing maxscripts instead of simply passing raw maxscript commands over COM. I might just be extending CgKit since it has functionalities for working with 3d objects.

Page 16 / 26