0
$\begingroup$

I'm testing Blender's scripting, with this simple operator from the documentation:

def main(context):
    print("OPNAME"+":", len(context.scene.objects), "object(s) in the scene")
    print(*[obj.name for obj in context.scene.objects])


class SimpleOperator(bpy.types.Operator):
    def execute(self, context):
        main(context)
        return {'FINISHED'}

In the first print instruction, I'd like to replace "OPNAME" by the name of the operator (supposedly bpy.types.Operator.bl_label). However main is not an operator's class method, so I can't access the variable directly.

When execute is called by Blender, what's the executing context? how can I access the operator instance (excluding passing the value to main)?

$\endgroup$
  • 1
    $\begingroup$ How about SimpleOperator.bl_label or pass the operator (self) to main, or make main an operator method. Note: the method main is only used by way of example is not mandatory. Could also look at context.active_operator. $\endgroup$ – batFINGER Aug 7 '18 at 16:53
  • $\begingroup$ @batFINGER: SimpleOperator.bl_label will work if main is only called by this operator. context.active_operator is None. $\endgroup$ – mins Aug 7 '18 at 17:07
  • 1
    $\begingroup$ Yes am aware of both counts. Needs to be a registered and invoked operator for context.active_operator. As mentioned would simply pass self to get the instance. ie def main(op, context): and call via main(self, context) $\endgroup$ – batFINGER Aug 7 '18 at 17:10
  • $\begingroup$ @batFINGER: I agree the example with a standalone method not receiving a reference to the operator is not a good choice, it makes sense to have everything in execute or use a parameter. I'll keep this question just for learning purposes. $\endgroup$ – mins Aug 7 '18 at 17:16
  • $\begingroup$ Disagree re choice. Can either call main from the operator. or import the main method from the module to use elsewhere. Listing the context objects is a pretty simplistic example... but that's the idea. $\endgroup$ – batFINGER Aug 7 '18 at 17:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.