Rey1000
Rey1000

Reputation: 3

How to select a random value from a list?

I am trying to select one item from the list below but my list = RN.choice(xyz) currently selecting everything in xyz list so can you guys please give me a tip on how to solve it?

import maya.cmds as MC
import pymel as pm
import random as RN

MC.polySphere(cuv=10, sy=20, ch=1, sx=20, r=1, ax=(0, 1, 0), n='BALL')
MC.selectMode( q=True, component=True )
listVert= MC.polyEvaluate(v=True)
print listVert
RandomSelection = []
for i in range (0,listVert):
    RNvert = RN.uniform(0,listVert)    
    xyz = [round(RNvert,0)]
    list = RN.choice(xyz)
    print list
    print xyz
MC.select('BALL.vtx[???]')
obj=MC.ls(sl=True)
print obj
allComponents = MC.ls( sl=True, fl=True ) 
print allComponents
shapeName = MC.polyListComponentConversion(obj, fv=True)[0]
objectName = MC.listRelatives(shapeName, p=True)[0]
    print "Object name is:"
    print objectName

The random number would be replace the ??? to select a random vertexes on a sphere.

Upvotes: 0

Views: 600

Answers (1)

Green Cell
Green Cell

Reputation: 4777

It looks like you're just trying to select a random vertex from a sphere? It's actually straight forward. I would avoid using random.uniform since that gives you a float, just use random.randint instead.

To get the random vertex to your '???' you just need to use basic string concatenation to stitch it all together.

Here's an example that creates a sphere and selects a random vertex:

import maya.cmds as cmds
import random

sphere, psphere = cmds.polySphere()  # Create a sphere
vert_count = cmds.polyEvaluate(sphere, v=True)  # Get the sphere's vertex count.
random_vert = random.randint(0, vert_count)  # Pick a random index from the vertex count.
cmds.select(sphere + ".vtx[" + str(random_vert) + "]")  # Concatenate strings.
#cmds.select("{}.vtx[{}]".format(obj, random_vert))  # Can also use `format` instead.

If this is not what you want then please edit your post and clearly explain what you expect the output should be.

Upvotes: 1

Related Questions