Python + MXS


#1

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

#2

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


#3

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)…


#4
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


#5

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


#6

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/


#7

Hi Dave,

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

ezza, can we have a sticky please?

Josh.


#8

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.


#9

What the Paul wants, the Paul gets.:smiley:

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.


#10

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?


#11

Sorry Paul, which COM scripts are you referring to?


#12

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


#13

oh ,…

at last i can help Paul and Yupech :stuck_out_tongue:

Samples\Scripts\Examples


#14

I believe that pyWin can be found here:
http://sourceforge.net/projects/pywin32/


#15

Great, I have it. Like I have time for this!! Damn you, damn you all to hellllllllllllllllllllllllllllll! Now I just need a quite beach, were the rest of humanity has been whipped out and the bank and tax man isn’t looking for me so that I can sit down and learn some of this. The talking apes might become of bit of a distraction.


#16

Please pardon my eternal ignorance, but what is the advantage to this Python/MXS tie in? I haven’t worked with Python before, what can it do that MXS can’t?

Any example applications? In other words.

Thanks.


#17

Hi, Guys,

Fist off, Camera Server that I mentioned is a simple little app im working on, that will keep animated cameras in sync. So an animator can be animating the camera and lets say a lighter is lighting the scene then his camera will have a live link to the animators etc… If that makes sense?? I am hoping to finish it ‘this’ week and ill post all the source for everyone to check out hopefully there will no be to much spaghetti code in there :slight_smile:

Anyways to answer a couple questions:

Wahooney, Python can’t really do more then Maxscript in fact its the other way around more that python is limited to what mxs can do, were Pythons power lies is in its ability to “tie it all together” which is why its so good for building pipelines with…

PEN, I second that a quite beach were i can sit down with laptop and gets some stuff done… wait quite beach, mmm forget the laptop…

ypuech, I had actually read that link a while ago, It does however show the power of com when used to communicate through software…

Kermit, I have only played with IronPython a little bit (IronPython is a version “distro” of python that runs on the .Net virtual machine (? I think so) ) I thought the standard library was implemented? I’m not sure if Iron python makes it easier to add python scripting to a dot net app, maybe ypuech could shed some light on this?

If there any specific examples of python scripts you guys want just let me no… I am hoping to be able to show you an example of a render manger written in python (for vray standalone, but could easily add the functionality for different renders etc…) but thats a little ways off yet.

Cheers
Dave

More code coming this week!


#18

Hi,
i’ve test succefully to implement a dotnet dll ( write in ironPython ) and integrate with in a maxscript rollout (thx to Yannick Puech for tutorial )

i need to try some Net 3 ( XAML ) dll for better UI ( like character vector drawing for rigging ), always with IronPython

the help tutorial mention u can import standart python library, but i do not test
i need more free time for this, it’s only a idea for your post . the big advantag i see is the communication between dontnet and mxs implemente by Autodesk

Kermit

ps : maybe difficult to read me, my english is very bad, sorry


#19

Wahooney, Python can’t really do more then Maxscript in fact its the other way around more that python is limited to what mxs can do, were Pythons power lies is in its ability to “tie it all together” which is why its so good for building pipelines with…

I’d say:
Python’s power is the one of a middleware.
All the c#/.net stuff is limited to winOS. Using python you can implement osindpendend clients and data parsers.
This might be based on thin ice but:
Python is definitely a stronger string and xml parser than maxscript.
I used python in the past mainly in peparing data for indesign and illustrator.


#20

Yes, you can extend .Net applications with ironPython.

I’ve only heared a little bit about ironPython. Seems powerful. It’s very useful to be able to create a managed assembly and use it under 3ds Max.