Python + MXS

Become a member of the CGSociety

Connect, Share, and Learn with our Large Growing CG Art Community. It's Free!

REPLY TO THREAD
 
Thread Tools Search this Thread Display Modes
Old 07 July 2007   #46
Pyro is a Python Remote Object library written in python itself. Basically it allows python code segments to sit on a centralized server and clients can execute the code from anywhere on the network. You can think of it as distributed COM for python. More info and documentation can be found here http://pyro.sourceforge.net/

Twisted is a Python event driven network engine also written in python. I'm not that familiar with it only know that it's used as a python based web framework although I prever Django. Maybe someone else can elaborate on how this fits into their pipeline and maxscript. The projects home page is http://twistedmatrix.com/trac/.
__________________
-Dave
 
Old 07 July 2007   #47
Ok, how does Pyro differ them setting a path to a folder on the server were you have modules that can be used? I might be missing something major here.
__________________
Paul Neale
http://paulneale.com
 
Old 07 July 2007   #48
You could definitely have modules on the network that you access from each station, but the code is still basically executed on the station that called it. Pyro lets you call methods and props on an object just as if it were local, but the processes happen on the server remotely.
__________________
- d3coy.com -
technical articles, scripts, and gallery
 
Old 07 July 2007   #49
Originally Posted by d3coy: You could definitely have modules on the network that you access from each station, but the code is still basically executed on the station that called it. Pyro lets you call methods and props on an object just as if it were local, but the processes happen on the server remotely.


Sorry for my ignorance, what is the advantage of this?
__________________
Paul Neale
http://paulneale.com
 
Old 07 July 2007   #50
I think for a lot of tasks it wouldn't matter. If you have multiple users making queries to a sql db then the db server is going to handle the multiple requests at once for you anyways. But if you have some methods that make file system traversals or even more potentially volatile, executing MXS on scene files, you might want to have a remote object handle these things so these processes don't potentially overlap from multiple users, causing who knows what kind of havoc... But if you're the only one that'll be executing this kind of code, there isn't a need for a server like that.
__________________
- d3coy.com -
technical articles, scripts, and gallery

Last edited by d3coy : 07 July 2007 at 04:32 PM.
 
Old 07 July 2007   #51
I think that I'm starting to get this. I will be looking into this more as this project moves forward into the next phases so I'm sure that I will have more questions then.

Thanks for your time.
__________________
Paul Neale
http://paulneale.com
 
Old 07 July 2007   #52
Originally Posted by RealThing: Pyro is a Python Remote Object library written in python itself. Basically it allows python code segments to sit on a centralized server and clients can execute the code from anywhere on the network. You can think of it as distributed COM for python. More info and documentation can be found here http://pyro.sourceforge.net/

Twisted is a Python event driven network engine also written in python. I'm not that familiar with it only know that it's used as a python based web framework although I prever Django. Maybe someone else can elaborate on how this fits into their pipeline and maxscript. The projects home page is http://twistedmatrix.com/trac/.


For me the only reason to use twisted (realtive to our 3d work) is for its asynchronous networking, its dead easy to write tcp servers. Also the Perspective Broker module is a really easy way to create authenticated RPC servers. Although for simple networking tasks the ease of pyro is second to none.

Anyways here are some example of using Pyro, just remeber these are quick hacks at best, if you were to implement something like these there would be allot of error handling etc one would want to do!

now for the code, here is a simple pyro server, I have pulled this code out some other apps so there is some stuff that does not need to be there etc... for example this server does not need to be run on a therad etc...


  """
  	Really Simple Example of Pyro Server...
  	I have pulled lots of code out of this, hope its not to spaghetti like!
  """
  
  import Pyro.core
  import Pyro.naming
  import threading
  from Pyro.errors import PyroError, NamingError
  
  ServPort = 9001
  
  """
  Pyro Proxy, subclassed from Pyro.core.ObjBase, this class will be callable,
  from connected clients.
  """
  
  class CameraServer(Pyro.core.ObjBase):
  	"""Pyro Proxy"""
  	def __init__(self):
  		Pyro.core.ObjBase.__init__(self)
  	def HelloWorld(self, name):
  		if type(name) == str:
  			print "Hello, %s" %(name)
  			return True
  		else:
  			return False
  
  """
  Run server on a thread (pyro is multi threaded,
  I normaly start pyro on a new thead when im using it with a gui.
  """
  
  class CreateServer(threading.Thread):
  	"""Server Thread"""
  	def __init__ (self, **kwds):
  		self.daemon=Pyro.core.Daemon(port = ServPort)
  		threading.Thread.__init__ (self, **kwds)		
  		self.setDaemon(0)
  		self.start()
  	def run (self):
  		"""Start Pyro Server"""
  		Pyro.core.initServer()
  		uri=self.daemon.connect(CameraServer(),"CameraMan") 
  		self.daemon.requestLoop()
  
  if __name__=="__main__":
  	#Start the Server	
  	a = CreateServer()
  
  


Now here is a client that will call the method on the server,


  
  import Pyro.core
  import Pyro.naming
  from Pyro.errors import PyroError, NamingError
  
  ##Connect To Pyro Server, needs the IP, Port and the Remote Method Name as a string. Will return a Pyro Proxy
  def ConnectToPyro(ServIp, ServPort, ServMethod): 
  	PugNet = Pyro.core.getProxyForURI("PYROLOC://%s:%s/%s" % (ServIp, ServPort, ServMethod ))	   
  	PugNet._setNewConnectionValidator	  
  	return PugNet
  
  if __name__ == "__main__":
  	con = ConnectToPyro("127.0.0.1", 9001, "CameraMan")
  	print con.HelloWorld("CgTalkers")
  	
  
  


I hope that show just how powerful this can be when used in the right circumstances... Here is an example using the COM class i posted earlyer, I have not had time test this code... Its worth mentioning this class is real hack ill post up one thats written "properly" in the next couple of days.


  
  ##Connect To Pyro Server, needs the IP, Port and the Remote Method Name as a string. Will return a Pyro Proxy
  def ConnectToPyro(ServIp, ServPort, ServMethod): 
  	PugNet = Pyro.core.getProxyForURI("PYROLOC://%s:%s/%s" % (ServIp, ServPort, ServMethod ))	   
  	PugNet._setNewConnectionValidator	  
  	return PugNet
  
  
   #A simple Python COM server, as simple as it gets.
  class PythonComClass:
  	_public_methods_ = ['SplitString', 'ConnectToPyro', 'QueryPyroMehtod']
  	_reg_progid_ = "PythonDemos.Utilities"
  	#Make sure you generate a new Class ID, use pythoncom.CreateGuid()
  	_reg_clsid_ = "{41E24E95-D45A-11D2-852C-204C4F4F5020}"
  	
  	def SplitString(self, val, item=None):
  		"""Splits a string"""
  		import string
  		if item != None: item = str(item)
  		return string.split(str(val), item)
  	def ConnectToPyro(self, serv, port, meth):
  		try:
  			self.PyroConn = ConnectToPyro(serv, port, meth)
  		except:
  			print "Error connecting to Pyro"
  	def QueryPyroMehtod(self):
  		"""Talk to Pyro"""	
  		return self.PyroConn.SomeRemotMethod("Some Data")
  	def __PrivateMethod(self):
  		return "This is a private method"
  	
    
    
    #Make sure this module has not been imported. Register COM server
  if __name__=='__main__':
  	print "Registering COM server..."
  	import win32com.server.register
  	win32com.server.register.UseCommandLine(PythonComC    lass)
  


Hope thats useful...

Cheers
Dave
 
Old 07 July 2007   #53
Thanks a ton spec, I'll give the com class a go and report back how I faired (I know you said it was quick code, I'll give it a shot anyways )
__________________
- d3coy.com -
technical articles, scripts, and gallery
 
Old 07 July 2007   #54
Originally Posted by d3coy: Thanks a ton spec, I'll give the com class a go and report back how I faired (I know you said it was quick code, I'll give it a shot anyways )


If you have any trouble post the tracebacks and ill get on it, "Hopefully" ill get time this weekend to write these up properly for everyone... Using the COM class can be hit and miss it can require a restart of the machine after having had made changes etc... I typically call the class and debug it before registering it as COM service.

Cheers
Dave
 
Old 07 July 2007   #55
Thanks for this post, seems to be promising. However I also see that MXS has .NET capabilities now so i'm kind of curious if there are serious limitations to python or .net. I understand this sounds like a x vs. y question :( Try to be objective. Can the two languages be used in relatively the same way?
__________________
"When sales determine status, I hold no faith in the majority" -El P

Draenei:Thread
Troll:Thread
 
Old 07 July 2007   #56
i'm kind of curious if there are serious limitations to python or .net. I understand this sounds like a x vs. y question :( Try to be objective. Can the two languages be used in relatively the same way?


For the most part I would say yes and that it depends.

If you're only concern is 3dsmax running on Windows then maxscript and .NET should offer you alot of possibilities.

You can't actually compare Python to .NET as one is a scripting language and the other is a framework. In over all power Python has alot of libraries which allow you to do anything that you can do within the .NET framework and then some. If you choose to use IronPython you also have full access to the .NET framework from within Python as well but it's not as portable (windows only).

So currently the only advantage .NET has is that the bridge between it and maxscript is already available in 3dsmax. But since Python is now built into a growing number of 3d and compositing apps and alot of studio's have pipelines built on or around Python connecting 3dsmax to Python is attractive. It also provides a portable middle ground for common code. So that a maxscript in 3dsmax running on windows can call the same python functions as a similar script in maya or nuke running on windows or linux.

Python has been around for 16 years and has been used in pipelines for most of those. So I would say that Python is production proven. http://www.python.org/about/success/ilm/

On the other hand my experience with the .NET framework on windows has been that the framework is pretty fragile. We use Deadline at work which relys on the .NET framework and we have had to go through the pleasure of removing and reinstalling the .NET framework on a couple of workstations to get things to work correctly after they suddenly stop working for no reason. Luckily the process is documented in MS's knowledge base but it involves manually removing a bunch of files, directories, and registry keys. Followed by reinstalling the framework.

Hopefully that avoided the whole x vs y thing as much as possible.
__________________
-Dave

Last edited by RealThing : 07 July 2007 at 03:59 AM.
 
Old 09 September 2007   #57
Python & MS

This is a really useful thread guys, thanks. We're running into a lot of the same issues at Blur, we use python for almost all our external tools & tools within XSI (since it supports it) but for all our MAX stuff we're still using MS. For us, it seemed the things that were most useful from Python to tie everything into our pipeline is object-oriented classes, easy XML read/write, and regular expressions - all things which maxscript lacked.

We've actually come up with some useful stuff for improving the power of scripting that mimics the python structure, while keeping it all inside 3dsMax Script so you don't have to work through COM. The one that I've found most helpful in creating more advanced apps inside maxscript is the OOP model, it allows you to create classes within maxscript that can be inherited from, and passes in itself as its first variable to a function, essentially be wrapping the struct Value inside of a classDef (another value that exposes all the same features of a class in python): eg:

local classA = classDef #ClassA (
struct _ClassAStruct (
data = undefined,
name = "",
function __init__ self name data:undefined = (
self.name = name
self.data = data
)
)
)
local classB = classDef #ClassB (
struct _ClassBStruct (
function __init__ self = (
ClassA.__init__ self "MyName" data:10
)
)
) baseClass:( classA )
local b = classB()
print b.name ---> "MyName"
print b.data ---> 10

--------------------------

its really been invaluable to me in making more complicated maxscript tools. it works almost exactly like python, and while it doesnt port directly externally, it is similar enough that if we need the same functionality internally of max & externally from max it can be re-written almost 1-1.

The other is the regular expression engine, and that was simply a matter of wrapping the .Net RegularExpression library in a DLL.

We're really trying hard to get the Blur Beta site back up, its our intention to release these DLL's publically. I'll let you know as soon as we do, but if anyone has any suggestions based on that example, or has any questions, lemme know. I hope that helps a bit!
 
Old 09 September 2007   #58
also

also, if anyone is interested in the DLLs for this, I can talk to my supervisor and see about putting just those up until the beta site is up. They've been extremely helpful for me in programming and I think could be really helpful for everyone else as well.
 
Old 09 September 2007   #59
Hi Eric,

if you need somewhere to put them until the BlurBeta site is back, let me know.

Dave
__________________
MAX Plugins.de - the database for 3ds max plugins
 
Old 09 September 2007   #60
Hi Eric,

I love where you guys are headed with this, currently I'm using A LOT of COM to get max talking to the rest of our pipeline, consequently anything that gets me away from COM I am very interested in!

The OOP example you showed below is very exciting, I can think of a dozen scripts and tools that i have written that would immedieatly benifit from this... Nice work, and I can not wait to try the dll's when blurbeta is back up.

Cheers
Dave
 
reply share thread



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
CGSociety
Society of Digital Artists
www.cgsociety.org

Powered by vBulletin
Copyright 2000 - 2006,
Jelsoft Enterprises Ltd.
Minimize Ads
Forum Jump
Miscellaneous

All times are GMT. The time now is 01:04 AM.


Powered by vBulletin
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.