|10 October 2012||#1|
randomly moving an objects verticies with python
im trying to write a script in python that will select all the verts and randomly move them, so that I can add some randomness to the topology of objects
Ive managed to write it in mel, so im trying to convert it - for practice more than anything else
$mySelection = `ls- sl`;
$myVerts = `getVerts`;
for ($vert in $myVerts)
float $min = -0.04;
float $max = 0.04;
float $randNumX = rand( $min , $max );
float $randNumY = rand( $min , $max );
float $randNumZ = rand( $min , $max );
select -r $vert ;
move -r $randNumX $randNumY $randNumZ ;
select -r $mySelection;
Heres my attempt in python so far:
import maya.cmds as mc
minRand = -30
maxRand = 30
randMoveX = random.uniform ( minRand , maxRand )
randMoveY = random.uniform ( minRand , maxRand )
randMoveZ = random.uniform ( minRand , maxRand )
selectedMesh = mc.ls(selection=True, dag=True, type='mesh', long=True)
for mesh in selectedMesh:
vtxCount = mc.polyEvaluate(v=True)
mc.move( randMoveX, randMoveY, randMoveZ, relative=True )
Im struggling making the verts actually move, I can get them selected. Is there a command similar to getVerts for python?
|10 October 2012||#3|
Sony SCEE Liverpool
Manchester, United Kingdom
Join Date: May 2008
you shouldn't rely on selection when scripting, it's not explicit enough and is a completely unnecessary overhead. `getVerts` is a MEL script and not a very good way to actually return selected verts as it performs conversion and selection (both usually undesirable in any situation where you simply want to "get" the selected verts)
to get selected verts (or any other component type) use the filterExpand command. When you want to perform an operation on something, don't select it first then perform an action - perform the action ON the item:
I've used polyMoveVertex instead of plain move because then you get it as a history state, don't need to iterate over vertices and it also has a random flag built in so you may be able to just use that instead of using random numbers...
|10 October 2012||#4|
Thanks for the reply,
Does the polyMoveVertex command move all the verts in the selVerts list the same?, I need the script to move each vert separately and randomly.
When I try it using the lines you wrote, it just moved my whole object (by moving the verts)
To do this would I need to iterate through my string of verts and run the command on each one?
|10 October 2012||#5|
Join Date: Sep 2003
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.
Note that as CGTalk Members, you agree to the terms and conditions of using this website.
|Thread Closed share thread|