如何使用 Python 在 Maya 中找到立方体的 Y 面
how to find Y face of the cube in Maya with Python
抱歉问了这么具体的问题,我想只有懂 Maya 的人才会回答。在 Maya 中,我有不同大小的立方体,我需要使用 python 找到立方体的哪个面指向 Y 轴下方。 (枢轴在中心)任何提示将不胜感激
非常感谢:)
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,代码可以更紧凑一些。选择朝下的面:
n=pm.PyNode("pCubeShape1")
s = []
for f in n.faces:
if f.getNormal(space='world')[1] < 0.0:
s.append(f)
pm.select(s)
如果您只需要一个不需要矢量数学和 Pymel 或 API 的快速解决方案,您可以使用 cmds.polySelectConstraint
找到与法线对齐的面。您需要做的就是 select 所有的面孔,然后使用约束只让那些面孔指向正确的方向。这将 select 网格中指向给定轴的所有面:
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
是您可以容忍的倾斜度数。要获得向下的面孔,你会做
select_faces_by_axis ('your_mesh_here', (0,0,-1))
或
select_faces_by_axis ('your_mesh_here', (0,0,-1), 1)
# this would get faces only within 1 degree of downard
此方法的优点是主要在 Maya 的 C++ 中运行,它将比循环遍历网格中所有面的基于 python 的方法更快。
抱歉问了这么具体的问题,我想只有懂 Maya 的人才会回答。在 Maya 中,我有不同大小的立方体,我需要使用 python 找到立方体的哪个面指向 Y 轴下方。 (枢轴在中心)任何提示将不胜感激
非常感谢:)
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,代码可以更紧凑一些。选择朝下的面:
n=pm.PyNode("pCubeShape1")
s = []
for f in n.faces:
if f.getNormal(space='world')[1] < 0.0:
s.append(f)
pm.select(s)
如果您只需要一个不需要矢量数学和 Pymel 或 API 的快速解决方案,您可以使用 cmds.polySelectConstraint
找到与法线对齐的面。您需要做的就是 select 所有的面孔,然后使用约束只让那些面孔指向正确的方向。这将 select 网格中指向给定轴的所有面:
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
是您可以容忍的倾斜度数。要获得向下的面孔,你会做
select_faces_by_axis ('your_mesh_here', (0,0,-1))
或
select_faces_by_axis ('your_mesh_here', (0,0,-1), 1)
# this would get faces only within 1 degree of downard
此方法的优点是主要在 Maya 的 C++ 中运行,它将比循环遍历网格中所有面的基于 python 的方法更快。