如何从列表中选择一个随机值?

时间:2019-08-22 04:53:06

标签: python maya

我正在尝试从下面的列表中选择一项,但是我的列表= RN.choice(xyz)当前正在选择xyz列表中的所有内容,因此,请给我一些有关如何解决问题的提示吗?

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

随机数将代替???选择一个球体上的随机顶点。

1 个答案:

答案 0 :(得分:1)

您似乎只是想从球体中选择一个随机顶点? 实际上很简单。我会避免使用random.uniform,因为这会给您带来浮点数,而只需使用random.randint

要将随机顶点添加到'???',您只需使用基本的字符串串联将它们缝合在一起。

这是一个创建球体并选择随机顶点的示例:

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.

如果这不是您想要的,请编辑您的帖子,并明确说明您期望的输出结果。