• 1/3

My First Forays into .NET and IronPython from MAXScript

Posted by Christopher Diggins, 21 March 2009 3:11 am

16 Comments

Yannick Puech

Posted 21 March 2009 8:06 pm

Thanks for this very interesting post Christopher.
The .NET framework is very powerful and with IronPython even more!
I'm interested in your managed C++/CLI assembly because I'm also doing .NET stuff with Max. I think we can do lot of things in MAXScript and the SDK that were not possible before.

nw42

Posted 1 April 2009 12:34 am

Great thing to see action on the 3dsmax & Python topic - I would really welcome to see some more Python support in 3dsmax...

thanks

nw42

loocas

Posted 10 April 2009 3:20 pm

Very interesting! Thanks a lot! I too hope to see Pythong finding its way into 3ds Max, it's such a powerful language with all those extensions it shouldn't be ignored! Just like Maya did get its Python sweetness!

Switcher

Posted 13 May 2009 5:30 pm

can't wait for this dll to come out !

thanks !

loocas

Posted 13 May 2009 8:19 pm

Fantastic news, Chris! I can't wait to get Python in Max, that'll be revolutionary and will allow for so much in regards of data sharing and cooperation with Maya, XSI and MotionBuilder!

Please, keep the great work up!

loocas

Posted 13 May 2009 11:58 pm

Unfortunately, when I try to replicate this procedure, I bump into a problem with:

PythonEngine = dotNetClass "IronPython.Hosting.PythonEngine"

resulting in: undefined

Do you know why that might be? Is it because IronPython isn't among my PATH variables? (installed only the bin version) Or is it something else?

Christopher Diggins

Posted 14 May 2009 12:07 am

[EDIT: Previous bad advice deleted.]

BTW: thanks everyone for the encouragement! I won't be able to get back to working on this for a few months (other products calling, and I have paternity leave coming up). However, I will get back to this as soon as I am able.

loocas

Posted 14 May 2009 12:14 am

Hey, Chris, thanks a lot for your prompt reply, much appretiated!

However, I wasn't able to fix the "undefined" problem.

I didn't install IronPython, only unzipped the binaries, that's why I thought at first that it was related to the PATH env. variable. However, not even after copying IronPython.dll to the root of 3ds Max was I able to make it past the two initial lines of code.

I simply cannot define the "IronPython.Hosting.PythonEngine" class it says "undefined" every time. I even tried to derive the Engine from the DLL itself, as you suggested through:

PythonEngine = dotNetClass "IronPython.dll"

Same result: undefined

Christopher Diggins

Posted 14 May 2009 12:27 am

Okay sorry I wasn't thinking straight, ignore my earlier post. I think what happened is you are missing the first two lines:.
My blog was a little misleading. Combine the first two steps (and replace the path to wherever IronPython is on your machine) and you should be good to go:

Assembly = dotNetClass "System.Reflection.Assembly"
Assembly.LoadFrom "C:\Program Files\IronPython 2.0.1\IronPython.dll"
PythonEngine = dotNetClass "IronPython.Hosting.PythonEngine"
engine = dotNetObject PythonEngine

Let me know if this works.

loocas

Posted 14 May 2009 12:33 am

Unfortunately, I wasn't missing the first step. I followed every step you took and I'm trying to run this code exactly:

Assembly = dotNetClass "System.Reflection.Assembly"
Assembly.LoadFrom "O:\RnD\Python\IronPython\IronPython-2.0.1\IronPython.dll"

PythonEngine = dotNetClass "IronPython.Hosting.PythonEngine"
engine = dotNetObject PythonEngine

I get: undefined for the part where I try to instantiate the PythonEngine class, after that an error for the engine instantiation.

I can substitute the PythonEngine string with a full path to the .dll, but to the same result.

I'm running a 64bit version of Win Vista Business (as an Admin), and a 64bit version of 3ds Max 2010 if that changes anything...

Christopher Diggins

Posted 14 May 2009 12:42 am

Hi Loocas, I am at a loss. I can't see why it doesn't work. I might suggest using the Visual Studio object browser to look at your DLL to see if you can see the classes in it. Also I'd suggest try running the installer. It may turn out that you can't open the assembly unless it is properly registered with the global assembly cache (GAC). To be honest, I am starting to get out of my depth here. .NET assembly installation is beyond my area of expertise. Good luck!

loocas

Posted 14 May 2009 12:52 am

Unfortunately even installing IronPython via the provided .msi installer didn't resolve the issue I'm having

I'll try to investigate further, but this is sad as I was really hoping to get Python code up and running from within 3ds Max even without the luxury of being able to access Max classes and objects, still it'd be a huge step forward for my studio.

Thanks a lot, however, for your time and efforts, at least your technique brought me on this path for which I'm really grateful! Thanks!

If you'll be at Siggraph this year, I'll buy you beer

Yannick Puech

Posted 14 May 2009 2:11 am

Hi all,

I have been able to use IronPython in Max very quickly but at the beginning I was not been able to run Chris code too. Using Reflector tool (http://www.red-gate.com/products/reflector/) I was able to find how to create an IronPython engine and run a script.

Here is my code to run an IronPython script from MAXScript:

(
Assembly = dotNetClass "System.Reflection.Assembly"
Assembly.LoadFrom "C:\IronPython-2.0.1\IronPython.dll"

ironPythonHosting = dotNetClass "IronPython.Hosting.Python"
pythonEngine = ironPythonHosting.CreateEngine()
pythonEngine.ExecuteFile("C:\Code\Python\maxFormTest.py")
)

And the maxFormTest.py example:

import clr
path = "C:\\Program Files\\Autodesk\\3ds Max 2009\\"

clr.AddReferenceToFileAndPath(path + "MaxCustomControls.dll")

import MaxCustomControls

class maxFormTest(MaxCustomControls.MaxForm):

def __init__(self):
self.Text = "MaxForm Test"
self.Name = "maxFormTest"

maxForm = maxFormTest()
maxForm.ShowModeless()

Christopher Diggins

Posted 14 May 2009 2:14 am

Thank you very much for sharing that Yannick!

loocas

Posted 14 May 2009 2:53 am

Yannick, this is beyond awesome!

It works! Thanks a lot mate!

By the way, is there a way, out of curiosity, to execute Python code directly? I mean without the need of saving it in a .PY file and executing it from the file? I tried the .Execute() method, however, it doesn't work as I expected. For example:
pythonEngine.Execute("print 10") returns:
-- Runtime error: dotNet runtime exception: Late bound operations cannot be performed on types or methods for which ContainsGenericParameters is true.

Do I have to convert the string into a dotNET compatible string or is there something else?

Btw: thansk for the .NET Reflector tip, it's great!

Snoelk

Posted 18 October 2010 12:11 pm

im trying to create some stuff via dotnet but i cant find anything about this cool MaxWrapper dll you mentioned in your thread. how you created it ? is there the possiblity to share this dll ?
right now im trying to attach to dll shiped within max. but how you can expose the scene directly to .net and write your stuff directly within c# ?

Add Your Comment

You must be logged in to post a comment. Login here or Register

Please only report comments that are spam or abusive.