How to convert a tuple into a list (python)

If you want to modify XSI array data and the array data has more than one dimension such as XYZ, UVW or RGBA you will need to know how to convert from the immutable tuple returned by XSI and a flattened mutable list.

WeightMaps: XSI will return a tuple that looks like this (('W1','W2','W3','W4','W5'),)
UVW,XYZ,RGBA: XSI will return a tuple that looks like this (('X1','X2','X3','X4'),('Y1','Y2','Y3','Y4'),('Z1','Z2','Z3','Z4'))

The following example shows you how to convert the tuple so you can write to it and pass it back to XSI.

import win32com.client
from win32com.client import constants
import sys

xsi = Application
true=1
false=0

def main():
	Test1()

# test1: turn a tuple into  a list
def Test1():
  # immutable tuple i.e. read-only
  print "1 dimensional tuple example" 
  w_tuple = ('W1','W2','W3','W4','W5')
  print repr(w_tuple) 
  
  w_list = [w for w in w_tuple]
  print  repr(w_list)
  
  w_tuple = (0.25,0.26,0.27,0.28,0.29)
  print repr(w_tuple) 
  
  w_list = [1.0-w for w in w_tuple]
  print  repr(w_list)
  
  print "2 dimensional tuple example (such as XYZ)" 
  w_tuple = (('X1','X2','X3','X4'),('Y1','Y2','Y3','Y4'),('Z1','Z2','Z3','Z4'))
  print repr(w_tuple) 
  
  w_list = [w_tuple[j][i] for i in range(len(w_tuple[0])) for j in range(len(w_tuple)) ]
  print  repr(w_list)
  
 
# test2: turn a weightmap tuple into a list, invert and set back
def Test2():
	obj = xsi.ActiveSceneRoot.AddGeometry( "Sphere", "MeshSurface", "TupleTest2" )
	wmap = xsi.CreateWeightMap("WeightMap", obj , "Weight Map", "Weight Map Property", false)(0)
	
	# get weightmap as tuple
	w_tuple = wmap.Elements.Array
   	print "wmap as tuple: " + repr(w_tuple)
	
	# convert to list and invert
	w_list = [ 1-w for w in w_tuple[0] ]
   	print "wmap as inverted list: " + repr(w_list)
   
	# set weightmap as list
	wmap.Elements.Array = w_list
  	
# test3: turn a uvw tuple into a list, invert and set back
def Test3():
	obj = xsi.ActiveSceneRoot.AddGeometry( "Cube", "MeshSurface", "TupleTest3" )
	xsi.GenerateUniqueUVs(obj, "Texture_Projection", "", "", "", "", "", "", "")

	mesh = obj.ActivePrimitive.Geometry
	cluster= mesh.Clusters.Filter( "sample" )(0)
	uvw = cluster.Properties.Filter("uvspace")(0)
  	
  	uvw_tuple = uvw.Elements.Array
  	print "uvw tuple: " + repr(uvw_tuple)
  	
  	uvw_list = [1-uvw_tuple[j][i] for i in range(len(uvw_tuple[0])) for j in range(len(uvw_tuple)) ]
  	print "uvw list: " + repr(uvw_list)
  	
  	uvw.Elements.Array = uvw_list
  	
def XSITuple_toList(_tuple):
	return [_tuple[j][i] for i in range(len(_tuple[0])) for j in range(len(_tuple)) ]
  	
if __name__=='__ax_main__':
	class WritableObject:
	    def __init__(self):
			self.content = ""
	    def write(self, string):
	    	if (string=='\n'):
				Application.Logmessage(self.content,32)
				self.content=""
	    	else:
				self.content+=string

	# example with redirection of sys.stdout
	sys.stdout = WritableObject()
	
main() 
	
 

This page was last modified 01:58, 18 Sep 2009.
This page has been accessed 74006 times.

© Copyright 2009 Autodesk Inc. All Rights Reserved. Privacy Policy | Legal Notices and Trademarks | Report Piracy