Through the console window, we explore the blender python library provided as bpy. This container holds every piece of information and executable function in blender and the currently opened project. Patrick will first show two key sections of the bpy module, that is: bpy.data.objects and bpy.context
- [Instructor] Now lets dive further into the BPY module. Once again we're gonna change to the scripting window. Then as we hover over the consul window, we can press control alt to maximize the space. One line of code you will find in every single Blender script is the import bpy line. This is what imports and makes available to you every piece of information in Blender and all of its properties and operators. Note that the interactive consul specifically provides the convenience import for you so you don't have to do this every time.
However in your own scripts you have to make sure you always import the bpy module. If I type up bpy then press dot and then use the shortcut key control space bar to auto complete, you can see it provides all these sub modules available to bpy. Specifically in this video we are gonna look at context and data. If I type out data and then start typing out the word objects, and then press control space bar again you'll note that it auto completes because there's only one possible solution with that starting string.
When I press enter you can then see we are returned the bpy collection that is three in size. This is because there are three objects in this Blender file. If I press the up arrow and then control space bar once more you can see it actually helps us auto complete the possible options for this collection. In this case we have the cube, camera, and lamp available to us. If I type out cube and then press enter this is the live cube object. The same as what we could see in the Blender screen.
If I press control up to un maximize this screen and then press up arrow again to get our last line you can see that I can actually change the location of the cube by assigning a new 3D coordinate. Once I press enter the cube immediately moves to the new location. If I delete the last line up to the last dot operator and then press control space once more you can see the list of all the available properties and functions on this cube.
You can see that many of these are the ones that built in to Blender but some of them may even be different scripts or add on's that have been installed and created by other programmers. This is a very convenient way to look through all your available options and properties in case you don't remember what you're looking for. Next we're gonna look at the bpy dot context module. By holding control and pressing backspace or delete, I will more quickly delete up to the bpy and dot line. Then type out context.
By pressing control space you can see all the available context modules. If I press control up to de maximize the screen, you can think of this as what's currently available to the user when they look at the screen. Such as what objects are available on the screen and what screens are open. For example if I type active object it will return to me the currently selected and active object. Which once again happens to be the cube. Note that the context run in a 3D view would be different from the context run within the text editor because they have different information and different properties available to them.
It is important that you are always aware of the current context otherwise your scripts may break. We are going to look further into the bpy dot context and dot data modules much further in future modules.
- Controlling Blender with code
- Exploring the bpy library
- Creating Blender operators and properties
- Writing scripts
- Auto running scripts
- Drawing interface elements such as panels and menus
- Building a custom add-on