Skip to content
Advertisement

how to create empty c type array (or just one NULL value) in python?

I was wondering whether (and how) one can create empty c type arrays in python. The following code works fine if I want to initialize an array with zeros:

from ctypes import *
data = [0 for i in xrange(10)]
a = ((c_float *10))(*data)

If data is a list of None values however, I get an error message:

data = [None for i in xrange(10)]
a = ((c_float *10))(*data)

TypeError:  float expected instead of NoneType instance

is there a way to initialize and array with NONEs or NULLs?

EDIT 1: the reason why I want an array with None / Null is that I want to upload this array to the GPU. If I initialize my array with Zeros, then zeros will be rendered. However if I initialize it will NULL (as defined in c), nothing is rendered.

EDIT 2: the reason for using c_float is that I need to cast my data to c_float in order for it to be compatible with opengl.

EDIT 3: if data is made of zeros, then the following code will render me a bunch of points @ location 0 / 0. I was hoping that sending NULL values to the GPU will not result in any points being rendered.

glBindBuffer(GL_ARRAY_BUFFER, vbo)    
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(data), data)
glVertexPointer(n_COORDINATES_PER_VERTEX, GL_FLOAT, 0, 0)
glDrawArrays(GL_LINE_STRIP, 0, len(data))

EDIT 4: Another use-case when you might want to use NULL in python: according to the opengl website, the following is how to allocate memory on the GPU:

glBufferData(GL_ARRAY_BUFFER, SizeInBytes, NULL, GL_STATIC_DRAW)

If I do this in my python code, I get the following error: NameError: global name 'NULL' is not defined

but there must be a way to do this in python, right?

Advertisement

Answer

A c_float has no equivalent value for ‘None’, so you can’t store None in a c_float.

See the table in the python ctypes docs – there are some types that support None – but none that support both floats and None.

You will have to use an alternate data type, record None values separately somehow, or find a way of representing None as a float that works for your application.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement