如何 select 列表中的随机值?
How to select a random value from a list?
我正在尝试 select 下面列表中的一项,但我的列表 = RN.choice(xyz) 目前 select 正在处理 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
随机数将替换 ???到 select 球体上的随机顶点。
看起来您只是想 select 从球体中随机生成一个顶点?
这实际上很简单。我会避免使用 random.uniform
因为它会给你一个浮点数,只需使用 random.randint
即可。
要获得 '???'
的随机顶点,您只需使用基本的字符串连接将它们拼接在一起。
这是一个创建球体和 select 随机顶点的示例:
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.
如果这不是您想要的,请编辑您的 post 并清楚地解释您期望的输出结果。
我正在尝试 select 下面列表中的一项,但我的列表 = RN.choice(xyz) 目前 select 正在处理 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
随机数将替换 ???到 select 球体上的随机顶点。
看起来您只是想 select 从球体中随机生成一个顶点?
这实际上很简单。我会避免使用 random.uniform
因为它会给你一个浮点数,只需使用 random.randint
即可。
要获得 '???'
的随机顶点,您只需使用基本的字符串连接将它们拼接在一起。
这是一个创建球体和 select 随机顶点的示例:
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.
如果这不是您想要的,请编辑您的 post 并清楚地解释您期望的输出结果。