python beginer

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 11 November 2011   #1
python beginer


What I am trying to do is making a plane so I can randomly select vertices on it and make cubes on each of those random vertices.

Here is my code :

import maya.cmds as cm from random import*

cm.polyPlane(sw=10, sh=10)

for vertices in range (1,10):"pPlane1.vtx[" + str(randint(1,100)) + "]") pos1 = cm.xform(q=True,t=True, ws=True)



cm.move(pos1) #here I don't know what to do with my xform command, how can I use it to tell my cube to go on the vertix's location ?

at the end want to make a random landscape with simple commands.

Hope you can help me on that.

Old 11 November 2011   #2
Pymel to the rescue!

If you use pymel libraries, you can take advantage of pymel's Object Oriented approach to python within Maya. This allows you to perform tasks more intuitively IMHO. This should work out for you. Be sure to freeze transformations on your plane though. Let me know if it works!

#import proper libraries
from pymel.core import *
import random

surfacePlane = PyNode("pPlane1") #store the plane into a variable. note replace "pPlane1" with the name of your plane
vtxList = surfacePlane.vtx #store the vertices into a list
numVtx = len(vtxList) #store the number of vertices
randIndex = random.randint(0, numVtx) #choose a random index
curVtx = vtxList[randIndex] #grab a random vertex
vtxPosition = curVtx.getPosition() #store the current vertices' position
newCube = polyCube()[0] #create and store a new cube
newCube.t.set(vtxPosition) #move the cube to the intended position
Old 11 November 2011   #3
Thank you for your answer.
Unfortunately I only use Python as this exercise is a python class' exercice. I cannot use pymel and I do not know anything about it. But once again thank you for your answer which is quite detailed.
Old 11 November 2011   #4
in that case, here's a pure python version (with some extra tips thrown in for free! ):
import maya.cmds as cm
from random import *
# ALWAYS capture the name of the things you create
# you can't guarentee the plane will always be called "pPlane1"
plane = cm.polyPlane(sw=10, sh=10)[0]	
for n in range(1,10):
	randomIndex = randint(1,100)
	# don't select things and operate on selection, operate ON the thing
	pos1 = cm.pointPosition('%s.vtx[%d]' % (plane,randomIndex), w=1) 
	# (you can use xform, but pointPosition is better for getting point positions)
	cube = cm.polyCube()[0]	# make a cube (get it's name so you can refer to it later)
	cm.scale(0.4,1.5,0.4, cube)	# scale it
	cm.move(pos1[0],pos1[1],pos1[2], cube)	# move it
Old 11 November 2011   #5
No problem! You are forcing me to use ugly default Maya Python. I swore on my mother's grave I would never do this again...just mother is still alive

#ugly regular python approach
import random
objName = "pPlane1" #not necessary but I like to use variables
numVtx = mc.polyEvaluate(objName, v=True) #store the number of vertices
randIndex = random.randint(0, (numVtx -1) ) #choose a random index. Note: numVtx is out of range so we subtract 1
curVtx = objName + ".vtx[" + str(randIndex) + "]" #generate string for "plane1.[5]" for example
vtxPos = mc.xform(curVtx, t=True, ws=True, q=True) #store the translation for the chosen vertex
newCube = mc.polyCube()[0] #create and store a new Cube
mc.xform(newCube, t=vtxPos) #move the Cube into position

or to do the final move using move() do this:
mc.move(vtxPos[0], vtxPos[1], vtxPos[2], newCube)

I know it's for a class and you have to use classic Maya Python. But maybe in your free time, you could research even comes installed in Maya 2011 and 2012. I personally don't like regular Maya Python. You have to do some ugly string manipulation at times. With Pymel, you get work with powerful objects and less with strings. For example:

Normal Maya Python:
curVtx = objName + ".vtx[" + str(randIndex) + "]"
vtxPos = mc.xform(curVtx, t=True, ws=True, q=True)

curVtx = objName.vtx[randIndex]
vtxPos = curVtx.getPosition()

Normal Maya Python:
mc.xform(newCube, t=vtxPos)


Cool huh! Well...if you think programming is cool

You were very close in your initial attempt! Keep it up. Soon you'll be a scripting master!
Old 11 November 2011   #6
Hmm... pointPosition...
Nice! Will have to steal that one from you! Should come in handy.

Thanks Nate
Old 11 November 2011   #7
Thank you very much all of you. That is very usefull.
Old 11 November 2011   #8
NaughtyNathan >>

cube = cm.polyCube()[0] # what does [0]stands for ?
cm.scale(0.4,1.5,0.4, cube) #
cm.move(pos1[0],pos1[1],pos1[2], cube) # cube at the end of the () is just to make sure that i move my cube and not smthg else ?

Gtbull08 >> I see that it is really more intuitive. I ll come to that as I amm really enjoying scripting. I think it ll be a matter of what soft i want to use afterward. As i like Blender I really want to know python, and as i learn Maya at school, i think Maya ll be my main soft and i ll probably do more pymel.

once again thank you for your answers.
Old 11 November 2011   #9
the [0] just means the first element of the list/array. if you have a list:
myList = [ 'a','b','c','d','e','f' ]
is equal to: 'a' right?
well the same is true for anything that is a sequence/list, and as:
results in a list (i.e.: [ 'pCube1', 'polyCube1' ])
is therefore equal to: 'pCube1'
it's not absolutely necessary to do it this way, we could have left the variable 'cube' as a list, but we didn't need the rest of the list, so we ditched it there and then.

and yes, putting 'cube' in the commands makes sure we are operating on the right, specific object. There is no point selecting things in a script and operating on what you assume is selected, as this is not only more work for you to type and more work for your script to do, but dangerous as you don't always have complete control over what Maya is selecting.

Last edited by NaughtyNathan : 11 November 2011 at 08:59 PM.
Old 11 November 2011   #10
What if he wanted to be even MORE efficient and instance the cubes?
If animation couldn't change the world, it wouldn't be such a Micky-Mouse place.
Old 11 November 2011   #11
then he would create the source cube at the same time he created the plane, and replace:

Old 11 November 2011   #12
I'd only add that it's only more efficient if you wanted cubes (or any other structure) of the same size. Otherwise, you'd not want to instance. In fact you'd want to introduce some randomization to the transformations of the duplicated objects.
Old 11 November 2011   #13
Originally Posted by gtbull80: I'd only add that it's only more efficient if you wanted cubes (or any other structure) of the same size. Otherwise, you'd not want to instance. In fact you'd want to introduce some randomization to the transformations of the duplicated objects.

not really, only the shape node gets instanced, the transform, which handles all of the transformations (position, rotation and scale) is unique for each instance.

Old 11 November 2011   #14
Hmm...Makes sense. I think I was trying to solve a problem that wasn't asked
Old 11 November 2011   #15
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:15 PM.

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