Get vertex normal in OpenMaya?

Got it!
Thanks a lot dive! :slight_smile:
Much appreciated!

1 Like

mannnnnn! thats perfect! I tested the code now @dive
thanks a lot for your help!
Its working perfect, and I understood what you did…
really thanks! :slight_smile:
when you are in brazil, let me know so we drink a beer haha.
cheers!

1 Like

No problem, glad it helped! Brazil sounds nice.
Btw this part of the code could probably be written better:

# create the locators aggreating the selection add=True
for i in range(len(sphere_vtxs_ws)):
  loc = mc.spaceLocator()
  mc.select(loc, add=True)
  loc_sl = om.MGlobal.getActiveSelectionList()
  ldag, lmobj = loc_sl.getComponent(0)
  loc_dags.append(ldag)
  loc_mobjs.append(lmobj)
  sclear()

-----------------------------

Rather than create them at 0,0,0 and move them in an iteration, then going through them again and orientating, the locators can be created directly via mc.spaceLocator(position=vtxposition), and oriented in the for loop below. No need to select them either, since they are selected upon creation.

great idea! makes tottaly sense.
I will modify that!
thanks a lot!

1 Like

worked very well, man!

# create the locators aggreating the selection add=True
for i in range(len(sphere_vtxs_ws)):
  offsetvector = om.MVector(sphere_vtxs_ws[i].x, sphere_vtxs_ws[i].y, sphere_vtxs_ws[i].z)
  loc = cmds.spaceLocator(position=offsetvector)
  cmds.select(loc, add=True)
  loc_sl = om.MGlobal.getActiveSelectionList()
  ldag, lmobj = loc_sl.getComponent(0)
  loc_dags.append(ldag)
  loc_mobjs.append(lmobj)
  sclear()
1 Like

Hey @dive!
I tried to improve a bit more the speed… let me know what you think. In this case I create only one point. edit: removed things unnecessary

import maya.cmds as cmds
import maya.api.OpenMaya as om

cmds.timer(s=True)

ws = om.MSpace.kWorld
YVector = om.MVector(0, 1, 0)

mesh = "pSphere1"
cmds.select(mesh)
sl = om.MGlobal.getActiveSelectionList()
tdag, mesh_mobj = sl.getComponent(0)
mfn_mesh = om.MFnMesh(tdag)

loc = cmds.spaceLocator()
cmds.select(loc)

for i in range(24962): # range = vertex count
    
    mesh_vtx_ws = mfn_mesh.getPoint(i, ws)
    mesh_normal_ws = mfn_mesh.getPolygonNormal(i, ws)
   
    loc_sl = om.MGlobal.getActiveSelectionList()
    locdag, locMobj = loc_sl.getComponent(0)
    
    normal = mesh_normal_ws.normalize()
    n = om.MVector(normal.x, normal.y, normal.z)
    quaternion = om.MQuaternion(YVector, n, 1.0)
    posVector = om.MVector(mesh_vtx_ws.x, mesh_vtx_ws.y, mesh_vtx_ws.z)
    
    locxform = om.MFnTransform(locdag)
    locxform.setRotation(quaternion, ws)
    locxform.setTranslation(posVector, ws)

tempo = cmds.timer(e=True)
print "tempo percorrido:", tempo
1 Like

hey @dive , just a tip based on my tests here, maybe its useful for you later.
do not get all vertices normals at once with getNormals()
use getVertexNormal and put all into a dictionary.
if u get all at once it sometimes get fake normals in some kind of meshes.

cheers!

1 Like