Python script to append an array(list)

Python script to append an array(list)

thresholdengineering
Contributor Contributor
5,845 Views
3 Replies
Message 1 of 4

Python script to append an array(list)

thresholdengineering
Contributor
Contributor

I've been Googling for two days now and trying every suggestion, so far nothing is working. 

I've tried append, add, insert, in every syntax I can think of and nothing works.

 

I'm using Python script because there are things (working) that I couldn't figure out with MEL

 

I's starting out with a polymesh array with a length of 1 called 'transforms'

And a matrix array with 3 xforms called matrices

 

This is the current error 

# Error: AttributeError: file <maya console> line 1: 'unicode' object has no attribute 'insert'

 

And here is the code snippit

num = 1
    
print '-------------------------Starting Outer Loop\n'
for x in range(2):
    print '-------------------------Starting Inner Loop\n'
    print '===loop:' + str(x)

    for matrix in matrices:
        cmds.select(transforms)
        cmds.duplicate()
        cmds.xform( m = matrix )
        cmds.makeIdentity( apply=True )
        num += 1
        print 'Number of objects = ' + str(num)
        obj = cmds.ls(sl=True)
        transforms.insert(num, obj)
        print(matrix)

 

Surely someone has figured this out?

0 Likes
Accepted solutions (1)
5,846 Views
3 Replies
Replies (3)
Message 2 of 4

thresholdengineering
Contributor
Contributor

It just occurred to me I may need another include statement?

I only have one for maya commands

 

import maya.cmds as cmds
sel = cmds.ls(sl = True)
matrices =[]

 

0 Likes
Message 3 of 4

jmreinhart
Advisor
Advisor
Accepted solution

That error means that your variable "transforms" is a string not an array/list. Check whichever line you are using to get that value.

0 Likes
Message 4 of 4

thresholdengineering
Contributor
Contributor

I had declared transforms as an array then set it equal a single object basically getting rid of the array.

transforms = sel[len(sel)-1]

 

Where I should have done this (which works)

obj = sel[len(sel)-1]
transforms.insert(0, obj)

 

Now if I could just get the new meshes to stat where they are and reset the transformation this simple script would be done

 

Manually Modify>FreezeTransformations followed by Modify>ResetTransformations does exactly what I want

But the "ResetTransformations" doesn't even register in the script editor so I have no idea what comand to use in my script

0 Likes