Maya API docs demystified for Python users Got muscles? Fight Night Round 4
Feb 24

I saw users on www.cgtalk.com talking about this and I thought I would post it to show how Python is bridging the gap not only between MEL and the C++ API in Maya. But Python is bridging the gap between software as well.

Here are the steps to make Python work in 3ds Max.

# Step 1: Download Python Version x.x any version at http://www.python.org/download/
# Step 2: Download and install pywin32 which has the win32com python module in it here: http://sourceforge.net/projects/pywin32/
# Step 3: Make sure to download the win32com for the python version you have downloaded
# Step 4: Open up the Python IDLE Shell and select File\New Window
# Step 5: Copy and paste this code, written by David Mackenzie and posted on cgtalk.com:

/*
  	Just a quick hack to demonstraight some python in max. This is a very script on both ends.
  	If there is any interest I will develop it further.
  		crafted by, David Mackenize

  	Notes:
  		-Any strange errors, python or max please send to me.
  		-I would recomend, where ever you would like to place a tab insert 4 'spaces'
  		-You might find that if you have 2 or more Dialogs open (of this script) the previous ones
  		will drop/loose there connection with COM.

  	Cheers
  	Dave

 */
import sys
import os
import win32com.server.register

Writing = True

def TBWrite():
	"""Returns the traceback as a string, VERY handy dandy biatch"""
	import traceback
	return traceback.format_exc()

#Catch sys.stdout, this is what would normaly be returned to the screen by python
class CatchOut:
	def __init__(self, obj):
		"""obj requires the method COut"""
		self.Obj = obj

	def write(self, data):
		"""This method catches sys.stdout, stdout expects a write method"""
		self.Obj.COut(data)
		#global Writing
		#Writing = False

class MXSPrompt:
	_public_methods_ = [ 'RunCom' ]
	_reg_progid_ = "DaveTools.MXSPrompt"
	_reg_clsid_ = '{CF24CDE3-680B-4A38-82E0-958C57921AF7}'

	def __init__(self):
		self.Buffer = ""
		self.StdOut = CatchOut(self)
		#sys.stderr = self.StdOut
		sys.stdout = self.StdOut

	def RunCom(self, commstr):
		"""Method that is visible to COM"""
		try:
			global Writing
			self.Buffer = ""
			exec commstr
			"""
			while(Writing):
				pass
			Writing = True
			"""
			return self.Buffer
		except:
			return TBWrite()

	def COut(self, dstr):
		"""Just Updates the Buffer"""
		self.Buffer = self.Buffer + dstr

if __name__ == "__main__":
	win32com.server.register.UseCommandLine(MXSPrompt)

# Step 6: Select from the menu with the Python module the menu Run\Run Module or hit F5
# Step 7: Open 3ds Max
# Step 8: Open the script editor and run this maxscript code also written by David Mackenize:

/*
  	Just a quick hack to demonstraight some python in max. This is a very script on both ends.
  	If there is any interest I will develop it further.
  		crafted by, David Mackenize

  	Notes:
  		-Any strange errors, python or max please send to me.
  		-I would recomend, where ever you would like to place a tab insert 4 'spaces'
  		-You might find that if you have 2 or more Dialogs open (of this script) the previous ones
  		will drop/loose there connection with COM.

  	Cheers
  	Dave

  */

fn GetComObj =
(
	try
	(
		o = createOLEObject "DaveTools.MXSPrompt"
		return o
	)
	catch
	(
		print "error create ole object"
		return False
	)
)

rollout pyprompt "Python Prompter"
(

	group "Py Prompt"
	(
		edittext py_out "" height:125
		edittext py_in "" height:150
	)
	button btn_submit "Submit" align:#right

	local obj = GetComObj()

	on btn_submit pressed do
	(
		t = obj.RunCom py_in.text
		py_out.text = t
	)
)

createDialog pyprompt 450 350

Once you are done executing the above code in max a dialog will pop up. Type in python code in the bottom window and hit submit and it will return your results!

Also note that Blur Studios came out with a Python plug-in for 3ds Max located on my friend Paul Neale’s web site here:
http://www.paulneale.com/downLoads/blurPython.zip I can not seem to find the plug-in on Blur’s site, it is probably in the 3ds Max package they have posted on Blur Beta.

I have not done much with this since right now but it looks like it has alot of potential for max users to use their Python knowledge. For example You could use obj.RunCom to call the Python e-mail module and send yourself an e-mail when finished with a batch in 3ds Max. Using the very same code a Maya users or someone outside of either program would use. This means the code you right does not need to be thrown away when switching to another package or rewritten when using two packages.

For further reading:

Tech Art Tiki
http://techarttiki.blogspot.com/2008/03/calling-python-from-maxscript.html

3dsMax SDK and MaxScript
http://forums.cgsociety.org/showthread.php?p=5700073#post5700073

More on COM
http://en.wikipedia.org/wiki/Component_Object_Model

This has documentation on PythonWin32
http://python.net/crew/mhammond/win3/

But if you want newer versions for Python 2.5 and 2.6 of PythonWin32 go here:
http://sourceforge.net/projects/pywin32/

Cheers,
-RyanT

Leave a Reply