Python Assign list values to an object's translateXYZ?

Become a member of the CGSociety

Connect, Share, and Learn with our Large Growing CG Art Community. It's Free!

Thread Tools Search this Thread Display Modes
Old 04 April 2013   #1
Python Assign list values to an object's translateXYZ?

Hey guys, i have a list of WS coordinates that I collected from a plane's vertex positions.
Now lets say I also have a list of objects ( cubes for now), what i want to do is assign them cooridnates in my list to the cubes XYZ translate.

Could anyone point me to the right direction?

so far this is what i have:

vtxWorldPosition = []
for i in verts:
curPointPosition = cmds.xform( i, query=True, translation=True, worldSpace=True ) # [1.1269192869360154, 4.5408735275268555, 1.3387055339628269]
vtxWorldPosition.append( curPointPosition )

print vtxWorldPosition[1]
[-60.0, -2.6645352591003757e-14, 120.0]

cube = "pCube1"


this places 1 cube to the location of the first coordinate in the list. now i guess i need to figure out how to make it use the lists in a for loop.

Any help would be much appreciated.

Last edited by lazed : 04 April 2013 at 09:08 PM.
Old 04 April 2013   #2
import maya.cmds as cmds vtxWorldPosition = [] for i in verts: curPointPosition = cmds.xform( i, query=True, translation=True, worldSpace=True ) vtxWorldPosition.append(curPointPosition) for i in range(len(vtxWorldPosition)): #print vtxWorldPosition[i][0], vtxWorldPosition[i][1], vtxWorldPosition[i][2] cube = cmds.polyCube()[0] cmds.setAttr(cube+".translate", vtxWorldPosition[i][0], vtxWorldPosition[i][1], vtxWorldPosition[i][2], type="double3")

Hope this works and helps you out man. You might want to use the xform command to move the cubes also.

Cheers Jake
Old 04 April 2013   #3
Thanks man. took me an hour and half to figure it out myself but got there at the end.
I still changed it abit around after seeing yours because mine wasnt perfect.
Many Thanks again.
Old 04 April 2013   #4
i kinda ran into an error after trying to change it.

for i in range(len(vtxWorldPosition)):
#print vtxWorldPosition[i][0], vtxWorldPosition[i][1], vtxWorldPosition[i][2]
building = MakeRectengularTower(10, 10, 50, 6, 15, 20)[0]

## this is what builds me 10 buildings that i defined before, i added the [0] to the end of it because you had it after the polyCube so i thought it will work just fine. but i get this error:

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

# it creates my buildings but it doesent translate them to the vertex positons.

cmds.setAttr(building+".translate", vtxWorldPosition[i][0], vtxWorldPosition[i][1], vtxWorldPosition[i][2], type="double3")

any help would be much appreciated again.
Thanks for your time
Old 04 April 2013   #5
Yep I understand the problem your having. The reason I had the index on the back of the polycube command is because it returns a list of nodes.

import maya.cmds as cmds cube = cmds.polycube() print cube #[u'pCube5', u'polyCube5']

You can see here the cube variable is actually a unicode list of two nodes, the first being the actual transform node and the second being the polycube node. So the zero index at the end of the polycube command I had is only grabbing the transform node of the cube because that is the first in the list. The reason being that you wanted to edit the transformations of the cube not its actual properties.

So for your code I'm not sure how MakeRectengularTower() works but if you want to move it once you have created it you will want to return a transfrom node for the tower out of that function so that you can then position it. Right now it does not appear to be returning anything so that's the reason it says "# Error: 'NoneType' object is unsubscriptable".

Hope this clears it up and helps you out.

Cheers Jake
Old 04 April 2013   #6
Thanks man it defo helped me, i managed to figure out how to return their values. Incase anyone else got the same problem, i just had to add this to the end of my function
buildinglist = [] # empty list
buildingslist.append(buildingname[0]) # adding new buildings to list

print ", ".join(buildingname) # thi is just a print so i know its building the towers
print "Completed, Loading rest of the structures..."
return buildingslist # returns the buildings after it built them

Many thanks for your help again.
Also just a quick question, do you know what the code is so i can see what my script is doing?
someone shown me before in class but i cant remember. i think its something to do with setting the viewport refresh time it was like one line of code
Old 04 April 2013   #7
Thread automatically closed

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.
Thread Closed share thread

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Society of Digital Artists

Powered by vBulletin
Copyright 2000 - 2006,
Jelsoft Enterprises Ltd.
Minimize Ads
Forum Jump

All times are GMT. The time now is 03:52 AM.

Powered by vBulletin
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.