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

Question:

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?

Asked By: memyself

||

Answers:

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.

Answered By: Gareth Latty

What does it mean to have null value for c_float? in C null is zero, so basically you want to have a array of zeroes, which you can do by converting array of Nones e.g.

from ctypes import *
data = [None for i in xrange(10)]
# convert to zeroes
data = [i or 0 for i in data]
a = ((c_float *10))(*data)

Edit: looks like you don’t want to draw anything if values are None, in that case there is no way to have c_floats with ‘nothing’ as NULL is 0, best way would be to set array as NULL and not draw anything using if condition

Answered By: Anurag Uniyal

Do you want to pass a NULL array pointer via ctypes? Use None for NULL. You can’t have a NULL float value, but you can have a NULL float array pointer.

And like @Anurag said, if you don’t want to draw anything, don’t call the function to draw.

Answered By: Mark Tolonen
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.