Notifications
Clear all

[Closed] Python + MXS

Hi, Guys, sorry this is bit late but its been one of those weeks. Better late than never I guess. Anyways there a a couple of ways to get python to talk to max and vice versa. The simplest way being COM. This of course requires that you have max registered as a COM server and that you have some MXS function visible to COM (if you have not used com before check the Example scripts on 3ds max disc for getting com going, very easy to do).

   First off the code im posting is the most simple of examples! but it should give you a glimpse of what could be possible, which is allot.
   
   Fist off create a really simple function in max:
    fn DoSomething obj = (
          try(
               execute obj
               return true
           )catch(
               return false
               )
       )
       registerOLEInterface #(DoSomething)
   As you can see there is a ultra simple function that will just execute whatever is parsed to it. Now for the Python part, this will require the win32 module.
#Import win32com.
  import win32com.client
       
  #Create a connection to Max
  conn = win32com.client.Dispatch("MAX.Application.9")
       
  #Flag as a Method otherwise python or win32com will most likely treat it as attribute.
  conn._FlagAsMethod("DoSomething")
       
  #Call 'DoSomething' 
  #obj.DoSomething("Box()") Thanks to Joel Hooks for pointing out the typo..
       
 conn.DoSomething("Box()")
   With any luck you should have a box sitting in the middle of you view port. As I said above this is a very simple example, I will post some more exciting ones! Its worth mentioning that python can be called from max in much the same way, although the best* work flow (at least i think so) is to create a python server, which listens for and queries max, this has some awesome work flow implications, ie lots of artists working on the same job it becomes very easy all the sudden to keep things in sink like camera animation for example... did someone say camera server? 
   
   Hope thats enough to get you guys started, as I said I will post more stuff soon!
   
   Cheers
   Dave
313 Replies

Thanks for setting this up Dave. Really appreciated. Can you do a copy/paste of your python resources post from the challenge thread here?

I sure can:

I use python for all manor of things we have written full feature project management apps, asset browsers and a host of other bits and pieces. In terms of resources I must admit my first port of call is python.org, however I do like the oreily books “Programing Python” comes to mind. I would also recommend the Python help file that comes with the Active State distribution of python (I also think there IDE is the best, Komodo, wich is now free).

There also so some modules that I like and use all the time, I found these ones come in handy all the time:
[ul]
[li]PIL, this imaging lib is awesome I have written some really cool asset browsers that have utilised PIL, http://www.pythonware.com/products/pil/ [/li][li]Pyro, or Python Remote Object, I love this module for simple networking tasks, RPC and distrusted process’s, http://pyro.sourceforge.net/ [/li][li]Twisted, is another networking module, its a bit big (theres an entire book on it), but it has some useful parts…[/li][li]WxPython, this is the BEST gui lib for python, its wrapped around the C lib so its not so ‘pythonic’ but once your used to it or wrapped it you can build very feature rich apps very quickly and its very well documented… http://www.wxpython.org/ [/li][li]PyWin lib is awesome it provides access to just about everything in windows, including com/ole so working with max becomes very easy. You can do some very cool stuff with it. I don’t have a link as I have no idea were i downloaded it, I know Mark Hammond wrote it (most of it??) I do believe its built into the Active State distribution that I mentioned above. (If anyone wants it just pm me)[/li][/ul]Hope that helps, I think allot of people (maxers at least) tend to skip over python, but it is very easy to work with python in max and have python work with max (Pyro = back burner replacement)…

 rdg
did someone say camera server?

What’s a camera server?

Thanks for this introduction. I’m not as biased against winOS like maybe others. But OLE somehow got a ‘bad bad’ tag. I will have to reevaluate this.

Having a queque and a lockfile on your server that keeps track of requests could be a way to build a ‘remote scene compositor’ where the ‘artists’ could be just monkeys spread around the world.

Sidenode:
What are the legal consequences of such setups? I think most plugins aren’t licensed for server setups?

Anyway:
I heard that other applications use/will use python as well – so it is propable the middleware of choice for pipelines. Though I miss the {} …

Georg

hi,

personnaly, i use IronPython for developpe Dotnet assembly and use in 3dsmax and communicate with maxscript

less problem than OLE communication

but i don’t if all “classic” python librairy can be use with ironPython

Kermit

Thanks David for the simple but very interesting example.
Yes, Python is one of the best scripting language.

I’ve also read an article about using 3ds Max as OLE Server (show how to communicate between XSI and Max) : http://www.sloft.net/2006/11/26/how-to-make-xsi-and-max-friends/

Hi Dave,

Thanks for starting this off. Muchly appreciated! there goes my weekends!

ezza, can we have a sticky please?

Josh.

 PEN

I wish that I had weekends to dedicate to this…darn.

This is a great start. I have known this was possible for a long tim but I have never look into how to do it. Time to start I guess.

Can this thread please be made sticky! It is a very important solution for Max houses that need complex pipelines. Which is exactly what I’m in the process of setting up.

I wish that I had gone with Python in the first place an not learned Perl. ah well.

1 Reply
(@erilaz)
Joined: 10 months ago

Posts: 0

What the Paul wants, the Paul gets.

Same here. Not that I don’t have a billion other things to be learning right now, but this is too good to pass up.

 PEN

Ok then…a three day work week, paid for six. Am I pushing it now? Thanks for making it sticky.

I just had a look at setting up OLE and that looks stright forward.

I’m looking at the example scripts, Funny…I didn’t know that PEN Attribute Holder still shiped with Max:) Any way I can’t find the COM scripts? What are they called? This is the Max 9 disc.

And, does 64 bit change any of this?

Sorry Paul, which COM scripts are you referring to?

1 Reply
(@ypuech)
Joined: 10 months ago

Posts: 0

Maybe ole.ms and oledemo.xls (“Running the OLE Demo” in MAXScript Reference). Can’t find them to.

Page 1 / 26