[Bf-python] module consts / dictionary consts

Joseph Gilbert jgilbert at TIGR.org
Mon Aug 15 19:56:33 CEST 2005


Ken Hughes wrote:

>
> Well, we're starting to see other module constants as well, based on 
> ascotan's wiki proposal (e.g., Blender.Text3d.MIDDLE={'name': 
> 'Text3d.MIDDLE', 'value': 1}).  That's where I was confused; does 
> anyone envision making dictionaries of these? (e.g., 
> Blender.Lamp.Types['Spot'] = {'name':Lamp.SPOT', 'value':2})
>
That's another way i guess :0
So you can do:
1. Blender.Module.CONSTANT (where CONSTANT is a PyConstant Object that 
contains {name='x', value='1'})
2. Blender.Module.constants['CONSTANT'] (where 'constants' is a 
dictionary of {name='CONSTANT', value='1'})
3. Blender.Module.constants['CONSTANT'] (where 'constants' is a 
dictionary of {name='CONSTANT', value=PyConstant} - where PyConstant = 
{name='x', value='1'})

-------------------
The advantage of 1:
- Your comparing a PyObject 'instance' e.g.
if object.attribute is Module.CONSTANT
this is an instance comparison of the object and is value independant 
i.e. it makes no different what the value of the constant is.

The disadvantage of 1:
- You can't use this like a dictionary. You have to do something like:
if myObject.attriubte is Module.C1 or myObject.attribute is Module.C1 or 
myObject is Module.C3, etc....
-------------------
The advantage of 2:
- You can get constants in a dictionary like fashion e.g.
for Constant in Module.constants.values():
...if object.attribute == Constant:
......print 'woot!'

The disadvantage of 2:
- This is value dependant. Your comparing by the numeric value of the 
constant. The problem here is that people can do this:
if object.attribute == 25....
Which is not what is intended but easily done. This is because when you 
write your comparison code you will have to get the 'value' from the 
constant dictionary. This value is not an object. It's an integer value 
which your comparing. So people can do number tricks to set flags, etc 
that the API should not be exposing. This being the primary reason for 
recommending the object instance comparison above.
--------------------
The advantage of 3:
- You can do both of the above. Your comparing by instance and can use a 
dictionary syntax.

The disadvanatge of 3:
- You have to generate a bunch of constant object and store them in a 
dictonary (also a disadvantage of 1)

Therefore the best solution maybe 3 where people can use a dictionary 
syntax AND be instance comparing constants instead of doing number 
tricks with flags.

As a side note to lamp.c:
We should never, never redefine blender source #defines like:
#define EXPP_LAMP_MODE_NODIFFUSE  2048
#define EXPP_LAMP_MODE_NOSPECULAR 4096
This is bad, bad, bad. The constants should use the ones defined in 
DNA_lamp_types.h unless these constants are nowhere defined or there is 
some sort of naming confict.



More information about the Bf-python mailing list