![](/img/trans.png)
[英]How to select a random text value from a python list using pyspark?
[英]How to select a random value from a list?
我正在嘗試從下面的列表中選擇一項,但我的列表= 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
隨機數將替換??? 選擇球體上的隨機頂點。
看來您只是想從球體中選擇一個隨機頂點? 實際上很簡單。 我會避免使用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.
如果這不是您想要的,請編輯您的文章,並明確說明您期望的輸出。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.