Selecting objects of highest hierarchy level

Hello guys, I am still learning a lot about Python and I would like to ask for some help. I would like to somehow get rid of any child objects present in my selection and work only with objects of highest hierarchy level.

I have found operators which select highest parents but parents only, if there are other objects which have no children they are unselected and I would like to keep these selected.

I also found an operator do it the other way around, find any child type in my selection so I can hide them… and this works the best but there is one scenario where it does not work and is if there is no child present in my selection. Then all stays selected and then is hidden by the next operator.

I would appreciate if someone helped me with this :smiley:

Loop through your selection and test for parent == None.
Keep all that are True

for i in selection:

if i.parent == None:

keep in Selection
remove from Selection

Thanks for writting back! just found out Blender cloud has an introduction to scripting and just found how simple this was… thanks!

I am trying to look at the API documentation but I cannot find the correct term to check for the parent/child. How can I access the parent information? is it only through item? should I change then how am I creating the loop?

for ob in bpy.context.selected_objects:
if ?(object parent) = ? (true?)

import bpy

mySelection = bpy.context.selected_objects

for ob in mySelection:
    if ob.parent == None:

thanks for taking your time to write this. May I just ask where do you find all these ob. attributes? I could not find them in the API documentation.

In the python console (not the text editor) you can enter something and then press tab. You will then get a list of possible stuff.

For example if you select an object and enter:
bpy.context. and then press tab

You will get a list of stuff you can do in this context.
Here you can see for example “active_object”

So next you enter:
followed by tab and you will get another list.

And this is how I just slowly work through the long list of stuff.

BTW, you can also enter unfinished stuff, such as:

Note that ob is not something predefined. You could use anything else in this loop.
using “i” is a common name for stuff in loops because it is short for “iterator”. But you could also use “HeckiHeckiHeckiPatum” instead.