An issue with executing code directly (via operators), is large blocks of code will flood the info window. If we add built-in support for that - my concern would be that we get bug reports about add-ons filling the info window with code (even if there aren’t bug reports it’s )
We could constrain this to not allowing new-lines, although that just pushes developers to do tricks with list-comprehension (which can end up being fairly awkward).
Another down side is having to put code in strings, @BD3D’s example has a bug - where emitter names containing single quotes will generate invalid code (simple to solve, but an example of the kinds of mistakes that are easy to make when generating code as strings)
Of course we can just accept the down-sides but I think it’s worth considering some alternatives.
Support a kind of “simple” operator registration.
from {*some_module*} import simple_operator
cls = simple_operator(id="my.op", execute=lambda: scene.frame_current = 10)
# Registration would still be needed.
bpy.utils.register_class(cls)
There are many ways we could use utility functions to support associating operator ID’s with code, which can be opt-in for developers who want to use code-snippets in the UI.
Another alternative could be to reference functions in modules, this used to be supported in the Blender Game-Engine and I found it worked well. Although it does require using modules/add-ons.
e.g. module_name.fn_name
or package_name.sub_package.fn_name
.
So you could define reference a function by name in the current module, then the operator would take the module + function name and execute it. This has the advantage that the functions are code (not strings), and the operator references code with a short & unambiguous identifier.
I’d suspect that someone who wants to execute code-directly probably sees the alternatives I’m suggesting as complicated which is understandable as they involve some constraints on how the code is executed.
Even so, I’d much prefer if functions could be used directly without passing the code as a string which feels awkward & error prone.
Note that we could have multiple solutions (no reason we have to pick just one), although it’s worth thinking which might solve the issue @L0Lock has raised, or if it should be addressed directly at all.
[quote=“BD3D, post:30, topic:25836”]