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:

Continue reading »

Feb 15

Someone sent me an e-mail trying to understand more about how to read the Maya API docs. Since they are written for C++ users it can be hard to understand them unless you understand a little more about C++. So below is my rather long winded answer. The only way you would completely understand this would be to read a C++ book but I think this gives most people a basic enough understanding that they can at least know what functions they can use, what to pass to those functions, and what to expect after doing that.

First off you find the Maya API documentation here:

http://download.autodesk.com/us/maya/2009help/API/classes.html

So lets say you are using this function:

MStatus MItMeshPolygon::getConnectedEdges ( MIntArray &edges )

Which you can find in the class:

MItMeshPolygon.

Continue reading »

Feb 13

I was curious if it was possible to overload the [] operators twice in a row to imitate a matrix while still using a single array. Someone gave me an example to go off of and here is a working example of how to make a flatMatrix class in python.

Try this in the Python script tab in Maya:

class SubItem:

	def __init__(self, parent, idx):
		self.parent = parent
		self.idx = idx

	def __repr__(self):
		return repr(self.parent.data[self.idx])

	def __setitem__(self, subIdx, item):
		index = (self.idx * self.parent.col) + subIdx
		self.parent.data[index] = item

	def __getitem__(self, subIdx):
		index = (self.idx * self.parent.col) + subIdx
		return self.parent.data[index]

class flatMatrix:
	def __init__(self, data):
		self.data = data
		self.col = 4

	def __setitem__(self, key, item):
		return SubItem(self, key, item) 

	def __getitem__(self, idx):
		return SubItem(self, idx)

A = flatMatrix([0,1,2,3,
		4,5,6,7,
		8,9,10,11,
		12,13,14,15])

print A[0][0]

A[0][0] = 50

print A[0][0]

I think its kind of cool. Not so sure I dig how complex it is to read though.
-RyanT

Feb 10

I have been converting a C++ plugin to Python and I came across some C++ code that I was unsure how to convert to Python. I needed to overload several operators like +, -, <<, and [] so how does one go about this? First thing which is always best is to find the python.org help docs.

http://www.python.org/doc/2.5.2/ref/numeric-types.html

Unfortunately they dont have any good examples so I went searching for more. And here is an example I compiled after my search concluded. The lambda function might seem a bit confusing at first but it simply defines two variables ( in this case ) then it runs an expression and at the end of it, it defines what those variables are. The map() function is even more simple. map() can run a function on a list. For example:

#map() example
def add_one(a):
	return (a+1)

foo = [1,2,3,4]
foobar = map(add_one, foo)
print foobar
>>[2, 3, 4, 5]

lambda is special in that it is a function and operates on lists so its allowed to be inside of map()

#lambda example
foo = [1,2,3,4]
bar = [1,2,3,4]
foobar = lambda x, y: x + y, foo, bar
print foobar
( at 0x0000000017A9BEB8>, [1, 2, 3, 4], [1, 2, 3, 4])

And last here is the overload operators example:

Continue reading »

Feb 04

I ran into this module and just thought it was cool so I thought I would mention it here. Python has a few modules for working with processing URL’s and using ftp. So you could create a tool that you share with the public and have it read a URL which would allow you to dynamically create start up messages or even tell the user that the tool has been updated. Ahhhh.. that could get scary.

import urllib2

# Open the URL for reading
urlFile = urllib2.urlopen('http://www.rtrowbridge.com/blog/about/')

# Get info abou the URL
urlFile.info()

# Read the URL source code
for line in urlFile:
	try:
		print line
	except:
		pass

Cheers,
-RyanT

Feb 03

I found this out the hard way while working on my 4×4MatrixToTRS plugin. So I thought it would be handy to pass along. There are a few bugs with the MTransformationMatrix class when using Python. You can get around them, you just need to know about them to do so. The main bug seems to be that any function of this class that asks for RotationOrder &order will not work with Python. It will always throws an error.

So if you are attempting to get the Euler rotation in a certain order like I was then you need to find a work around. What I found was I just need to use the MTransformationMatrix.rotation() function to get the quaternion then I could use the MQuaternion.asEulerRotation() function and from there I could use the MEulerRotation functions to get what I needed. Here are two examples one that does not work that should and one that does work. I have let Autodesk know about this issue.

If you want to get the rotation value of the MTransformationMatrix class this is the wrong way to do it:

Continue reading »

Feb 03

I have been meaning to post this for some time well here it is finally. Hopefully this plugin is commented well enough to understand. I have included the source so you can tinker with it. I have also included an html file that looks like a traditional Maya Node help file. Which lists all the attributes and what they do.

Since the script is rather long I wont post it here. Please take a look at it and if you have any questions feel free to post or e-mail me.

You can find the new python plugin at my new downloads page.

Have fun!
-RyanT