Calculate Vector

231 views
Skip to first unread message

justin hidair

unread,
Mar 29, 2017, 10:12:50 PM3/29/17
to Python Programming for Autodesk Maya
========
*See the joint file*

Ho do I get the flashy green and red arrows (by arrows i mean vectors) from that one vector (Face Normal from maya) ? I can't wrap my head around that... Cross product only works if you know two vectors..

so how I can manage to determine these two from a face normal , with of course the right orientation ?? thanks in advance
yap.png

Kyle Burris

unread,
Mar 31, 2017, 11:06:38 AM3/31/17
to Python Programming for Autodesk Maya
You could get the positions of two vertices that form one of the edges, make a vector from that, and get the mid point. Then use that vector to use in your cross

Paul Molodowitch

unread,
Mar 31, 2017, 12:15:01 PM3/31/17
to python_in...@googlegroups.com
It's a little unclear what the red and green arrows are supposed to represent.  Ate they pointing to the midpoints of edges? Are they supposed to be perpendicular to each other? Are they supposed to be aligned to u (or v)?

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/46eb446b-ad4d-4f5d-9620-c741dd935452%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

justin hidair

unread,
Apr 1, 2017, 4:39:24 PM4/1/17
to python_in...@googlegroups.com
green and red arrows are vectors, it's like "axis" , secretly they are binormal and tangent vector, but how do I get them ?

justin hidair

unread,
Apr 1, 2017, 4:40:53 PM4/1/17
to python_in...@googlegroups.com
Imagine a rotated ngon like a flat disk , in this one case, the technique you suggested fails

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.

Alok Gandhi

unread,
Apr 1, 2017, 10:36:56 PM4/1/17
to python_in...@googlegroups.com
Given a surface normal, the choice for a tangent vector is arbitrary for a flat surface. You have the freedom to decide the direction of the tangent vector. In your case, the tangent vector can be defined from the base point of the normal to any arbitrary point on the polygon, this can be the mid-point of an edge or even can be one of the vertices. The choice is yours. Calculating the bitangent, then, is trivial. It is the result of the cross product between the normal and tangent.

The technique would be:

1. Get the tangent (this is arbitrary - it could be an edge or a vector between the mid-point of edges and the base of the normal etc.- any vector  that lies on the surface meaning you just need two points on the surface, does not matter where - this works only for flat surfaces which I assume you already have.)
2. Cross the normal and the tangent to the bitangent.

You can find some reference here

- Alok

Alok Gandhi

unread,
Apr 1, 2017, 11:53:35 PM4/1/17
to python_in...@googlegroups.com
erratum:
2. Cross the normal and the tangent to *obtain the bitangent.

Leonardo Bruni

unread,
Apr 2, 2017, 6:18:49 AM4/2/17
to python_in...@googlegroups.com
If you have the normal,you could use an arbitrary vector that i'll call worldUp for the first cross actually.
In something like a pseudocode, i suppose that you get "mynormal", and i'll call the variable red and green like in your image.

vector N = normalize(mynormal)
vector worldUp = [0,1,0] 
vector red = normalize(N ^ worldUp) 
vector green = normalize(red ^ N)

With 2 cross you can be sure that the matrix that you'll build will be orthogonal
Now you should have the result that you posted in your image

2017-04-02 5:53 GMT+02:00 Alok Gandhi <alok.ga...@gmail.com>:
erratum:
2. Cross the normal and the tangent to *obtain the bitangent.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.

justin hidair

unread,
Apr 2, 2017, 6:48:41 PM4/2/17
to Python Programming for Autodesk Maya
Thanks Alok !

Rémi Deletrain

unread,
Apr 5, 2017, 6:36:21 AM4/5/17
to Python Programming for Autodesk Maya
I make this code for create matrix from mesh face

def get_matrix_from_face(mfn_mesh, point, face_id):

    """
    !@Brief Get matrix from point position and face id.
            Take normal, tangent, binormal of all face vertex and average it.

    @type mfn_mesh: OpenMaya.MFnMesh
    @param mfn_mesh: MFnMesh for get point orientation
    @type point: MFloatPoint
    @param point: Position on face.
    @type face_id: int
    @param face_id: Face id

    @rtype: pymel.Core.datatypes.Matrix
    @return: Matrix found
    """

    #   Get normal
    normals = OpenMaya.MFloatVectorArray()
    mfn_mesh.getFaceVertexNormals(face_id, normals)
    normal = average_vector_array(normals)

    #   Get tangent
    tangents = OpenMaya.MFloatVectorArray()
    mfn_mesh.getFaceVertexTangents(face_id, tangents)
    tangent = average_vector_array(tangents)

    #   Get binormal
    binormal = tangent ^ normal
    binormal.normalize()

    #   Force normal perpendicalary
    normal = binormal ^ tangent
    normal.normalize()

    #   Create matrix
    from pymel import core as pmc
    matrix = pmc.datatypes.Matrix(
        [tangent.x, tangent.y, tangent.z, 0.0],
        [normal.x, normal.y, normal.z, 0.0],
        [binormal.x, binormal.y, binormal.z, 0.0],
        [point.x, point.y, point.z, 1.0]
    )

    return matrix

def average_vector_array(vector_array):

    """
    !@Brief Average MVector array

    @type vector_array: OpenMaya.MVectorArray / OpenMaya.MFloatVectorArray
    :param vector_array: Vector array to average

    @rtype: OpenMaya.MVector
    @return: Vector average
    """

    if not isinstance(vector_array, (OpenMaya.MVectorArray, OpenMaya.MFloatVectorArray)):
        raise BaseException("Invalid argument !!!\n\tArgument must be a MVectorArray.")

    m_vector = OpenMaya.MVector()
    for idx in xrange(vector_array.length()):
        m_vector += OpenMaya.MVector(vector_array[idx])

    m_vector /= vector_array.length()
    m_vector.normalize()

    return m_vector



Reply all
Reply to author
Forward
0 new messages