how to use python to get centroid xyz of a polygon

   9630   5   1
User Avatar
Member
479 posts
Joined: 12月 2009
Offline
I want to get the centroid xyz and normal xyz of a series of polygon.

I used the following in a python node but it didn't work…

prims = hou.pwd().geometry().prims()

for prim in prims:
x = prim.attribValue(“P”)
print x

advise is appreciated!

- Ji
User Avatar
Member
1909 posts
Joined: 11月 2006
Online
position = sum((vert.point().position() for vert in primitive.vertices()),
hou.Vector3()) * (1.0 / primitive.numVertices())

normal = primitive.normal()
Graham Thompson, Technical Artist @ Rockstar Games
User Avatar
Member
479 posts
Joined: 12月 2009
Offline
Thank you very much, graham!

learned new trick, again!

- Ji
User Avatar
Member
479 posts
Joined: 12月 2009
Offline
the use of the sum() function as explained in:
Code Like a Pythonista: Idiomatic Python

http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html [python.net]

- Ji
User Avatar
Member
1743 posts
Joined: 3月 2012
Offline
Keep in mind that for polygons other than triangles and parallelograms, that gets you the centroid if the mass is equally distributed among the vertices, not the centroid if the mass is equally distributed over the area, which is a lot harder to compute. Of course, for many purposes, it doesn't matter which you use; just thought I'd let ya know!
Writing code for fun and profit since... 2005? Wow, I'm getting old.
https://www.youtube.com/channel/UC_HFmdvpe9U2G3OMNViKMEQ [www.youtube.com]
User Avatar
Member
479 posts
Joined: 12月 2009
Offline
noted with thanks, ndickson! - Ji
  • Quick Links