# Rows to columns

## Recommended Posts

I'm still a novice in Houdini. Help me understand how to swap the rows and columns of primitive indices. It is necessary that I can rotate the indexes, as shown in blue.

##### Share on other sites

Try dropping down a Sort node and place it in Shift mode. Then you can offset primitives with the slider.

Edited by Atom
##### Share on other sites

1 hour ago, Atom said:

Try dropping down a Sort node and place it in Shift mode. Then you can offset primitives with the slider.

Sort Node gives the wrong result. I do not need to shift the indexes, but rotate them 90 degrees. Node Sort works well with Grid, you can specify which axis to build columns, but for the torus it does not fit. I think that this can be implemented in the VOP. But I do not know.
Edited by Andrew Kant
##### Share on other sites

Compute index attribute:

```// Primitive wrangle.
int rows = chi("../torus1/rows");
int cols = chi("../torus1/cols");
i@index = @primnum % cols * rows + @primnum / cols;```

Then Sort by attribute:

By the way, this is reshape and transposition. Not 90 degree rotation:

```| 0  1  2  3  4  5|
| 6  7  8  9 10 11|
|12 13 14 15 16 17|

| 0  3  6  9 12 15|
| 1  4  7 10 13 16|
| 2  5  8 11 14 17|```

Edited by f1480187
• 1
• 1
##### Share on other sites

1 hour ago, f1480187 said:

Compute index attribute:

```
// Primitive wrangle.
int rows = chi("../torus1/rows");
int cols = chi("../torus1/cols");
i@index = @primnum % cols * rows + @primnum / cols;```

Then Sort by attribute:

By the way, this is reshape and transposition. Not 90 degree rotation:

```
| 0  1  2  3  4  5|
| 6  7  8  9 10 11|
|12 13 14 15 16 17|

| 0  3  6  9 12 15|
| 1  4  7 10 13 16|
| 2  5  8 11 14 17|```

Many thanks! That's exactly what I wanted. I was also advised to use the Transpose node. Can you know how you can apply it for this task?

##### Share on other sites

It's limited to Houdini's native 2×2, 3×3 and 4×4 matrices, and it probably used for shading and 3D transformation tasks. We can't really use it to transpose r×c matrix. Simple numerical algorithm in VEX did the job too.

For the sake of completeness, to work with arbitrary-sized matrices in Houdini use numpy:

```import numpy as np

rows = hou.evalParm('../torus1/rows')
cols = hou.evalParm('../torus1/cols')

# Make initial matrix representation.
old_sort = np.arange(rows * cols).reshape(rows, cols)

# Reshape and transpose.
new_sort = old_sort.reshape(cols, rows).T

# Make index attribute.
node = hou.pwd()
geo = node.geometry()
geo.setPrimIntAttribValues('index', new_sort.ravel())```

swap_rows_cols_python.hipnc

## 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.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

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

×
×