如何使用Python在Maya中找到立方体的Y面

2024-05-15 04:01:50 发布

您现在位置:Python中文网/ 问答频道 /正文

抱歉,我想只有了解玛雅的人才会回答这个问题。在Maya中,我有不同大小的立方体,我需要用python找到立方体的哪个面指向Y轴。(枢轴在中间)任何提示都将不胜感激

非常感谢:)


Tags: 指向枢轴maya人才
3条回答
import re
from maya import cmds
from pymel.core.datatypes import Vector, Matrix, Point

obj = 'pCube1'
# Get the world transformation matrix of the object
obj_matrix = Matrix(cmds.xform(obj, query=True, worldSpace=True, matrix=True))
# Iterate through all faces
for face in cmds.ls(obj + '.f[*]', flatten=True):
    # Get face normal in object space
    face_normals_text = cmds.polyInfo(face, faceNormals=True)[0]
    # Convert to a list of floats
    face_normals = [float(digit) for digit in re.findall(r'-?\d*\.\d*', face_normals_text)]
    # Create a Vector object and multiply with matrix to get world space
    v = Vector(face_normals) * obj_matrix
    # Check if vector faces downwards
    if max(abs(v[0]), abs(v[1]), abs(v[2])) == -v[1]:
        print face, v

如果您只需要一个没有向量数学和Pymel或API的快速解决方案,可以使用cmds.polySelectConstraint来查找与法线对齐的面。您只需选择所有面,然后使用约束仅获取指向正确方向的面。这将选择网格中沿给定轴指向的所有面:

import maya.cmds as cmds
def select_faces_by_axis (mesh, axis = (0,1,0), tolerance = 45):
    cmds.select(mesh + ".f[*]")
    cmds.polySelectConstraint(mode = 3, type = 8, orient = 2, orientaxis = axis, orientbound = (0, tolerance))
    cmds.polySelectConstraint(dis=True)  # remember to turn constraint off!

axis是你想要的x,y,z轴,tolerance是你能容忍的斜率。为了得到向下的脸你会这样做

^{pr2}$

或者

select_faces_by_axis ('your_mesh_here', (0,0,-1), 1)  
# this would get faces only within 1 degree of downard

这种方法的优势主要是在玛雅的C++中运行,它将比基于Python的方法在网格中的所有面上循环更快。在

使用pymel,代码可以更紧凑一些。选择向下的面:

n=pm.PyNode("pCubeShape1")
s = []
for f in n.faces:
    if f.getNormal(space='world')[1] < 0.0:
        s.append(f)
pm.select(s)

相关问题 更多 >

    热门问题