New to bpy - where do the bpy packages live?

Hi all, I’m just getting my feet wet, so to speak, with the Python API. I wasn’t able to figure this out from the API docs, so I thought I’d ask here - where do the relevant modules live in a generic installation of Blender? In the scripts/modules directory? I’m on Win 7 64, if it matters.

I’m just looking to conceptualize how the bpy packages and modules are imported, and from where.

The docs basically begin with “first, import bpy,” so that’s not a whole lot of help to my very visual brain.

I am not sure, but I always imagined that bpy was just built into the Blender executable. To examine the packages I just used the dir() command.


import bpy

print(dir(bpy))

Start blender, go to Scripting view and type “bpy.” into the PyConsole, then hit Ctrl + Spacebar to auto-complete - it will show you the list of available modules. Type and auto-complete to explore the structure! And read the API docs, there’s a lot of relevant information about operators, properties etc.

…and if you would like to have a decent IDE which allows you to debug an add-on running in Blender, and which displays not only the method or attribute names, but also their descriptions from the Blender API documentation, in the autocomplete information -
look inside the book presented on this page: http://airplanes3d.net/pydev-000_e.xml

Awesome, thank you for that link! I’ll get on reading that :slight_smile:

Hey thanks all for the replies. I wonder if I should start a new thread, or just ask another question here. I’ll ask here first, and if my questions become too many, I’ll just start another thread to keep asking more questions.

My next question is how to test whether an armature is in pose mode and whether the armature action has a keyframe set on the current frame. I’m kinda stuck in this mental loop in the API docs… bpy is kinda confusing.

Edit: I should say that I’m just looking for a push in the right direction, not for someone to hold my hand. :slight_smile: Though if someone WANTED to hold my hand through all my questions, I wouldn’t object, hah.

Test view port mode:
if bpy.context.mode == ‘POSE’

Test object mode:
if bpy.context.object.mode == ‘POSE’

import bpy

def keyframe_at_frame(ob, frame):
    if ob.type == 'ARMATURE' and ob.animation_data is not None:
        action = ob.animation_data.action
        if action is not None:
            for fcu in action.fcurves:
                # you might wanna round co.x or cast to int, since it's a float
                return True if frame in (p.co.x for p in fcu.keyframe_points) else False
    return False
    
ob = bpy.context.object
frame = bpy.context.scene.frame_current


print(keyframe_at_frame(ob, frame))

Wow, thank you. Very informative! I think I actually learned something just from reading that.

There are some things about bpy that I don’t quite get, though; for instance, I can’t find bpy.context.mode anywhere in the API docs, why is that? Just not yet documented? That’s been true of a couple items I’ve gone looking for, and I’ve been really confused by their absence. What’s the best way to get documentation about a given class or attribute in a case like that? Calling the help function sometimes raises errors, and when it doesn’t, it’s often kinda useless.

I’m going to go ahead and start a new thread; I’m slowly trudging my way through a script I’m writing and I’m going to have a lot more questions, so I figure I ought to have them all contained in a related thread.

blender.org is currently unavailable so i can’t search the docs, but it deffo lacks couple of things. It is mostly auto-generated, but some areas seem to be manually added. And certain things are just not explained at all, you need to learn it from other scripts. Those users who know the API don’t dare to spend time to write about the very “trivial” and complex topics :frowning:
I wished i had commit access to the docs, so i could add a couple things, but in my experience it’s better to discuss problems in this forums in detail, since you can’t give generic recommendations.

I generally use the autocomplete in the console to explore bpy a bit.
Autocomplete shortcut is CTRL+SPACE.
the console is in the Blender UI.
so I type in:
bpy.
then press CTRL+ SPACE
I navigate from there.

bpy.data is the main spot you want to look into for accessing and modifying data.
anyways… good luck!

@CoDEmanX - perhaps sometime you can break down your workflow in API exploration .Your solutions as well as Atoms are always stellar. :slight_smile:

ideasman added the missing context members!

Well i guess i should write down some of what i learned and share my test scripts… So far, check out e.g. this page:

http://wiki.blender.org/index.php/Dev:2.5/Py/Scripts/Cookbook/Code_snippets/Interface

Oh neat, I’ll read that today. Thanks!