Get vertex normal for creating morph by script [SOLVED]

meipemeipe Posts: 101

I would like to create an inflating morph by script (something similar to the push modifier)...

Creating a new morph is easy, but I don't know how to access to the vertex normals in order to use them into the morph...

var newMorphDeltas = new DzMorphDeltas();

var oNode = Scene.getPrimarySelection();

if( oNode ){

var oObject = oNode.getObject();

if( oObject ){

var oShape = oObject.getCurrentShape();

if( oShape ){

var oGeometry = oShape.getGeometry();

if( oGeometry ){

var nVerts = oGeometry.getNumVertices();

print(nVerts);

for( var i = 0; i < nVerts; i += 1 ){

newMorphDeltas.addDelta(i, ***, ***, ***, true);

print(newMorphDeltas.getDeltaVec (i));

}

}

}

}

 

Post edited by meipe on

Comments

  • PraxisPraxis Posts: 240
    edited November 2023

    I use the average of the relevant Facet Normals:  DzFacetMesh.getNormal( DzFacet.normIdx* )

    Attached VertexNormal.txt contains the relevant extracts from my library code, which works fast enough for my purposes:

     

    function GetVertexDataArr( P_Mesh, P_FacetNumArr )

    Returns an Array of the DzFacet data (including Normal Index #s) specified by parameters P_Mesh and P_FacetNumArr, sorted into Vertex Index # order.

    This needs to be called only once per "job" for P_Mesh & P_FacetNumArr[]

     

    function  GetVertexNormalDir( P_Mesh, P_VertexDataNum, P_VertexDataArr )

    Returns the Normal Direction at the Vertex# specified by P_VertexDataArr[ P_VertexDataNum ].Vnum (averaged when necessary)

    Parameter P_VertexDataArr = GetVertexDataArr()

     

    Of course, you may need to check for extreme cases such as 2 Facets connected at each Vertex (co-planar) and with opposite Facet Normal directions, which will cause the Vertex Normals from the above to all be (0,0,0), and you would probably want to calculate them from the average of the Edge Normals instead.

    Hope this helps.

    P

     

    txt
    txt
    VertexNormal.txt
    10K
    Post edited by Praxis on
  • meipemeipe Posts: 101

    It works great! I also used the DzFacetMesh function (and the related ones) from the DAZ script samples.

    Superthanks! :)

     

    (function(){

     

     

    var newMorphDeltas = new DzMorphDeltas();

    var curNormal;

    var oNode = Scene.getPrimarySelection();

     

    // Get the facet mesh of the root node

    var oMesh = getFacetMeshForRootNode( oNode );

    // If we don't have a facet mesh

    if( !oMesh ){

    // We're done...

    return;

    }

    // Get the VertexData Array

    var oVertexDataArr=GetVertexDataArr(oMesh)

     

     

     

    if( oNode ){

    var oObject = oNode.getObject();

    if( oObject ){

    var oShape = oObject.getCurrentShape();

    if( oShape ){

    var oGeometry = oShape.getGeometry();

    if( oGeometry ){

    var nVerts = oGeometry.getNumVertices();

    print(nVerts);

    for( var i = 0; i < nVerts; i += 1 ){

    //Getting the vextex Normal

    var curNormal=GetVertexNormalDir(oMesh,i,oVertexDataArr);

    //Adding a delta with the normal value

    newMorphDeltas.addDelta(i, curNormal.x, curNormal.y, curNormal.z, true);

    }

    }

    }

    }

     

    var newMorph = new DzMorph( newMorphDeltas );

    oObject.addModifier (newMorph, -1);

     

    //Adjusting the morph parameters

    var curProperty = newMorph.getValueChannel();

    curProperty.setValue( 0 );

    curProperty.setMin( 0 );

    curProperty.setMax( 1.5 );

    curProperty.setIsClamped ( true );

    curProperty.setCanAutoFollow( true );

    var myMorphName = 'My Morph Name';

    newMorph.setName( 'MeshInflate' );

    newMorph.setLabel( 'Mesh Inflate' );

    curProperty.setPath( 'Adjustments/' );

     

    }

     

    })();

  • meipe said:

    It works great! I also used the DzFacetMesh function (and the related ones) from the DAZ script samples.

    Superthanks! :)

    Please make sure you comply with the license terms.

  • PraxisPraxis Posts: 240

    meipe said:

    It works great! I also used the DzFacetMesh function (and the related ones) from the DAZ script samples.

    Superthanks! :)

    You are welcome - glad it helped.

    P

     

Sign In or Register to comment.