VEX help: Array with neighbor point position that updates!

   1005   2   1
User Avatar
Member
2 posts
Joined: 10月 2022
Offline
Hi,

I've been breaking my head with the following problem.
Goal: to make a procedural bookshelf. At a certain point the user can choose how many vertical planks he wants. After he has chosen the amount he should be able to them between their neighboring vertical planks. Basically: to move a point along a line between it's left and right neighbor. It cannot go past it's left and right neighbor.

Setup: line with length 1 in z direction form 0 to 1. Resampled and has 5 points.

// loops over points to find neighbors
// adds the z position of each points neigbor to an array
// writes that array as an attribute to the point
for(int i = 0; i < npoints(0); i++)
    {       
        float nearpos[] = array();
        vector pos_one  = point(0, "P", i - 1);
        vector pos_two  = point(0, "P", i + 1);
        
        insert(nearpos, 0, pos_one.z);
        insert(nearpos, 0, pos_two.z);

        setpointattrib(0, "nearpos", i, nearpos);
    }
    

Clearly I'm doing something wrong as this gives me a static array of positions. Where i to change the position of one point, the array does not get updated. Would love some help on this.
P.s: I know I can do this with for loop nodes, but I'm practicing VEX and would like to find a VEX solution.

Attachments:
Screenshot 2022-12-11 110505.png (1.0 MB)

User Avatar
Member
900 posts
Joined: 2月 2016
Offline
Not sure I get what you are trying to do, but see if this works:
(note, it must run inside a point wrangle)
int neighs[] = neighbours(0, @ptnum);
f[]@nearpos;
foreach(int pt; neighs) {
    vector pos = point(0, "P", pt);
    append(f[]@nearpos, pos.z); }


The code you provided seems to have few issues:
  1. You are running it in detail wrangle? You could do it inside a point wrangle and be more efficient (as point wrangle is multithreaded)
  2. You reset the array on each iteration, by declaring it inside the for-loop.
    Try do it outside and before the for loop.
  3. When using the setpointattrib() function to update attribute arrays it's better to specify the `mode` argument with "append", usually. Otherwise on each iteration it would just set a new array and overwrite the previous. If not specified, the `mode` argument is defaulted to `set`, I believe.
  4. Also, consider moving outside and after the for-loop that setpointattrib() function.
  5. Also, consider taking care of 0 and last point, since you are trying to access the previous and following point of each point.

Edited by Andr - 2022年12月12日 18:00:57
User Avatar
Member
2 posts
Joined: 10月 2022
Offline
Hi Andr, thank you so much for your reply and sorry about the late reply.

I'm still new to scripting and your pointers have really helped me out in understanding the run over options better.
I haven't been able to solve the problem with VEX yet, but have found a workaround without using VEX. I might update this if I ever figure it out.

Thanks again.
  • Quick Links