Skip to main content
 首页 » 编程设计

python之如何在 Maya 上检索 cmds.polyExtrude 创建的顶点

2024年12月31日8qq78292959

我正在编写一个脚本来更改由给定特定向量的拉伸(stretch)命令创建的顶点的位置。但是我找不到获取新生成的顶点/面/边的方法。

我尝试查看 cmds.getAttr('polyExtrudeFace1') 或 cmds.polyExtrudeFacet 的查询模式,但找不到正确的属性/标志得到我需要的东西。

请您参考如下方法:

我不确定是否有一种很好的方法来获取新的挤出组件 ID,但如果您有获取之前状态的工具,您可以轻松找到它。 另一种方法是停用每个构造节点,一个一个启用 polyExtrudeFace 并填充一个 dic,然后重新启用所有内容。 下面是一个在拉伸(stretch)对象上选择最新顶点的示例:

''' 
This script only work on the last polyExtrudeFace and on vertex 
''' 
# get the object 
sel = cmds.ls(sl=True, o=True) 
# get the extrude nodes, useful to create a dic with all polyExtrudeFace new component ids  
extrudenodes = [e for e in cmds.listHistory(sel) if cmds.nodeType(e) == 'polyExtrudeFace'] 
#current vtx count 
current_vtx_nb = cmds.polyEvaluate(sel, v=1) 
# disable a polyExtude 
cmds.setAttr("{}.nodeState".format(extrudenodes[0]), 1) 
# get the previous number 
previous_vtx_nb = cmds.polyEvaluate(sel, v=1) 
# re-enable it 
cmds.setAttr("{}.nodeState".format(extrudenodes[0]), 0) 
# get the range 
nb = current_vtx_nb - previous_vtx_nb 
mrang = [current_vtx_nb-nb,current_vtx_nb] 
# recreate the vtx s3election 
out = ['{}.vtx[{}]'.format(sel[0], i) for i in range(*mrang)] 
# select the vertex 
cmds.select(out) 

编辑:

这是一个构建字典循环的例子:

import maya.cmds as cmds 
 
''' 
This script build the vertices data loop 
''' 
 
class Counter: 
    idCounter = 0 
    def __init__(self): 
        Counter.idCounter += 1 
 
 
def loopIncSel(): 
    'relaunch the command to loop throught all key of the dic' 
    if sorted(dataExtrude.keys()): 
        count = Counter().idCounter % len(dataExtrude.keys()) 
        k = dataExtrude.keys()[count] 
        cmds.select(dataExtrude[k]) 
 
 
# get the object 
sel = cmds.ls(sl=True, o=True) 
# get the extrude nodes, useful to create a dic with all polyExtrudeFace new component ids  
extrudenodes = [e for e in cmds.listHistory(sel) if cmds.nodeType(e) == 'polyExtrudeFace'] 
# dic data : 
dataExtrude = {} 
for n in extrudenodes: 
    cmds.setAttr("{}.nodeState".format(n), 1) 
 
# reverse the processus to re-enable,  
# note that if there is node in between creating vertices and faces, it won't work 
for n in extrudenodes[::-1]: 
    # get the previous number 
    previous_vtx_nb = cmds.polyEvaluate(sel, v=1) 
    # re-enable it 
    cmds.setAttr("{}.nodeState".format(n), 0) 
    #current vtx count 
    current_vtx_nb = cmds.polyEvaluate(sel, v=1) 
 
    # get the range 
    nb = current_vtx_nb - previous_vtx_nb 
    mrang = [current_vtx_nb-nb,current_vtx_nb] 
    # recreate the vtx s3election 
    dataExtrude[n] = ['{}.vtx[{}]'.format(sel[0], i) for i in range(*mrang)] 
# select the vertex 
# cmds.select(dataExtrude['polyExtrudeFace3']) 
loopIncSel()