normal vectors from torus Mesh

97 views
Skip to first unread message

Dimitry

unread,
May 27, 2009, 12:16:45 PM5/27/09
to python_inside_maya
Hi All!

I'm new in python scripting in maya, and I have two different
question:

1. are maya.cmds based on strings?
2. if I create torus

import maya.cmds as mc
radiL=0.07/2
radi=1
radi=(radi+radiL)*1.102#r=1
tori=mc.polyTorus( ax=[0,0,1], sx=8, sy=10, r=radi, sr=radiL)

how it is possible to get normal vectors from each mesh?

All the Best!
Dima

Dimitry

unread,
May 28, 2009, 3:18:19 AM5/28/09
to python_inside_maya
hey Gurus,
can anyone help me please?
I need it realy
Best!
Dima

Ofer Koren

unread,
May 28, 2009, 4:01:19 AM5/28/09
to python_in...@googlegroups.com
regarding maya.cmds - generally yes, the functions in that module accept strings, lists of strings, numbers, and lists of numbers. Generally anything that has a MEL counterpart, which excludes dictionaries, non-homogeneous lists, and higher level object types.

Regarding the torus - do you mean getting the normal for each face in the torus? please elaborate if you can

Dimitry

unread,
May 28, 2009, 5:37:32 AM5/28/09
to python_inside_maya
Hi Ofer!
thank you for the answer.
yes I meaning exact the vector from each face in the torus,
I want save it in an array.

Best!
Dima

Chad Dombrova

unread,
May 28, 2009, 2:52:20 PM5/28/09
to python_in...@googlegroups.com
from pymel import *
torus = polyTorus()[0]
normals = torus.getNormals()

Dimitry

unread,
May 29, 2009, 2:58:40 AM5/29/09
to python_inside_maya
Hi Chad,

thank you for reply!
if I copy>pasted your code , i got an error

# Error: ('invalid syntax', ('<maya console>', 2, 21, 'from pymel
import * \xe2\x80\xa8torus = polyTorus()[0] \xe2\x80\xa8normals =
torus.getNormals()\n'))
# File "<maya console>", line 1
# from pymel import *
# torus = polyTorus()[0]
# normals = torus.getNormals()
# ^
# SyntaxError: invalid syntax #

I don't know why? maybe because I'm using maya9 on mac?

Chad Dombrova

unread,
May 29, 2009, 3:03:56 AM5/29/09
to python_in...@googlegroups.com
looks like you copied and pasted rich text which maya did not like.
just try retyping it.

-chad

Chad Dombrova

unread,
May 29, 2009, 3:11:32 AM5/29/09
to python_in...@googlegroups.com
i just noticed you said you were using osx. one thing i often do on
osx to ensure i'm working with plain text is open up TextEdit, paste
what i copied into there, then hit Apple-Shift-T to make plain text.
then recopy and paste wherever you want.


Dimitry

unread,
Jun 2, 2009, 6:32:21 AM6/2/09
to python_inside_maya
hey Chad,
thank you for tip, I reinstall my pymel and got it with just
copy&paste.

but I have other problem

arrMesh = mc.ls(sl=True)
#here i save mesh as string
print arrMesh[0]#prints |polySurface1
normals = arrMesh.getNormals()
#prints 'list' object has no attribute 'getNormals'

the mesh object was imported as .obj into maya and my idea was to get
normal of each face of this mesh.

Best!
Dima

Chad Dombrova

unread,
Jun 2, 2009, 11:45:22 AM6/2/09
to python_in...@googlegroups.com

> arrMesh = mc.ls(sl=True)

> print arrMesh[0]#prints |polySurface1
> normals = arrMesh.getNormals()

you're using maya.cmds so arrMesh is a python list of strings. so
even if you were to do the proper thing:

arrMesh[0].getNormals()

it would not work unless you were using pymel, in which case the first
element of the list saved by arrMesh would be a pymel Transform/Mesh
node.

-chad

Dimitry

unread,
Jun 2, 2009, 12:13:47 PM6/2/09
to python_inside_maya
ok...
but how I can save a mesh as variable to work with it in pymel?

My knowledge of pymel is to less,I don't know how I can get the mesh

from pymel import *
arrMesh = getMesh(selection=true) #it is possible to let a selected
mesh to be a arrMesh as pymel object
normals = arrMesh.getNormals()


where I can found the pymel documentation?

Chad, I very appreciate you time and you help! Thank you!
Best!
Dima

Chad Dombrova

unread,
Jun 2, 2009, 12:26:58 PM6/2/09
to python_in...@googlegroups.com
from pymel import *
arrMesh = ls(selection=true)
normals = arrMesh.getNormals()

Dimitry

unread,
Jun 2, 2009, 12:48:32 PM6/2/09
to python_inside_maya
yep, it works!

from pymel import *
arrMesh = ls(selection=True)[0]
print arrMesh
normals = arrMesh.getNormals()

it is also possible to get the middle point coordinate of each face in
the mesh?

Chad Dombrova

unread,
Jun 2, 2009, 1:57:47 PM6/2/09
to python_in...@googlegroups.com
Hi Dimitry,
check the pymel docs for MeshFace

Dimitry

unread,
Jun 3, 2009, 9:05:25 AM6/3/09
to python_inside_maya
Hi Chad,
thank you for you help!
I checked the docs, but i'm still confused:
i can get points(arrMesh.getPoint(i)) from vertexes, but the
normalvector calculated(i suppose) from middle of a meshFace,
so i need the middle point from meshface
like
arrMesh.getVertexNormal(i)
arrMesh.getVertexNormalCoordinate(i)
how it is possible?

where i can download pymel documentation on my mac?to make possible
open it outline.

Best!
Dimitry

Ofer Koren

unread,
Jun 3, 2009, 11:04:25 AM6/3/09
to python_in...@googlegroups.com


you could get the position of all surrounding vertices and average them together to get the face center:

from operator import add
center = reduce(add, arrMesh.getPoints()) / arrMesh.polygonVertexCount()

- Ofer
www.mrbroken.com

Dimitry

unread,
Jun 3, 2009, 11:44:31 AM6/3/09
to python_inside_maya
Hi Ofer!

thank you for the reply!

can you please describe this method(from operator import add)
how can you divide array of points by int?

if i'm using arrMesh.getFaceNormalIds(faceIndex=1)
i can get array of 4 vertexes(because my mesh is rectangle) [0, 1, 2,
3], but how i can get the middle point of this array?

best!
Dimitry

Dimitry

unread,
Jun 3, 2009, 1:14:09 PM6/3/09
to python_inside_maya
ok i got it

for item in arrCount:
objFace=arrMesh.getPolygonVertices(arrCount[item])
pt1=arrMesh.vtx[objFace[0]].getPosition()
pt2=arrMesh.vtx[objFace[2]].getPosition()
centerPt=[ (pt1.x+pt2.x)/2,(pt1.y+pt2.y)/2,(pt1.z+pt2.z)/2 ]

thank you Ofer and Chad for great support!
Best!
Dimitry

Dimitry

unread,
Jun 3, 2009, 2:11:43 PM6/3/09
to python_inside_maya
Ofer, Chad,

but anyway it's interested how it's possible to get mesh middle point
- centroid of each poly in a mesh

it is possible to get it in the similar way as arrMesh.f[0].getNormal
()
arrMesh.f[0].centroid() - something like this?

all the best!
Dimitry

Ofer Koren

unread,
Jun 4, 2009, 2:37:25 AM6/4/09
to python_in...@googlegroups.com
The nice thing about using pymel is that it returns objects that usually support common operations in a more intuitive form.
So in this instance meshFace.getPositions() returns a list of pymel Vector objects, which support operations such as addition, scalar division and multiplications (so you don't need to go element by element, x-y-z)

from pymel import *
v1 = Vector([1,2,3])
v2 = Vector([2,3,4])

print v1 + v2
print v1 * 2
print v2 / 2

Google up info about the built-python in function 'reduce' and the 'add' function from the built-in 'operator' module you'll see how those two together can take a list of objects and add them all up together... divide the result by a scalar value and you've essentially calculated the centroid of a list of Vectors.

Dimitry

unread,
Jun 4, 2009, 5:38:05 AM6/4/09
to python_inside_maya
i put
print arrMesh.faces[0].getPositions()
or
print arrMesh.faces[0].getPosition()

and got

# AttributeError: 'MeshFace' object has no attribute 'getPositions' #

and checked at pymel docs there is just getPosition for vertexes but
not for faces

if i pasted
from pymel import *
v1=Vector([1,2,3])
v2=Vector([2,3,4])
print v1+v2
print v1*2
print v2/2

I got error :(
# Error: name 'Vector' is not defined
# Traceback (most recent call last):
# File "<maya console>", line 2, in <module>
# NameError: name 'Vector' is not defined #

i'm confused, i'm using latest pymel update on mac






Ofer Koren

unread,
Jun 4, 2009, 11:02:00 AM6/4/09
to python_in...@googlegroups.com
Sorry, it's actually:

print arrMesh.faces[0].getPoints()


and try

from pymel import Vector

(I'm used to pymel 0.7.9, things might be a bit different with 0.9...)


- Ofer
www.mrbroken.com

Olivier Renouard

unread,
Jun 5, 2009, 8:43:51 AM6/5/09
to python_in...@googlegroups.com
In pymel.core.datatypes you have the "center" function (working on Points)
center(p, *args)
center(p[, q, r, s (...)]) –> Point Returns the Point that is the center of p, q, r, s (...)
Also available as a method of the class Point

center
(*args)
p.center(q, r, s (...)) –> Point Returns the Point that is the center of p, q, r, s (...)
Didn't add a weighted / barycenter method but that could be done.

Something related that might be interesting to note, because it's not provided by the Maya API or that easy to compute in a nice way and often useful :

bWeights(*args)
p.bWeights(p0, p1, (...), pn) –> tuple Returns a tuple of (n0, n1, ...) normalized barycentric weights so that n0*p0 + n1*p1 + ... = p. This method works for n points defining a concave or convex n sided face, always returns positive normalized weights, and is continuous on the face limits (on the edges), but the n points must be coplanar, and p must be inside the face delimited by (p0, ..., pn)
Think of an inverse barycenter, for a point p0 in a face and the p1...pn points of the face, will return normalized positive weights (not all algorithms ensure that). Works for any number of points face, convex or concave as long as it's planar (but the error you get for not being perfectly planar builds up in a continuous way, so still usable for near planar faces), and it's continuous at the limites (when point p0 is actually on a edge).




-- 
Olivier Renouard

Dimitry

unread,
Jun 8, 2009, 7:15:33 AM6/8/09
to python_inside_maya
Hi Oliver,

thank you for you answer! that's what i need! But it seams not to work
on my mac :(
if i put

from pymel import *
a=[-1,1,0]
b=[-1,-1,0]
c=[1,1,0]
d=[1,-1,0]
center([a,b,c,d])
print center

i got:
# Error: name 'center' is not defined
# Traceback (most recent call last):
# File "<maya console>", line 6, in <module>
# NameError: name 'center' is not defined #

i tried also Ofers suggestion
v1 = Vector([1,2,3])
v2 = Vector([2,3,4])
print v1 + v2

with a same effect:
# Error: cannot import name Vector
# Traceback (most recent call last):
# File "<maya console>", line 1, in <module>
# ImportError: cannot import name Vector #

and what means *args?

All the Best!
Dimitry

Olivier Renouard

unread,
Jun 8, 2009, 8:06:46 AM6/8/09
to python_in...@googlegroups.com
Hi,

It should read :

from pymel import *
a=[-1,1,0]
b=[-1,-1,0]
c=[1,1,0]
d=[1,-1,0]
print center(a,b,c,d)

or

from pymel import *
a=[-1,1,0]
b=[-1,-1,0]
c=[1,1,0]
d=[1,-1,0]
ctr = center(a,b,c,d)
print ctr

Does that work ?

*args indicates a variable number of arguments, so center(p, *args) takes at least one argument, and possibly as much as you want (all the points of the face), so :

print center() would complain
print center(a) would print a
print center(a, b) would print the center of a and b thus (a + b)/2

etc.

If you can't import Vector then you probably have a pymel installation
problem (wrong path in PYTHONPATH or the like).

Olivier
--
Olivier Renouard

Dimitry

unread,
Jun 9, 2009, 8:18:05 AM6/9/09
to python_inside_maya
hi Olivier,


no it's still not working, my pymel/maya don't realize that center is
a function of pymel, i'll try to reinstall pymel at the weekend, but i
think center function will be a righte one,
i scripted

objFace=arrMesh.getPolygonVertices(arrCount[item])
pt1=arrMesh.vtx[objFace[0]].getPosition()
pt2=arrMesh.vtx[objFace[2]].getPosition()
centerPt=[ (pt1.x+pt2.x)/2,(pt1.y+pt2.y)/2,(pt1.z+pt2.z)/2 ]
pt1=arrMesh.vtx[objFace[1]].getPosition()
pt2=arrMesh.vtx[objFace[3]].getPosition()
centerPt2=[ (pt1[0]+pt2[0])/2,(pt1[1]+pt2[1])/2,(pt1[2]+pt2[2])/2 ]
pt1=centerPt
pt2=centerPt2
centerPtFace=[ (pt1[0]+pt2[0])/2,(pt1[1]+pt2[1])/2,(pt1[2]+pt2[2])/
2 ]

i think it's the same with pymel.center

Anyway Chad, Ofer and Olivier thank you very much for great support,
my debut in pymel was very successful.

All the Best!
Dimitry
Reply all
Reply to author
Forward
0 new messages