[Bf-python] Iterators in blender data

Campbell Barton cbarton at metavr.com
Fri Sep 15 03:20:20 CEST 2006


When making the metaballs api I added an iterator to the metaball type 
called .elements

mb= Metaball.Get('someMb')
for ml in mb.elements:
  print ml.co
  print ml.type
  print ml.radius

new_el= mb.elements.add()

But this got me thinking. just as we may not use scn.objects.add() in 
favor of scn.add()

why not make the metaball type an iterator its self?

for ml in mb: print mb
or len(mb)

The only bad thing about this is that you cant assign a list.

Since apart of my plans are to make the API's consistant. what about 
doing the same thing for groups.

gp= Group.Get('Group')
for ob in gp.objects:
  print ob.name

could be....
for ob in gp:
....

In this case, having 'objects' is usefull because you can assign objects 
to it.
gp.objects = [ob1, ob2, ob3]

if objects was removed, a solution might be a 2 step process.

gp.clear()
gp.extend([ob1, ob2, ob3])


The only reason not to do this is for data that may have more then 1 
type of iterable data. - like mesh for eg.
but for Metaballs, Groups, Modifiers and Scenes it should be ok.
(Im unsure about scenes because they contain a fair bit of data, but 
objects are the most commonly used iterables they contain so I guess its ok)
 
The one bad thing is it makes the code a bit less readable especialy if 
you done have good variable names,
for E in M: ...
for O in g: ...

for E in M.elements: ...
for O in g.objects: ...

- Cam




More information about the Bf-python mailing list