Select Maya 中的子对象 python
Select Subobjects in maya python
我目前正在尝试为 DAG 节点的每个子对象赋予不同的颜色。当我在组件 select 模式下 selecting 每个 subobject/elements (但不是全部),然后 运行 在它上面的脚本时,它就起作用了。但是 select 将网格作为一个整体,对我来说不起作用。我已经尝试过不同种类的 listRelatives,但 none 有效。我可以进入对象并获取每个顶点,但它们没有按连通性分组。
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)
import random
obj = cmds.ls(selection=True)
obj_vtx = [cmds.ls(o + '.vtx[*]', fl=True) for o in obj]
for obj in obj_vtx:
color_id = random.uniform(0,1)
for vtx in obj:
cmds.polyColorPerVertex(vtx, r=0, g=0, b=0, cdo=True)
cmds.polyColorPerVertex(vtx, r=color_id, cdo=True)
编辑:将颜色放入对象循环
当前有效的解决方案。它没有任何故障保险等。例如,如果对象没有父对象,它可能会报错。就我而言,这是可行的。
非常感谢 DrWeeny 的意见。
import maya.cmds as cmds
import random
par=cmds.listRelatives(cmds.ls(selection=True),p=True)
selection = cmds.ls(selection=True)
cmds.select(selection)
pivotTranslate = cmds.xform (selection[0], q = True, ws = True, rotatePivot = True)
print(piv)
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=selection[0], ch=False)
cmds.xform (selection[0], ws = True, pivots = pivotTranslate)
cmds.select(par)
cmds.parent(selection[0])
使用分离然后重新合并网格会导致很多意想不到的问题,有些问题您已经 运行 进入,所以最好避免这种情况。
相反,我们可以创建一个函数,该函数将 return 通过 return 一组对象的面部索引列表将对象的所有 shell 分开。这可以通过 cmds.polySelect
将面索引传递给它来完成,然后它将 select 整个 shell。由于 shell 现在 selected 我们现在可以收集新的 selection 并继续获得下一个 shell。然后很容易循环遍历它们中的每一个并用 cmds.polyColorPerVertex
.
给它们着色
下面的示例将创建 运行dom 数量的多边形球体,将它们全部组合起来,然后为每个 shell:
设置 运行dom 颜色
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)
我目前正在尝试为 DAG 节点的每个子对象赋予不同的颜色。当我在组件 select 模式下 selecting 每个 subobject/elements (但不是全部),然后 运行 在它上面的脚本时,它就起作用了。但是 select 将网格作为一个整体,对我来说不起作用。我已经尝试过不同种类的 listRelatives,但 none 有效。我可以进入对象并获取每个顶点,但它们没有按连通性分组。
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)
import random
obj = cmds.ls(selection=True)
obj_vtx = [cmds.ls(o + '.vtx[*]', fl=True) for o in obj]
for obj in obj_vtx:
color_id = random.uniform(0,1)
for vtx in obj:
cmds.polyColorPerVertex(vtx, r=0, g=0, b=0, cdo=True)
cmds.polyColorPerVertex(vtx, r=color_id, cdo=True)
编辑:将颜色放入对象循环
当前有效的解决方案。它没有任何故障保险等。例如,如果对象没有父对象,它可能会报错。就我而言,这是可行的。 非常感谢 DrWeeny 的意见。
import maya.cmds as cmds
import random
par=cmds.listRelatives(cmds.ls(selection=True),p=True)
selection = cmds.ls(selection=True)
cmds.select(selection)
pivotTranslate = cmds.xform (selection[0], q = True, ws = True, rotatePivot = True)
print(piv)
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=selection[0], ch=False)
cmds.xform (selection[0], ws = True, pivots = pivotTranslate)
cmds.select(par)
cmds.parent(selection[0])
使用分离然后重新合并网格会导致很多意想不到的问题,有些问题您已经 运行 进入,所以最好避免这种情况。
相反,我们可以创建一个函数,该函数将 return 通过 return 一组对象的面部索引列表将对象的所有 shell 分开。这可以通过 cmds.polySelect
将面索引传递给它来完成,然后它将 select 整个 shell。由于 shell 现在 selected 我们现在可以收集新的 selection 并继续获得下一个 shell。然后很容易循环遍历它们中的每一个并用 cmds.polyColorPerVertex
.
下面的示例将创建 运行dom 数量的多边形球体,将它们全部组合起来,然后为每个 shell:
设置 运行dom 颜色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)