python - 在maya python中选择子对象
问题描述
我目前正在尝试为 DAG 节点的每个子对象赋予不同的颜色。当我在组件选择模式下选择每个子对象/元素(但不是全部)然后在其上运行脚本时,它会起作用。但是选择整个网格对我不起作用。我已经尝试过不同种类的 listRelatives 但没有一个奏效。我可以进入对象并获取每个顶点,但它们没有按连通性分组。
def assignDMat(self,*args):
#selection = self.selector()
selection = cmds.ls(selection=True)
for j in range(0, len(selection)):
cmds.polyColorPerVertex(selection[j], r=0, g=0, b=0, cdo=True)
cmds.polyColorPerVertex(selection[j], r=random.uniform(0,1), cdo=True)
新代码:
import maya.cmds as cmds
import random
selection = cmds.ls(selection=True)
cmds.polySeparate(selection)
for i in range(0,len(selection)):
obj=cmds.listRelatives(selection[i])
print(len(obj))
for j in range(0,len(obj)-1):
cmds.polyColorPerVertex(obj[j], r=0, g=0, b=0, cdo=True)
cmds.polyColorPerVertex(obj[j], r=random.uniform(0,1), cdo=True)
cmds.polyUnite(("polySurface*"), n="Result", ch=False)
解决方案
使用单独然后重新合并网格可能会导致很多意想不到的问题,其中一些您已经遇到过,因此最好避免这种情况。
相反,我们可以创建一个函数,通过返回一组人脸索引列表来返回对象的所有单独外壳。这可以cmds.polySelect
通过将面部索引传递给它来完成,然后它将选择整个外壳。由于现在选择了外壳,我们现在可以收集新选择并继续获取下一个外壳。然后,很容易遍历它们中的每一个并用cmds.polyColorPerVertex
.
这是一个示例,它将创建随机数量的多边形球体,将它们全部组合起来,然后为每个外壳设置随机颜色:
import random
import maya.cmds as cmds
# Just makes a bunch of random spheres, combines them, then returns the new mesh.
def make_bunch_of_spheres():
meshes = []
for i in range(random.randint(20, 50)):
meshes.append(cmds.polySphere()[0])
cmds.move(random.randint(-20, 20), random.randint(-20, 20), random.randint(-20, 20), meshes[-1])
return cmds.polyUnite(meshes, constructionHistory=False)[0]
def get_shell_faces():
shells = [] # We'll be putting in lists of face indexes in here later. Each sub-list will represent a separate shell.
sel = cmds.ls(sl=True)
for obj in sel:
faces = cmds.ls(obj + ".f[*]", flatten=True) # Get all face indexes from the object.
for face in faces:
index = int(face.split("[")[1].rstrip("]")) # Extract the faces index number.
cmds.polySelect(obj, extendToShell=index) # Use the face's index to select the whole shell.
new_faces = cmds.ls(sl=True, flatten=True) # Now that the shell is selected, capture its faces.
shells.append(new_faces) # Append the face's as a new shell.
# Remove indexes from the new shell from this current loop, to optimize, and to avoid adding duplicate shells.
for new_face in new_faces:
if new_face in faces:
faces.pop(faces.index(new_face))
cmds.select(sel) # Restore selection.
return shells
# Create a bunch of combined spheres then select it.
new_mesh = make_bunch_of_spheres()
cmds.select(new_mesh)
shells = get_shell_faces() # Get shells from selection.
# Color each shell!
for shell in shells:
cmds.polyColorPerVertex(shell, r=random.random(), g=random.random(), b=random.random(), cdo=True)
推荐阅读
- laravel - Laravel - 检查文件是否已转换的最佳实践
- xamarin.forms - Xamarin Forms:选项卡式页面图标和标题重叠
- electron - 电子自动更新器下载什么?
- neo4j - 从关系数据库中获取的数据的 csv 创建节点和关系
- c - 从文件中获取数据并在每次新行开始时将其打印出来
- kotlin - 是否有在调试信息中包含源文件的配置?
- python - 如何将 python 包上传到 Nexus sonartype 私人仓库
- java - 识别在 Servlet 容器中运行的每个虚拟主机
- ios - Cocoapods 错误:metadata.rb:15:in `initialize':
- css - 图像不会显示在移动设备上,但它们可以在台式机上显示并正常工作,如果“桌面站点”已打开