Grease Pencil Edit Mode?

Ummm. OK. Would someone explain this to me. I’m trying to change mode to edit strokes, bpy.ops.object.mode_set(mode=‘GPENCIL_EDIT’). It tells me GPENCIL_EDIT is not an option and I’m looking right at it. How do I change to GPENCIL_EDIT using python.

https://docs.blender.org/api/blender_python_api_master/bpy.ops.object.html#bpy.ops.object.mode_set

What is the error exactly? Does an object exist or just the grease pencil strokes?

best thing I can tell ya is create a grease pencil stroke on object and run the code. It says that GPENCIL_ EDIT isn’t an option for mode set ,but yet you can toggle to it. Grease pencil stroke is set to object and surface if that matters.

Here’s screenshots, makes no sense.

Blender Screenshot


Error


Try running it from an operator like this:


import bpy




def main(context):
    bpy.ops.object.mode_set(mode="GPENCIL_EDIT")




class SimpleOperator(bpy.types.Operator):
    """Tooltip"""
    bl_idname = "object.simple_operator"
    bl_label = "Simple Object Operator"


    @classmethod
    def poll(cls, context):
        return context.active_object is not None


    def execute(self, context):
        main(context)
        return {'FINISHED'}




def register():
    bpy.utils.register_class(SimpleOperator)




def unregister():
    bpy.utils.unregister_class(SimpleOperator)




if __name__ == "__main__":
    register()

Spacebar search for Simple operator.

Running it the way you did puts it’s context in the text editor.

I’ll try that and see what happens. I found another way to enter edit mode.

bpy.data.grease_pencil['GPencil'].use_stroke_edit_mode = True

I think that would be the better way, I just thought you wanted to use the ops way. That non-ops is better as it won’t depend on context.

The edit mode for Grease Pencil is a bit of a hack, since you can’t actually set that particular mode value on any particular object and expect it to keep working when you select another object.

As AFWS #6 mentions, the only really reliable way is to use


bpy.data.grease_pencil['GPencil'].use_stroke_edit_mode = True