Operators are the first step to creating custom buttons, menu items, and panels in Blender. In this chapter of Scripting for Artists, Sybren explains how to create your own operators.
- Ingredients of an operator: 00:50
- Creating your own operator: 03:15
- Passing parameters to the operator: 08:17
- An explanation of classes in Python: 15:57
- Using the context: 17:10
- Limiting when the operator can be used: 17:57
18 Comments
Join to comment publicly.
Tchelet Levi
16th May 2020 - 20:49
As someone who really struggled with Python and the Blender API a year or so ago, these videos are a life saver! I love your way of teaching. It's very clear and easy to understand :)
Guillaume Thelissen
3rd April 2020 - 17:10
Very clear, very informative! Thanks a lot Sybren!
Jay.w.overstreet
29th November 2020 - 05:20
I am following along verbatim, step-by-step, but the monkey_grid.py is not registering, where I cannot find it in F3 menu. In addition, when I save the file, close out, and restart blender, the monkey_grid is not found in bpy.ops.mesh nor F3 menu. I am working with Blender 2.9.1. Is there an update to 2.9.1 which can potentially be prevent the scripts from registering? Or, could a step be missing from the video? Thank you in advance for your help.
Jay.w.overstreet
29th November 2020 - 22:22
@Sybren A. Stüvel
George Castro
4th December 2020 - 18:19
@Jay.w.overstreet i got this from the youtube comments and it worked: Edit>Preferences>Interface tick Developers Extras
Jay.w.overstreet
30th December 2020 - 23:58
@George Castro Thanks! I will give it a try.
Marlon Boettger
7th December 2020 - 23:32
@Jay.w.overstreet Same problem here, checked the code all over for typos but it seems exactly like in the video. It does not register for me in 2.91.
Sybren A. Stüvel
11th January 2021 - 13:59
Be sure to distinguish between "does not register" (i.e.
bpy.ops.your.operator()
doesn't even run in the console) and "cannot be found in the menu". The latter could be caused by the Operator Search menu changes in Blender 2.90.Gregory L. Hatcher
7th April 2020 - 01:38
Nice tutorial. I have a question: I want to add Suzzannes along the Z axis as well.
I came up with the following code:
count_z: bpy.props.IntProperty( name="Z", description="Number of Suzannes in the Z-direction", default=3, min=1, soft_max=10 )
def execute(self, context): for idx in range(self.count_x * self.count_y * self.count_z): x = idx % self.count_x y = idx // self.count_x z = idx // self.count_x bpy.ops.mesh.primitive_monkey_add( size=0.2, location=(x, y, z) ) This line is not producing the correct output:
z = idx // self.count_x
I will continue to experiment on my own, but I wanted to know what operation I need to use here.
Thanks again Sybren for this tutorial!
Sybren A. Stüvel
7th April 2020 - 10:44
*@Gregory L. Hatcher* when you add another dimension the math becomes a bit tricker indeed. What is probably easier is to have three nested for-loops, each counting in the X, Y, and Z direction. Something like this:
(except that in comments the indentation isn't maintained -- indent each
for
another step like always)Gregory L. Hatcher
7th April 2020 - 18:09
*@Sybren A. Stüvel* Thanks! Working as expected now. However, I did discover a few interesting patterns experimenting with different combinations of the // and % operators. :)
felixpitau
13th September 2020 - 15:03
*@Gregory L. Hatcher* I added a z-axis to my function too. Seems to work just by using modulo with each of the respective counts. The floor division operator
//
is not necessary.EDIT: Okay it's not that simple, this seems to work with distinct values though
EDIT 2: To make it work for non-distinct values too is actually more similar to the original 2D solution. Use an additional modulo with the next dimension in line (resetting y back to zero in time for additional layers) then for the new dimension floor divide by x times y (as that would make up an entire 2D layer of monkies)
gines capote
11th January 2021 - 15:13
Thanx :)
emi.15.xion
8th May 2020 - 11:16
HI! I have the blender 2.82 and the operator monkey_grid doesnt appears in f3 and console menu. I need to save the script in a special place?
Sybren A. Stüvel
14th May 2020 - 19:57
*@emi.15.xion* You need to install it as an add-on. Then Blender saves it in a special place. See Blender's Directory Layout for more info.
emi.15.xion
4th June 2020 - 14:19
*@Sybren A. Stüvel* THanks!!!!! IT works!
kontakt5
25th May 2020 - 16:21
Thanks for this tuto. Could you provide more? I am especially interested in modal operators. Sometimes modal methods are combined with execute, others only invoke. For reasons I don't understand every tuto I found is about creating menus and buttons, but basic tools (e.g. transform ) running modal for good reason. I feel quite lost about this topic and I am surely not the only one.
Sybren A. Stüvel
11th January 2021 - 14:00
@kontakt5 Modal operators are handled in chapter 15. Maybe at some point I'll extend the series, but for now this is it.