Jump to content

detail wrangle: nested loops question


oneXone

Recommended Posts

for(int j=0;j<chi("iterations");j++){
    for(int i=0;i<@numpt;i++){
        vector pos = point(0, "P", i);
        pos.x += chf("iterationdist");
        setpointattrib(0,"P",i,pos);
    }
}

Why does this code not work as expected in a detail wrangle? When "iterations" is 1 or more, it always moves each point by "iterationdist" only once.

Link to comment
Share on other sites

Hi,

The problem is related to the way Houdini updates point attributes during node execution. When you're modifying point positions within a loop and reading the updated values in the same node execution, the result may not be as expected.

When you perform operations on points using functions like point() or setpointattrib(), Houdini doesn't immediately update the geometry with each of these changes. Instead, these changes are queued and applied at the end of the execution of the node. So if you change a point attribute and then attempt to read that attribute later in the same execution, you end up reading the old value, not the updated one.

In your specific case, the nested for-loops are intending to increment the x component of each point multiple times. However, since the updated positions aren't applied immediately, the point() function keeps reading the original positions, not the positions from the previous iteration. As a result, only the last iteration effectively gets applied.

To resolve this, you can introduce an intermediate data structure, like an array, to store and update the positions. After all the updates, you can apply the final positions back to the points using setpointattrib().

Here's one way:

int count = @numpt;
vector pos [ ] = array ( );

// Store initial positions
for ( int i = 0; i < count; i++ ) {
    vector p = point ( 0, "P", i );
    append ( pos, p );
}

// Iterate and update the x position
for ( int j = 0; j < chi ( "iterations" ); j++ ) {
    for ( int i = 0; i < count; i++ ) {
        pos [ i ].x += chf ( "iterationdist" );
    }
}

// Set updated positions
for ( int i = 0; i < count; i++ ) {
    setpointattrib ( 0, "P", i, pos [ i ] );
}

 

  • Like 2
Link to comment
Share on other sites

Thank you! It's usually better to use the straightforward approach (a point wrangle in a "for-loop with feedback") to get multithreading, but this technique is good to know too.

Edited by oneXone
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...