String data from list

Hello everyone,

I am just getting my feet wet using python in Blender, and have a question regarding strings. First of all, the goal of the first part of the program is to find the names of all objects whose names end in a particular set of characters. For example, I would like to find all objects named XXXXXpOut, where XXXXX = any string.

My thinking for doing this is to create a list of all the objects in the current scene, then test for the name condition.

When I use the following:

import Blender
from Blender import Scene, Object, Camera
myData = Blender.Scene.getCurrent().getChildren()
print myData
myObjCount = len(myData)
print myObjCount
myStr = myData[1]
print myStr

The output is:

[[Object “Cube”], [Object “Lamp”], [Object “Camera2”]]
3
[Object “Lamp”]

Now, I would like to find the length of myStr. When I use the following:

myStrLen = len(myStr)

I get:

Traceback (most recent call last):
File “Text.003”, line 9, in ?
TypeError: len() of unsized object

Is there something special about the list created using Blender.Scene.getCurrent().getChildren()? I have tried creating my own list manually, all entries are text strings in quotes, and the logic works fine. I suspect that because each entry in the list that is created using Blender.Scene.getCurrent().getChildren() has imbedded quotes [Object “Lamp”], I will have to do something different, but I’m not sure what.

Any and all help greatly appreciated!!

Well currently you’re just getting the actual objects.

You want to get the object names for what you want to do.

So to create a neat list of all objects in the current scene


objectlist = [ob for ob in Blender.Scene.GetCurrent().getChildren()]

You can now loop through the list, get the names and do whatever you want.


## Loop though
for a in range(len(objectlist)):
	## Get the name
	name = objectlist[a].getName();

	## Get the length of the name
	check = len(name);

	## Print out the object nr in the list, the name, and it's length
	print a, name, check;

Is there something special about the list created using Blender.Scene.getCurrent().getChildren()?

The returned list is a list of Objects. not strings. Pay attention to the documented return type of methods!

To get your list of objects, you can simply do


ob_list = [ ob for ob in Blender.Scene.getCurrent().getChildren() if ob.name.endswith('pOut') ]

Note: you do not need range(len()) to iterate over a list now. This works just fine:


for a in objectlist: 
      name = a.getName()

Thanks for the help stiv and macouno!!

So, I tried to back track, taking your info and trying to find documentation for it. I found lots of examples of “ob for ob”, but no documentation. If you have the time, could you please explain what is going on when using:

ob_list = [ ob for ob in Blender.Scene.getCurrent().getChildren() if ob.name.endswith(‘pOut’) ]

Thanks for your patience!!