PDA

View Full Version : python beginer


EmileXXI
11-12-2011, 05:28 PM
Hello,

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):

cm.select("pPlane1.vtx[" + str(randint(1,100)) + "]") pos1 = cm.xform(q=True,t=True, ws=True)


cm.polyCube()

cm.scale(0.4,1.5,0.4)

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.


thanks

gtbull80
11-12-2011, 06:11 PM
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

EmileXXI
11-12-2011, 06:18 PM
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.

NaughtyNathan
11-12-2011, 07:36 PM
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

:nathaN

gtbull80
11-12-2011, 07:49 PM
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 kidding...my mother is still alive:)


#ugly regular python approach
import random
objName = "pPlane1" #not necessary but I like to use variables:D
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 Pymel...it 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)

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

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

Pymel:
newCube.t.set(vtxPos)
or
newCube.setTranslation(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!

gtbull80
11-12-2011, 07:58 PM
Hmm... pointPosition...
Nice! Will have to steal that one from you! Should come in handy.

Thanks Nate:)

EmileXXI
11-13-2011, 06:26 PM
Thank you very much all of you. That is very usefull.

EmileXXI
11-13-2011, 06:36 PM
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.

NaughtyNathan
11-13-2011, 08:56 PM
the [0] just means the first element of the list/array. if you have a list:
myList = [ 'a','b','c','d','e','f' ]
then:
myList[0]
is equal to: 'a' right?
well the same is true for anything that is a sequence/list, and as:
cm.polyCube()
results in a list (i.e.: [ 'pCube1', 'polyCube1' ])
cm.polyCube()[0]
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.
:nathaN

EightBit
11-14-2011, 05:40 AM
What if he wanted to be even MORE efficient and instance the cubes?

NaughtyNathan
11-14-2011, 10:50 AM
then he would create the source cube at the same time he created the plane, and replace:
cm.polyCube()
with:
cm.instance(sourceCube)

:nathaN

gtbull80
11-14-2011, 03:04 PM
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.

NaughtyNathan
11-14-2011, 07:54 PM
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.

:nathaN

gtbull80
11-14-2011, 08:33 PM
Hmm...Makes sense. I think I was trying to solve a problem that wasn't asked:)

CGTalk Moderation
11-14-2011, 08:33 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.