Get vertex normal for creating morph by script [SOLVED]
meipe
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
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
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/' );
}
})();
Please make sure you comply with the license terms.
You are welcome - glad it helped.
P