Jump to content
Sign in to follow this  

mimic for each 'attribute value' in python

Recommended Posts


i would like to know if there is a way to loop through points via an attribute value , like the ForEach node does when it s setup on 'each attribute value' . i already know how to make simple loops , but i m stucked in the way to do that via an attrbute value stored in the points.

if someone has some ideas , please share :)



Edited by sly

Share this post

Link to post
Share on other sites

## I'm kinda new to this as well.

## Is there a better way?

import hou



p=g.points() # get list of points geo

# pre-get attributes if you wanna find out what's available?


a_pt = p[0]

pt_geo = a_pt.geometry()

l_attrs = pt_geo.pointAttribs()

print "attribues:",l_attrs

#### now go through points for whatever attrib value..

## point position in this simple case.

print "size p:",len(p)

for pt in p:

print i

i +=1

# For now, just print attribute "P"

print pt.attribValue("P")

Edited by moomarkus

Share this post

Link to post
Share on other sites

so i create a dictionary this way :

pts = geo.points()

dico = {}

for pt in pts:

ptNum = pt.number()

attrValue = pt.attribValue("class")

dico[ptNum] = attrValue

is this the correct way ?, i m very noob in python.

now i have to find how to use this.

Edited by sly

Share this post

Link to post
Share on other sites

thank you a lot for the example petz , very helpfull !

Share this post

Link to post
Share on other sites

I threw this together real quick in an attempt to make something that would try and handle things as efficiently as possible. As it requires numpy and collections so it might not work on all platforms unless those modules are available. It also doesn't support string attributes.

This function returns a defaultdict object whose keys are unique attribute values and whose indices are any entity number(s) whose values correspond to the key. It supports points or primitives of any size.

from collections import defaultdict
import numpy

def getPartitionedIndices(attribute):
    attribType = attribute.type()
    dataType = attribute.dataType()
    name = attribute.name()
    size = attribute.size()

    # Can't handle string attributes.
    if dataType == hou.attribData.String:
        raise hou.OperationFailed("Attribute cannot be a string attribute.")

    if attribType == hou.attribType.Point:
        values = numpy.fromstring(attribute.geometry().pointFloatAttribValuesAsString(name), dtype=numpy.float32)

    elif attribType == hou.attribType.Primitive:
        values = numpy.fromstring(attribute.geometry().primFloatAttribValuesAsString(name), dtype=numpy.float32)

        raise hou.OperationFailed("Invalid attribute type for {0}.  Must be point or primitive.".format(name))

    # Convert to integers if necessary.
    if dataType == hou.attribData.Int:
        values = values.astype(numpy.int)

    ddl = defaultdict(list)

    # Handle attributes whose size is > 1.
    if size > 1:
        # If the size is not 1 we need to reshape the flat array to group components together.
        values = numpy.reshape(values, (float(len(values)) / size, size))

        for idx, val in enumerate(values):
            # Need to convert away the numpy-ness and create at tuple which is hashable.
        for idx, val in enumerate(values):

    return ddl

  • Like 1

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this