[Bf-python] Re: wishes about colorbands

alex mole nal at blueyonder.co.uk
Wed Mar 31 10:48:16 CEST 2004


Hi Joseph

Are you talking about using PyMem_Malloc to create the CBData that will 
be stored within Blender, or just copies whose data gets copied to the 
Blender-allocated objects?

Either way, I can see problems [there are always problems :)]

With the first, if the user creates a ColorBand through the UI, it will 
have been allocated with MEM_callocN, so won't be allocated compatibly.

With the second, the problem is much smaller: it only becomes an issue 
of persistance and synchronisation. If the BPY object is stored in 
Blender.Registry, and the actual Blender ColorBand's data is changed 
between runs, the object won't get automatically updated. I guess that 
could be solved by making the API:

- create new python ColorBand objects and create the associated CBData 
simultaneously [this would probably mean that PyMem_Malloc isn't even 
needed, as the destructor could simply free the data]

- have get/set routines to copy the data from/to individual textures 
[not to "assign" the python ColorBand object with the Texture- changing 
the ColorBand after would not change the Texture's CB data]


Such an implementation would make Nicolas' ColorBand dictionary easy to 
write :)


Obviously this isn't a 100% ideal solution though. If we really wanted 
to represent the underlying Blender code with two-way updating, it might 
be possible to have the python object store a pointer to the texture, 
and check the texture's COLORBAND flag before trying to access the 
colorband memory... But then it would be impossible [or hacky] to create 
ColorBands who weren't associated with textures :(


Alex


Joseph Gilbert wrote:
> I believe that when colorbands are added internally, memory is allocated for
> them (CBData objects) with MEM_callocN which will increment the totblock
> count in blender. However, you can also use PyMem_Malloc as well and in this
> case python will take care of the memory management. All pointers allocated
> with PyMem_Malloc will be cleaned up through python's GC.
> The best way to do this it seems would be to create a 1 element list that
> contains a colorband python object -> [PyColorband] or [].  You could create
> a function to append Python colorbands to a texture eg.
> text->attachColorband(myBand) - which would append the colorband to the
> texture's list.
> This way your not passing pointer references around to data objects that
> could disappear. The allocated pointers would be in the PyColorband object
> and managed by python (rather then through blender).
> The allocated data would be PyMem_Malloc created for the colorband, the
> PyColorband attached inside a PyList to the PyTexture. This will keep the
> PyColorband associated with the texture until you remove the PyColorband
> from the PyList.
> Anyway that's my thoughts.
> 
> Joseph
> 



More information about the Bf-python mailing list