Whenever you press a button or use a shortcut key, you are calling an operator. Understanding what they are and how to use them is a key part of controling a blend file through code. Later in this course we will look at how to create your own operators.
- [Voiceover] One important concept in Blender is the idea of an operator. This is essentially any action you take in Blender, such as pressing a button or running a shortcut key in the user interface. If I pull this window up and then convert it into a terminal window or a python console window then increase the size a little bit you then explore bpy.ops. you then explore bpy.ops. This is one of the submodules that lists all the operators available to you inside of Blender. If I press control space now and maximize the screen of control up you see that these are all the categories for which we have operators.
One of the key ones you'll be working with is the object category. If I then look at these options you see the long list of all the available operators. This is going to be a list of all the operators defined by Blender as well as ones defined by scripts and add ons you have loaded into your scene. One operator we already looked at was the duplicate operator. Press control space before getting to the parentheses. You can see all the different options we have. There are a few different duplicate operators available to us. One such as move, one such as move linked, and duplicates make real.
If I simply use an open and close parentheses and press enter you'll notice that it returns finished indicating that the operator has completed. By pressing control up arrow to demaximize the screen I can hover over the 3D view, press G to prove that in fact it had created a duplicate of the cube. I'm gonna select both of these then delete them using x. Note that operators always return either finished or cancelled or module depending on the situation. If I were to press up arrow and run this code again, notice that it still returns finished.
This is one key property or design paradigm for an operator inside of Blender. It always tries to run successfully. So even though there was no objects in Blender to duplicate it still returned finished. There are no popups. There's nothing to say that it didn't or could not work. It finished as usual. It was part of the operator's code to avoid creating a duplicate when there wasn't an object to duplicate. This is the same thing as if you were to press shift e and the 3D view when nothing was selected. You wouldn't expect anything to pop up.
In chapter 4, we will cover how to create your own custom operators. But for now we're gonna continue using the ones built into Blender to create our own custom scripts.
- 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