View Full Version : python data type

02 February 2008, 06:48 PM
I am trying to build an array from some particle information. I am new to python and am a bit confused about how it handels arrays.

I want to be able to build a master array that gets assembled through a loop and then be able to access parts of that array. I'm not quite sure how to go about it.

#go into solvers menu and create particle disk cache before you run script.
#this script will calcuate the position or each particle and return a unit vector

from math import sqrt
import pickle
import maya.cmds as cmds

time = cmds.currentTime( query=True )
timeStep = 10

sel = (sl=True)
ptcount = cmds.getAttr (sel[0] + '.count')

for i in range(0, ptcount):

cmds.currentTime( str(time), edit=True ) (sel[0] + '.pt[' + str(i) + ']', r=True)
pos = cmds.getParticleAttr (sel[0] + '.pt[' + str(i) + ']' , at ='worldPosition' )

cmds.currentTime( str(time+timeStep ), edit=True )
posTwo = cmds.getParticleAttr (sel[0] + '.pt[' + str(i) + ']' , at ='worldPosition' )

vx = pos[0] - posTwo[0]
vy = pos[1] - posTwo[1]
vz = pos[2] - posTwo[2]

magnatude = sqrt((vx * vx) + (vy * vy) + (vz * vz))

normVector += [vx/magnatude,vy/magnatude,vz/magnatude]


This is the error i keep getting.

# Error: 'float' object is unsubscriptable
# Traceback (most recent call last):
# File "<maya console>", line 36, in <module>
# TypeError: 'float' object is unsubscriptable #

02 February 2008, 01:23 AM
Hi there,

The way you're handling the normVector variable looks wrong.
I wouldnt expect that you could += a vector together onto an array/list.
Something along the lines of normVector.append(value) to build up the list should work.

Here are a couple of good cheat sheets I've been using while converting over to Python. You'll want to have a look at the Lists sections:


02 February 2008, 08:38 AM
yeah I wonder if it's the += at the end- try using the list methods instead, list.append() won't work in this case since you're adding multiple elements, but there is a list method that lets you append another list- just can't remember off the top of my head which method it is- should be in the list method docs-

02 February 2008, 03:01 PM
normVector[2][1] is the offending line. normVector is a list of floats, so
normVector[2] is a float, which can't be subscripted.

When you do this:

normVector += [vx/magnatude,vy/magnatude,vz/magnatude]

the individual elements get appended to normVector, not the list. Instead do this:


CGTalk Moderation
02 February 2008, 03:01 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.