gpt4 book ai didi

python - Maya Python 3x - 基于法线角度选择边

转载 作者:太空宇宙 更新时间:2023-11-04 05:13:16 25 4
gpt4 key购买 nike

一般来说,我对 python 和脚本/编码还很陌生。

我目前正在编写一个小脚本,用于根据角度选择场景中对象的边缘。我有两个 intfields 供用户指定最小值和最大值。根据输入,我想要一个按钮来选择最小/最大范围内的所有边缘。
到目前为止一切正常,除了单击按钮时,不考虑最小/最大输入。 Maya 仅选择默认值为 0 的所有边,而不是选择最小/最大范围内的所有边缘,我不知道为什么。

根据我有限的知识,似乎“selEdges”函数是问题所在。“ab=”标志通常采用两个整数值,如果我这样定义它:ab=(0, 30),它可以工作,但我不想对其进行硬编码。我只是想不通如何将用户输入从两个内场传递到“selEdges”函数。

这是我的脚本:

import maya.cmds as cmds

if cmds.window('myWindow', exists=True):
cmds.deleteUI('myWindow')

cmds.window('myWindow', title='myWindow', wh=[200, 100])

cmds.rowColumnLayout(numberOfColumns=2)

minNormalAngle = cmds.intField(minValue=0, maxValue=360)
minNA = cmds.intField(minNormalAngle, query=True, value=True)

maxNormalAngle = cmds.intField(minValue=0, maxValue=360)
maxNA = cmds.intField(maxNormalAngle, query=True, value=True)

def selEdges(*pArgs):
cmds.polySelectConstraint(m=3, a=True, w=2, ab=(minNA, maxNA))
cmds.polySelectConstraint(m=0)

def sofEdges(*pArgs):
cmds.polySoftEdge(a=180)

cmds.button(label='select', command=selEdges)
cmds.button(label='soften', command=sofEdges)

cmds.showWindow()

为了更好地理解 Maya 中的 UI,这里有一个屏幕截图。!( http://abload.de/image.php?img=selscriptq9kcy.jpg )

编辑:

这是我现在拥有的:

# Selection Script by David Sikorsky 
#Alpha Version 0.5

# - NOT FEATURE COMPLETE
# - BUGS MAY OCCURE WHILE USING THIS SCRIPT

import maya.cmds as cmds
import functools


# Close window if it already exists
if cmds.window('myWindow', exists=True):
cmds.deleteUI('myWindow')

# Create a Window
myWindow = cmds.window('myWindow', title='Selection Script', resizeToFitChildren=True)
# Column 1: How To Text
cmds.columnLayout('columnLayoutName01', adjustableColumn=True)
cmds.frameLayout('layoutFrame01', label='How To Use:', collapsable=True, collapse=False, borderVisible=False)
cmds.separator(h=1, style='none')
cmds.text('howTo', font='boldLabelFont', label='How to use:', align="center")
cmds.separator()
cmds.text('desciption', font='boldLabelFont', align="center", label='''- Select an Object in your scene.
- Specify min/max Edge Angle.
- Select all Edges In Range.
- Soften or harden or Selected Edges.''')
cmds.separator(h=1, style='none')
cmds.setParent('..')

# Column 2: Soft/Hard Text
cmds.rowColumnLayout('columnLayoutName02', numberOfColumns=3, cw=[(1, 102),(2, 20), (3, 102)])
cmds.text(label='<-Soft->', align='center', font='boldLabelFont')
cmds.separator(h=1, style='none')
cmds.text(label='<-Hard->', align='center', font='boldLabelFont')
cmds.setParent('..')

# Column 2: min/max Text
cmds.rowColumnLayout('columnLayoutName03', numberOfColumns=7, cw=[(1, 51), (2, 51), (3, 20), (4, 51), (5, 51)])
cmds.text(label='Min', align='left')
cmds.text(label='Max', align='right')
cmds.separator(h=1, style='none')
cmds.text(label='Min', align='left')
cmds.text(label='Max', align='right')
cmds.setParent('..')

#Column 3: Infields
cmds.rowColumnLayout('columnLayoutName04', numberOfColumns=7, cw=[(1, 50), (2, 2), (3, 50), (4, 20), (5, 50), (6, 2), (7, 50)])

def selSoftIRFunc(*pArgs):
minSoftNA = cmds.intField(minSoftNormalAngle, query=True, value=True)
maxSoftNA = cmds.intField(maxSoftNormalAngle, query=True, value=True)
cmds.polySelectConstraint(m=3, t=0x8000, a=True, w=2, sm=0, ab=(minSoftNA, maxSoftNA))
cmds.polySelectConstraint(m=0, a=False)

minSoftNormalAngle = cmds.intField(minValue=0, maxValue=180, enterCommand=selSoftIRFunc)
cmds.separator(h=1, style='none')
maxSoftNormalAngle = cmds.intField(minValue=0, maxValue=180, enterCommand=selSoftIRFunc)
cmds.separator(h=1, style='none')

def selHardIRFunc(*pArgs):
minHardNA = cmds.intField(minHardNormalAngle, query=True, value=True)
maxHardNA = cmds.intField(maxHardNormalAngle, query=True, value=True)
cmds.polySelectConstraint(m=3, t=0x8000, a=True, w=2, sm=0, ab=(minHardNA, maxHardNA))
cmds.polySelectConstraint(m=0, a=False)

minHardNormalAngle = cmds.intField(minValue=0, maxValue=180, enterCommand=selHardIRFunc)
cmds.separator(h=1, style='none')
maxHardNormalAngle = cmds.intField(minValue=0, maxValue=180, enterCommand=selHardIRFunc)

cmds.setParent('..')

#column 4: Buttons Layout
cmds.rowColumnLayout('columnLayoutName05', numberOfColumns=3, cw=[(1,102),(2,20),(3,102)])
for i in range(3):
cmds.separator(h=5, style='none')

cmds.button(label='Select Edges', command=selSoftIRFunc)
cmds.separator(h=5, style='none')
cmds.button(label='Select Edges', command=selHardIRFunc)

for i in range(3):
cmds.separator(h=5, style='none')

def softEFunc(*pArgs):
cmds.polySoftEdge(a=180)

cmds.button(label='Soften Edges', command=softEFunc)
cmds.separator(h=5, style='none')

def hardenEFunc(*pArgs):
cmds.polySoftEdge(a=0)

cmds.button(label='Harden Edges', command=hardenEFunc)

for i in range(3):
cmds.separator(h=5, style='none')

def selSoftEFunc(*pArgs):
cmds.polySelectConstraint(m=3, t=0x8000, w=2, sm=2)
cmds.polySelectConstraint(m=0)

cmds.button(label='Select Soft Edges', command=selSoftEFunc)
cmds.separator(h=5, style='none')

def selHardEFunc(*pArgs):
cmds.polySelectConstraint(m=3, t=0x8000, w=2, sm=1)
cmds.polySelectConstraint(m=0)

cmds.button(label='Select Hard Edges', command=selHardEFunc)

for i in range(3):
cmds.separator(h=5, style='none')

def selInvertFunc(*pArgs):
cmds.InvertSelection('*', tgl=True)

cmds.button(label='Invert Selection', command=selInvertFunc)
cmds.separator(h=5, style='none')
cmds.setParent('myWindow')

# Close Button Layout
cmds.rowColumnLayout('columnLayoutName06', numberOfColumns=3, cw=[(1,102),(2,20),(3,102)])
def closeCallback(*pArgs):
if cmds.window('myWindow', exists=True):
cmds.deleteUI('myWindow')
for i in range(20):
cmds.separator(h=10, style='none')
cmds.button(label='Close', command=closeCallback)
cmds.showWindow('myWindow')

最佳答案

通常,您希望按如下顺序执行此操作:

  1. 将当前选区转换为边
  2. 对选区应用 polySelectConstraint 以将其筛选
  3. 重置 polySelect 约束,使其不会干扰 future 的选择

最好编写该函数,使其不会更改选择,因为很容易意外取消选择具有错误值的内容。最低版本应该是这样的(注意它如何在最后重置选择)

def edges_by_angle(selection, min_angle, max_angle):
'''return the edges in selection with a crossing angle between <min_angle> and <max_angle>'''
result = []
try:
edges = cmds.polyListComponentConversion(selection, te=True)
cmds.select(edges)
cmds.polySelectConstraint(mode =3, type = 0x8000, a = 1, ab = (min_angle,max_angle))
result = cmds.ls(sl=True) or []
finally:
cmds.polySelectConstraint(dis=True)
cmds.select(selection)
return result

您可以使用 intFieldGrp 而不是两个 intFields 来简化 GUI。养成在函数内构建图形用户界面的习惯是个好主意——您上面编写的代码将在监听器中运行,但如果从模块文件导入,则会出现奇怪的行为。将内容放入函数中是控制变量范围的一种廉价方法,因此不难找出所需的回调函数所在的位置。这是一个非常小的例子:

def selection_window():

# callbacks
def select_by_angle(a, b):
old_sel = cmds.ls(sl=True, o=True) or []
new_sel = (edges_by_angle(old_sel, a, b))
if new_sel:
cmds.select(new_sel)

def smooth_edges(*_):
cmds.polySoftEdge(a=180)

def harden_edges(*_):
cmds.polySoftEdge(a=0)

# gui....
w = cmds.window(title='select by edges')
c = cmds.columnLayout()
ifg = cmds.intFieldGrp(label = 'angles', nf = 2, cc = select_by_angle)
cmds.rowLayout(nc=2)
cmds.button('Soften', c= smooth_edges)
cmds.button('Harden', c= harden_edges)

#show the window and return it if you need it's name later...
cmds.showWindow(w)
return w

您可以将 edges_by_angle() 函数与此分开,因为它可能在其他情况下有用。

关于python - Maya Python 3x - 基于法线角度选择边,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42467574/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com