Accessing 3D Textures in Script TOP via numpyArray()

Hello TouchDesigner Community,

I am working with the Script TOP and am trying to access a 3D texture created by stacking multiple images. Specifically, I would like to retrieve a numpy array with the shape (N, res_x, res_y, 3), where N represents the number of images, and res_x and res_y are the dimensions of each image with 3 RGB channels.

Currently, I am using an OP Viewer to flatten the images and then extracting the data in Python inside the Script TOP. However, this method is quite inefficient in terms of performance.

Is it possible to use the numpyArray() method to directly access such a 3D texture from the Script TOP? If so, I would appreciate any guidance or examples on how to achieve this.

Thank you for your help or suggestions

Hi @LivingSparks,

I don’t think that is currently possible. Added it as an RFE.

Best
Markus

Thanks @snaut !

For now I can share my implementation inside the Script TOP

def extract_patches(image, patch_size=224, rows=4, cols=4):
    height, width, channels = image.shape
    patches = []
    for row in range(rows-1, -1, -1):  # Start from bottom row
        for col in range(cols):
            y_start = row * patch_size
            x_start = col * patch_size
            patch = image[y_start:y_start+patch_size, x_start:x_start+patch_size, :]
            patches.append(patch)
    return np.array(patches)

I had a similar use case recently. My approach was to use a GLSL top to extract one slice of the 3d texture at a time. I parametrized the depth of the slice, then wrote a script that loops through the depth values, and for each one appends the result of op.numpyArray() to a larger w x h x d x 4 array. Not sure if the performance is any better than flattening with opViewer and dividing into patches, but this should get around resolution limits for those using the non-commercial version, if I understand correctly.

In addition to reading from 3d textures to a numpy array, it would also be great if we could write to 3d textures from an array with copyNumpyArray()!

1 Like

That’s fantastic! I’m no longer working on that project, but if possible, would you consider sharing it with the community?

This would be amazing, Imagine 3D volumetric shaders…

Hm, I don’t think I have enough credit on this forum yet to post a link to the file. But happy to share if anyone reaches out.

Exactly, was inspired by Josef Pelz’s T3D family to make my own version. Working nicely so far!

Here we are:

3d_texture_to_numpy.toe (7.9 KB)

This is a simple demo of the “slice” approach outlined above. Including a simple handy tox for selecting a u, v, or w slice from a 3d texture by normalized parameter.

1 Like

Hey,
just tuning it here. The current version of T3D includes a toTOPT3D, which basically converts a 3D texture to either on slice of it or a flattended version of the whole thing. Not sure if that’s what you’re looking for.

2 Likes

In builds 2023.31979 and later, numpyArray() will now return the whole 3D texture with a shape (depth, height, width, 4)