
#-
# ==========================================================================
# Copyright (C) 2008 Autodesk, Inc. and/or its licensors.  All 
# rights reserved.
#
# The coded instructions, statements, computer programs, and/or related 
# material (collectively the "Data") in these files contain unpublished 
# information proprietary to Autodesk, Inc. ("Autodesk") and/or its 
# licensors, which is protected by U.S. and Canadian federal copyright 
# law and by international treaties.
#
# The Data is provided for use exclusively by You. You have the right 
# to use, modify, and incorporate this Data into other products for 
# purposes authorized by the Autodesk software license agreement, 
# without fee.
#
# The copyright notices in the Software and this entire statement, 
# including the above license grant, this restriction and the 
# following disclaimer, must be included in all copies of the 
# Software, in whole or in part, and all derivative works of 
# the Software, unless such copies or derivative works are solely 
# in the form of machine-executable object code generated by a 
# source language processor.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND. 
# AUTODESK DOES NOT MAKE AND HEREBY DISCLAIMS ANY EXPRESS OR IMPLIED 
# WARRANTIES INCLUDING, BUT NOT LIMITED TO, THE WARRANTIES OF 
# NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR 
# PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE, OR 
# TRADE PRACTICE. IN NO EVENT WILL AUTODESK AND/OR ITS LICENSORS 
# BE LIABLE FOR ANY LOST REVENUES, DATA, OR PROFITS, OR SPECIAL, 
# DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES, EVEN IF AUTODESK 
# AND/OR ITS LICENSORS HAS BEEN ADVISED OF THE POSSIBILITY 
# OR PROBABILITY OF SUCH DAMAGES.
#
# ==========================================================================
#+

# This example shows how to override a class's __str__() method so that
# it displays its contents in a more readable manner.

import maya.OpenMaya as om

# -------------- MVector --------------------

# Print out an MVector using its default __str__() method.
v = om.MVector(1, 2, 3)
print "With it's default __str__() method an MVector prints like this:", v

# Define a method to format an MVector into a string.
def MVector_str(self):
	return "(%g, %g, %g)" % (self.x, self.y, self.z)

# Save MVector's existing __str__() method.
oldMVector_str = om.MVector.__str__

# Replace MVector's __str__() method with our own.
om.MVector.__str__ = MVector_str

# Try printing the vector again.
print "With our replacement __str__() method it prints like this:", v

# Restore the original __str__() method.
om.MVector.__str__ = oldMVector_str

print

# -------------- MIntArray ------------------

# Print out a sparse MIntArray using its default __str__() method.
sparse = om.MIntArray(100)
sparse[7] = -3
sparse[20] = 13
sparse[82] = 6

print "A sparse MIntArray of 100 elements prints like this:", sparse

# Replacement for MIntArray's __str__() method
# which compresses long sequences of identical values.
def compressedMIntArray_str(self):
	s = "["
	count = 0
	prev = None
	for i in self:
		if i == prev:
			count += 1
		else:
			if prev != None:
				s += compressSeq(count, prev) + ", "
			count = 1
			prev = i

	s += compressSeq(count, prev) + "]"
	return s

# Helper function which returns a string containing
# 'count' copies of 'value'.
# If 'count' is three or less then they are returned as
# a comma-separated list, otherwise they are returned
# in 'value * count' notation. E.g. '3*24' means a
# sequence of 24 values of 3.
def compressSeq(count, value):
	if count > 0:
		if count > 3:
			s = str(value) + "*" + str(count)
		else:
			s = str(value)
			for i in range(1, count):
				s += ", " + str(value)
	else:
		s = ""
	return s

# Save MIntArray's existing __str__() method.
oldMIntArray_str = om.MIntArray.__str__

# Replace MIntArray's __str__() method with our own.
om.MIntArray.__str__ = compressedMIntArray_str

# Try printing the array again.
print "With our replacement __str__() method it prints like this:", sparse

# Restore the original __str__() method.
om.MIntArray.__str__ = oldMIntArray_str
