Page MenuHome

Misleading code sample
Closed, ResolvedPublic

Description

May not be a bug, but it is potentially misleading. In the document, there is this

Menu Item

.... The method used for adding a menu item is to append a draw function into an existing class:

def menu_func(self, context):
    self.layout.operator(ObjectCursorArray.bl_idname)

def register():
    bpy.types.VIEW3D_MT_object.append(menu_func)

For docs on extending menus, see: bpy.types.Menu.

But using that register() function does not work, because it is not creating the ObjectCursorArray object first. This tutorial is for beginners, right? So, the reader would not have much knowledge about add-ons, and just follow the code. I have wasted a lot of time trying to add a menu with that code by installing a plugin (.py) file. After some time, that I figured out that it does not register the object, and sure enough, when I scrolled down furthur, there was "Bringing it all together" section which had

def register():
    bpy.utils.register_class(ObjectCursorArray)
    bpy.types.VIEW3D_MT_object.append(menu_func)

In my opinion, the code in the Menu Item section should have the line to register the class, too, in order the sample to work at that part of the document.