如何用Python在Maya中找到Y面的Y面

时间:2018-02-09 12:51:21

标签: python maya

对不起这些具体问题的人,我认为只有掌握玛雅知识的人才会回答这个问题。在Maya我有不同大小的立方体,我需要找到python哪个面的立方体指向Y轴。 (枢轴位于中心)任何提示将不胜感激

非常感谢:)

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

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

import pandas as pd

from bokeh.core.properties import value
from bokeh.io import output_file
from bokeh.models import FactorRange
from bokeh.palettes import Spectral8
from bokeh.plotting import figure, show

df1 = pd.DataFrame([['1','1','1','2','2','2','3','3','3'],[1.2, 3.5, 44, 77, 3.4, 24, 11, 12, 13], ['30312', '20021', '23423', '23424', '45646', '34535', '35345', '34535', '76786']]).T
df1.columns = ['QID','score', 'DocID']
df1 = df1.pivot(index='QID', columns='DocID', values='score').fillna(0)
df1.index = [(x, 'df1') for x in df1.index]

df2 = pd.DataFrame([['1','1','1','2','2','2','3','3','3'],[21.2, 13.5, 12, 77.6, 3.9, 29, 17, 41, 32], ['30312', '20021', '23423', '23424', '45646', '34535', '35345', '34535', '76786']]).T
df2.columns = ['QID','score', 'DocID']
df2 = df2.pivot(index='QID', columns='DocID', values='score').fillna(0)
df2.index = [(x,'df2') for x in df2.index]

df = pd.concat([df1, df2])

p = figure(plot_width=800, x_range=FactorRange(*df.index))
p.vbar_stack(df.columns, x='index', width=0.8, fill_color=Spectral8, 
             line_color=None, source=df, legend=[value(x) for x in df.columns]) 

p.legend.location = "top_left"

output_file('foo.html')
show(p)

答案 2 :(得分:1)

如果您只需要一个没有矢量数学和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是您可以容忍的度数的斜率。为了得到你想做的向下的面孔

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的方法更快,它循环遍历网格中的所有面。