I am just learning programming and am having difficulty running this example code from Blender's official python documentation on the quick start page: https://docs.blender.org/api/2.93/info_quickstart.html
It said to copy and paste what is show below, but the last sends an error message in the system console for the last line( the test call). If I comment it out, no error is sent, but no operator is shown in the 3D viewport's F3 search menu when I type in "simple".
This is the error message in the system console:
Traceback (most recent call last):
File "C:\Users\north\Downloads\scripting for artists.blend\Text", line 35, in <module>
File "C:\Program Files\Blender Foundation\Blender 2.93\2.93\scripts\modules\bpy\ops.py", line 132, in __call__
ret = _op_call(self.idname_py(), None, kw)
RuntimeError: Operator bpy.ops.object.simple_operator.poll() failed, context is incorrect
Error: Python script failed, check the message in the system console
I am using Blender version 2.93.0
Python:
import bpy
def main(context):
for ob in context.scene.objects:
print(ob)
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()
# test call
bpy.ops.object.simple_operator()