Getting locations of points with Python is slow

   4571   3   0
User Avatar
Member
9 posts
Joined: 4月 2015
Offline
Hi,

I am trying to collect the locations off all points in a mesh with python. Currently I am doing that with the following line:

MyNumPoints = len(MyNode.geometry().points())

for i in range(MyNumPoints):
hou.node.geometry().points().position()


The script gets exremely slow on meshes that have around 1000 points or more.
Is there a better way to access point positions via Python?

Or is it perhaps better to use hscript or VEX for something like that?
User Avatar
Member
1922 posts
Joined: 11月 2006
Online
Your method is slow because every time you call points() it has to build hou.Point objects for every point before you can index it. A faster method would be iterPoints() which doesn't do this and allows fast random access instead. However that is still not the best way.

The easiest better way will be to just iterate over the points directly:
for point in node.geometry().points():
position = point.position()

The fastest possible way involves using hou.Geometry.pointFloatAttribValues(“P”) to get a raw list of all the position values and using the numpy module to reshape them into tuples of 3.

It all depends on what you're trying to do.
Graham Thompson, Technical Artist @ Rockstar Games
User Avatar
Member
9 posts
Joined: 4月 2015
Offline
Thank you, this is a lot faster.

I am interested in pointFloatAttribValues. How do I find out which other Attributes besides “P” I can use in pointFloatAttribValues?
I checked in the manual but I can not find a list of Attributes. Hadn´t you told me I would never have known that “P” works. How would one find that out?
User Avatar
Member
1922 posts
Joined: 11月 2006
Online
It should work on any numerical attribute.
Graham Thompson, Technical Artist @ Rockstar Games
  • Quick Links