randomly moving an objects verticies with python

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
  10 October 2012
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 random
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.select(mesh+'.vtx[0:'+str(vtxCount)+']', add=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?


Walk without rhythm and you wont attract the worm...

=>Reel & Portfolio<=
  10 October 2012
woops lost some formatting there!
Walk without rhythm and you wont attract the worm...

=>Reel & Portfolio<=
  10 October 2012
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:
selVerts = mc.filterExpand(sm=31)
mc.polyMoveVertex(selVerts, t=(randMoveX, randMoveY, randMoveZ), random=1)

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
Hey Nathan,
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?

Walk without rhythm and you wont attract the worm...

=>Reel & Portfolio<=
  10 October 2012
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.
CGTalk Policy/Legalities
Note that as CGTalk Members, you agree to the terms and conditions of using this website.
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 05:37 AM.

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