Simple error, cant understand why!

Just picking up the blender api, and am going through some tutorials. As far as I can tell, this should be completely acceptable code, but I keep getting an error when attempting to register the operator. I check the blender console, but all it gives is the line of the error, which is the register_class call.

Any idea?


class ExportOperator(bpy.types.Operator):
    bl_idname = "opExport"
    bl_label = "Export Me"
    bl_description = "Testing"
    
    def execute(self, context):
        return {'FINISHED'}
    
bpy.utils.register_class(ExportOperator)


I don’t know which Blender version you are using, but when I tried that snippet in 2.60 I got two errors.

  1. bpy undefined
  2. bl_idname invalid

Adding an import bpy and renaming the bl_idname made it work for me.


import bpy

class ExportOperator(bpy.types.Operator):
    bl_idname = "op.export"
    bl_label = "Export Me"
    bl_description = "Testing"
    
    def execute(self, context):
        return {'FINISHED'}
    
bpy.utils.register_class(ExportOperator)

For some reason the bl_idname didn’t like any capital letters and it required at least one period in its name…

Hi LevyDee,

There are a few pitfalls to be aware of;

  • Operator bl_idname can’t contain capital letters.
  • The operator must be contained in one of the groups of operators under bpy.ops which is specified in bl_idname.

These are quite simple to fix:

class ExportOperator(bpy.types.Operator):
    bl_idname = "export_mesh.op_export"
    bl_label = "Export Me"
    bl_description = "Testing"
    
    def execute(self, context):
        return {'FINISHED'}
    
bpy.utils.register_class(ExportOperator)

  • Changing the bl_idname to op_export eliminates the capitals.
  • Adding export_mesh to the bl_idname places it under bpy.ops.export_mesh.op_export()

Hope that helps.

Cheers,
Truman

OHHH jeez! I have been trolling the api documentation for a few hours it feels! Thank you for fix! <3