Patrick shows how to use Blender's built-in python console window to run commands and how to execute scripts from the text editor window. You will learn how to use the python command line interface to run individual commands and access all core blender data also available through the blender user interface.
- [Instructor] For this module let's go ahead and make sure we open up Blender and have the console window available to us. If you're on a Windows machine, you may recall you can go under Window and then toggle system console. And if you're on a Mac you can use the alias we created in the previous video. With this initial default layout in Blender, we have a very good general purpose view. But it's not so well-suited to scripting. We can actually use one of the built-in templated views by going to the square icon at the top and then selecting Scripting.
From here we see the three key views available to us as developers. The bottom is the Interactive Python Console. And the middle left the text editor window. And then the top we have the info bar. While it's currently empty as we change things in Blender and move things around this will actually act as a log that basically describes all the actions you have taken since Blender was started. Note that for an existing window we're able to change it to another type by using the drop down in the top left and the bottom left.
And then change it to the current one such as the Python Console. Let's now look further into the Python Console that we have down here. If I hover over this window and press Control + Up Arrow, it will maximize this screen area. This is a full blown complete Python Console much like in the terminal or any other IDE you may have used. We can run commands such as Hello world. And then print them. And you see that it prints directly to the console itself.
Note you can also use the up arrows to go to previously written lines or back down to go to more recent. While holding control you can also more quickly navigate left and right by using the arrow keys. And wherever you are in the line, if you press tab it will automatically indent it by four spaces. If I press Control + Up to demaximize this area, I can then hover over the text editor view. Once again, I will press Control + Up to maximize this view. This is the built-in text editor window where you can write your code or additional information about your Blend file.
If I press the New button down here we create a new data text block. Note that if you have multiple data text blocks inside Blender, you can press this icon here to see a list of all the ones that you have available. From here there may be a few settings you wanna know about such as enabling line numbers, enabling word wrap as well as syntax highlighting. Additionally if you press the plus button at the left hand side, you see more options such as if you wanna have tabs act as four spaces or as a tab.
Additionally you're able to save the text blocks to external files using this Text menu. Or you're able to load in external files. As an example we can do print("hello world"), and then by using the Run Script button or the Text menu Run Script option or the shortcut of P, we can run the script. And then if I press Command + Tab to go to the terminal window, you can see that it printed out to the Interactive Console.
And now we can do our first Blender related command where we first import the Blender library and then run an operator command which is bpy.ops.object.duplicate. We can then press the Run Script button to actually run the script. While nothing pops up immediately if we demaximize this view using Control + Up and then move over the 3D view, press G to grab this cube, you can now see there is a duplicate cube. And, of course, if we go Command + Tab into the terminal again, we can see that the Hello world has printed out once more.
Finally, we have the now populated info window. If I hover over this space at the top of my view and press Control + Up you can see we actually have a log of everything we have done in this Blender file since we opened it. And you can, for example, see we have the command duplicate that we have run here. Note in this view that we're able to right click to select different lines and you can right click again to deselect them. Making good use of these three views available to us in Blender is what will allow us to make our scripts with ease.
- 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