On Tuesday, January 29, 2013 3:30:41 AM UTC+1, Jesse Capper wrote:
> If you use pymel, you can call getNormal() on a face to get that face's 
> normal, and save yourself a love of time, and getNormals() to get the 
> vertexFace normals.
> 
> I'm not sure how to get a faceNormal using cmds since when edges are soft 
> polyNormalPerVertex will return averaged normals and you need unaveraged for 
> calculating the face normal (as far as I understand it).
> 
> That being said, a couple of comments about your approach above.
> 
> * Why are you using decimal here? Especially if you're just converting back 
> to float?
> * polyInfo can operate on multiple faces at once, so you don't need to list 
> through and append them, just pass in the list of faces and you'll get a list 
> of results back
> * You don't need to use regex for what you're trying to do, use the .split() 
> method of strings:
> If you have:
> normal_info = u'FACE_NORMAL    243: 0.133830 0.517799 -0.844970\n'
> # .split() with no arguments splits on whitespace, so splitting that string 
> gives you ['FACE_NORMAL', '243:', '0.133830', '0.517799', '-0.844970']
> label, vertex, x, y, z = normal_info.split()
> # remove the trailing ':' for the vertex (if you want it) and convert the 
> coords to float
> 
> This gets the face normal of the selected faces using polyInfo and averages 
> them all like you're doing.
> 
> faces = cmds.filterExpand(sm=34, expand=True)
> face_normals = cmds.polyInfo(faces, fn=True)
> normals = [[], [], []]
> for normal in face_normals:
>     label, vertex, x, y, z = normal.split()
>     normals[0].append(float(x))
>     normals[1].append(float(y))
>     normals[2].append(float(z))    
>     
> normals_count = len(face_normals)
> x_avg = sum(normals[0]) / normals_count
> y_avg = sum(normals[1]) / normals_count
> z_avg = sum(normals[2]) / normals_count
> 
> 
> On Saturday, January 26, 2013 3:09:16 AM UTC-8, JR wrote:
> > Thanks Jesse! 
> > 
> > I also found a way with the polyInfo command which can return values for 
> > the face normal itself, but my code is quite a bit longer to get the same 
> > result as you did with the verts. Thought I'd post it anyways. I'm assuming 
> > with these values of 0 to 1 or 0 to -1 for x,y,z that I should now turn 
> > that data into an angle in world space? I think that's what the next step 
> > is anyways.
> > 
> > import maya.cmds as cmds
> > import decimal
> > decimal.getcontext().prec = 2
> > def reFunction(item, isolate):
> >     s = re.compile( isolate )
> >     f = s.finditer(item)
> >     return [ x.span() for x in f ]
> > sl = cmds.ls(selection=True, flatten=True)
> > faceList = []
> > for i in sl:
> >     x = cmds.polyInfo( i, fn=True )
> >     faceList.append(x)
> > xValue  = []
> > yValue  = []
> > zValue  = []
> > for i in range(len(faceList)):
> >     y = reFunction(faceList[i][0], ': ') # finding the split in the poly 
> > face normal info
> >     a = faceList[i][0][(y[0][1]):] # removes all text and leaves values
> >     p = reFunction(a, '\s+') # finds the whitespace between x,y,z values
> >     xValue.append( decimal.Decimal( a[:p[0][0] ] ) )
> >     yValue.append( decimal.Decimal( a[ p[0][1] : p[1][0] ] ) )
> >     zValue.append( decimal.Decimal( a[ p[1][1]: ] ) )
> > xAverage = float( decimal.Decimal (sum(xValue) ) / 
> > decimal.Decimal(str(float(len(xValue)))) )
> > yAverage = float( decimal.Decimal (sum(yValue) ) / 
> > decimal.Decimal(str(float(len(yValue)))) )
> > zAverage = float( decimal.Decimal (sum(zValue) ) / 
> > decimal.Decimal(str(float(len(zValue)))) )
> > print xAverage
> > print yAverage
> > print zAverage
> > 
> > 
> > Cheers,
> > 
> > J
> > 
> > 
> > On Friday, January 25, 2013 11:28:35 PM UTC+1, Jesse Capper wrote:
> > > Typo at the start:
> > > You can get vertexFace normal directions from the polyNormalPerVertex 
> > > command.
> > > 
> > > On Friday, January 25, 2013 2:26:39 PM UTC-8, Jesse Capper wrote:
> > > > You can vertexFace normals from the polyNormalPerVertex command.
> > > > 
> > > > So something like:
> > > > 
> > > > # Get the face vertices from the face
> > > > vtxfaces = cmds.polyListComponentConversion(my_poly_face, ff=True, 
> > > > tvf=True)
> > > > # Get a flattened list of the normals (the length of this list is: 
> > > > number of vertexFaces * 3[for the x,y,z coord of each normal])
> > > > flatnormals = cmds.polyNormalPerVertex(vtxfaces, q=True, xyz=True)
> > > > # Which you can group into x,y,z 3-tuples if you want:
> > > > normals = []
> > > > for i in range(0, len(flatnormals), 3):
> > > >     normals.append(flatnormals[i:i+3])
> > > > 
> > > > There's a slicker way of splitting a list into n-sized chunks, but it's 
> > > > not as obvious/easy to figure out at first glance:
> > > > normals = zip(*[iter(flatnormals)]*3)
> > > > 
> > > > On Friday, January 25, 2013 10:47:34 AM UTC-8, JR wrote:
> > > > > Hey all,
> > > > > 
> > > > > Been learning a lot from this group but there is one problem that I 
> > > > > can't seem to figure out. I'm trying to calculate the angle of a 
> > > > > selected face based on the normal but I have no idea how to get that 
> > > > > information. Nothing in the python command field seems to help. Any 
> > > > > ideas?
> > > > > 
> > > > > Thanks!
> > > > > 
> > > > > J


What I'm basically trying to do is when I use the polyChipOff command, and 
another mesh is created from the selected faces, I want to be able to use the 
averaged normal direction to re-orient it's rotation axis with the xform 
command. I am running into a few stumbling blocks though, I can get the normal 
directions but translating them into angles is another story. I started to use 
a dotproduct script from stackoverflow but the results do not translate 
properly when plugged into the xform rotateAxis flag.

import math
normalValue = [0.66, 0.562, 0.499]
xValue = [1,0,0]
yValue = [0,1,0]
zValue = [0,0,1]
def dotproduct(v1, v2):
  return sum((a*b) for a, b in zip(v1, v2))

def length(v):
  return math.sqrt(dotproduct(v, v))

def angle(v1, v2):
  return math.acos(dotproduct(v1, v2) / (length(v1) * length(v2)))

xRadian =  angle(normalValue, xValue)
yRadian =  angle(normalValue, yValue)
zRadian =  angle(normalValue, zValue)
x = xRadian*180/3.14
y = yRadian*180/3.14
z = zRadian*180/3.14
print x
print y
print z

Still working with it but the values never seem to come out properly. I'm still 
fairly new to Python in Maya so if there is something obvious I'm overlooking I 
certainly would appreciate any help you can give :)

Cheers,

J

-- 
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 [email protected].
To post to this group, send email to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to