Search code examples
pythonmaya

how to find Y face of the cube in Maya with Python


sorry for such specific question guys , I think people only with knowledge of Maya will answer tho. In Maya I have cubes different sizes and I need to find with python which face of cube is pointing Y axis down. (Pivot is in center) Any tips will be appreciated

Thanks a lot :)


Solution

  • If you just need a quick solution without vector math and Pymel or the the API, you can use cmds.polySelectConstraint to find the faces aligned with a normal. All you need to do is select all the faces, then use the constraint to get only the ones pointing the right way. This will select all the faces in a mesh that are pointing along a given axis:

    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!
    

    The axis is the x,y,z axis you want and tolerance is the slop in degrees you'll tolerate. To get the downward faces you'd do

    select_faces_by_axis ('your_mesh_here', (0,0,-1))
    

    or

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

    This method has the advantage of operating mostly in Maya's C++, it's going to be faster than python-based methods that loop over all the faces in a mesh.