Tips for finding base structures in the API

I often have trouble with the Blender API to figure out the “base structure” required for a particular setting.

For example, I wanted to set “show_relationship_lines = False”. With google, I arrived pretty quickly to the API page for View3DOverlay, and determined that I need to set SOMETHING.show_relationship_lines = False.

It then took an embarassingly long time googling various other settings on this API page to find some example code on stackexchange where somebody set some other setting, and I finally discovered that SOMETHING in this case needs to be context.space_data.overlay.

SOMETHING = context.space_data.overlay     # this was difficult to find
SOMETHING.show_relationship_lines = False

So my question is, is there a way to use the API better to more efficiently figure out the actual python code required as the base structure on any given API page?


context.space_data is just a shortcut to the current space for that context. You already found the api page for view3doverlay- scrolling all the way to the bottom of that page you can find a section called ‘references’ and see that SpaceView3D has a member called ‘overlay’ that is a View3DOverlay object.

Lets assume for a moment that we don’t know that context.space_data is a shortcut to a view3d’s SpaceView3D space. How do we know how to access this info? At the bottom of SpaceView3D’s api docs there is another section called ‘references’ that takes us to Area.spaces. If you scroll down (you see where I’m going with this), you’ll see a reference to context.area

At this point, you should be in the ballpark of knowing where you’re at and can infer the information you need with a bit of testing. For example, you could look at bpy.context.area.spaces and see if any of the spaces in that collection are a SpaceView3D type, and if so, access the overlay attribute. Once you get a bit more confidence you may start to notice that areas of certain types always have spaces of certain types, so you could just jump directly to something like this:

if area.type == 'VIEW_3D': 
    area.spaces[0].overlay.show_relationship_lines = False

Or, while you’re investigating context.area you may come across context.space_data and realize that’s exactly what you need.

Regardless, all of the information is in the API docs and it’s all interconnected. Using your original search term and api page result I can trace a direct path from there to ‘understanding’ just by following references.

1 Like