This sounds really basic, yet I can’t find this online. Is it possible to call a function from within a class, have that function process some information then spit back the results back into the class it was called from? Example:
import bpy
def superCoolFunction(x, y):
result = x + y
print (result)
class myClass(bpy.types.Operator):
bl_idname = "object.my_class"
bl_label = "My Class"
x = 55
y = 45
superCoolFunction(x,y)
#Yo, I need that result to carry on here. Howzzz do I get it?
def register():
bpy.utils.register_module(__name__)
def unregister():
bpy.utils.unregister_module(__name__)
if __name__ == "__main__":
register()
I’ve got several classes doing the same tasks. I’d like to consolidate those tasks within a function so each class can call upon it. How do I do that? Or am I coming at this backwards. Thanks.
import bpy
def superCoolFunction(x, y):
result = x + y
return result
class myClass(bpy.types.Operator):
bl_idname = "object.my_class"
bl_label = "My Class"
x = 55
y = 45
result = superCoolFunction(x, y)
print("Result", result)
def register():
bpy.utils.register_module(__name__)
def unregister():
bpy.utils.unregister_module(__name__)
if __name__ == "__main__":
register()
But that’s not how you’re supposed to use operators, see the Operator Simple template in the text editor, here’s a slightly modified version:
import bpy
def main(context):
return ", ".join(ob.name for ob in context.scene.objects)
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):
self.report({'INFO'}, 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()
Run script, spacebar over 3D View and search for “Simple”, then run the operator.
The operator’s execute() method calls the global function and issues an error with the concatenated names of all selected objects.
Thanks. I didn’t realize I needed to bury the function into a variable inside of the class to get the returned result… I was just trying to call on “result” raw doggin’ it. “That’s not how you’re supposed to use operators.” You mean it’s bad to have a function perform some work then return the result while in a class that any other class can then use? That seems pretty useful to me.
No, i mean you can’t call global functions at class scope of an operator, you need to do this in methods of the operators (such as execute(), invoke(), draw())
RickyBlender what do you mean “the simple return from the function”? And the self.report thing prints the result of the “main” function in the headline of the info bar so it would just print the list of scene objects with a comma between them.
But since the function name and argument list is short, and we don’t need the return value for anything else, there’s no point in temporarily storing the return value in a variable.