简体   繁体   中英

random selection by maya-python

my question is how can i select random numbers of points of (for example in this script) lattice or vertex of an object or face or everything or totally random selection from a list in python.

please first explain random selection on this script

cm.select('ffd1Lattice.pt[:][:][:]',r=True)

and please after that explain random selection on lists and on every method.

best regards

import maya.cmds as cm
import sys
import random as rand

myList = cm.ls ( sl = True)


def softIt(*arg):
    cm.polySmooth( c = True , dv = 1 , kb = False )

def randomize(*arg):
    myList = cm.ls ( sl = True)
    for i in myList:
        cm.lattice( dv=(4, 5, 4), oc=True )
        cm.select('ffd1Lattice.pt[:][:][:]',r=True)
        cm.xform( r = True , t = [ rand.uniform(-1,1) , rand.uniform(-1,1) ,   rand.uniform(-1,1)]  )

cm.headsUpMessage('this script just work with one object at time', verticalOffset=250)


cm.window( t = 'Randomaize' , s = False)
cm.columnLayout()
cm.button( l = 'do it' , c = randomize , w = 200)
cm.button( l = 'soft it' , c = softIt , w = 200)
cm.showWindow() 

sys.stdout.write("this script just work with one object at time\t\"script by Amin   khormaei\"")

If you aren't understanding that little snippet then you should first understand how list slicing works. These links should point you in the right direction.

The line:

cm.select('ffd1Lattice.pt[:][:][:]',r=True)

says basically 'select all the control points in the lattice'. There seems to be a bug, however, in maya 2011 (don't know about later) which will try to select points that dont exist. A 1x1x1 lattice should have 8 points numbers 0-1 in all three dimensions - but using your snippet and then calling ls on the result will report

// result: 'ffdLatticeShape.pt[0:2][0:2][0:2]' // 

... at least it does on my machine -- and that should be impossible. I'd avoid using the [:] form with lattices (actually the [*] form has the same bug too. Ouch.). It works fine with regular vertices, however.

For your specific application, do you want to move all of the points by the same random amount -- which is what your snippet suggests -- or all of the points by different random amounts? Or random sets of points by a random amount? the code would be different for each alternative.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM