oneXone Posted June 30, 2023 Share Posted June 30, 2023 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. Quote Link to comment Share on other sites More sharing options...
animatrix Posted June 30, 2023 Share Posted June 30, 2023 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 ] ); } 2 Quote Link to comment Share on other sites More sharing options...
oneXone Posted June 30, 2023 Author Share Posted June 30, 2023 (edited) 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 July 1, 2023 by oneXone Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.