Context reference
Everything a node can do in emit(ctx).
ctx is a NodeContext (core/context.py), created for every node each time the add-on is built.
Values
| Call | Returns / does |
|---|---|
ctx.input(key) | Expression of input key: the connected value (converted to the socket's type), else the socket's own value |
ctx.input(key, default="None") | Same, but default when nothing is connected |
ctx.inputs(key) | Expressions of all sockets of a dynamic group |
ctx.is_linked(key) | Whether input or output key is connected |
ctx.output(key, expr) | Set data output key. From a flow node: available to everything after it |
ctx.resolve(field) | The node a Reference field points to, or None |
Writing code
| Call | Returns / does |
|---|---|
ctx.code(template) | Write statements where this node's code goes (module level for root nodes) |
ctx.module(template) | Write at module level (classes, functions) |
ctx.flow(key, layout=None, outputs=None) | Placeholder for the code connected to flow output key. layout: layout for interface flows below; outputs: {key: expr} values only valid in there |
ctx.join(items) | Placeholder for a list of code items (may be multi-line, may contain placeholders; empty items are skipped) |
ctx.layout | Current layout expression in interface flows |
Placeholders must be on their own line. They are indented to the column they sit in.
Module
| Call | Returns / does |
|---|---|
ctx.imports("import math") | Add an import line (deduplicated) |
ctx.helper("log") | Expression to call a shared helper with, utils.log (core/helpers.py) |
ctx.symbol(node_or_tree, name) | Expression for name: the name itself, imported with from .<module> import name from another tree; _<module>.name if the two trees import each other |
ctx.script(module_key, alias_key, source) | Add source unchanged as scripts/<name>.py and import it here; returns the name to use it by. Both names are declared in sn_names() (naming.Script, naming.Symbol) with a shared owner |
ctx.on_register(line) / ctx.on_unregister(line) | Lines for the module's register() / unregister() |
ctx.var("row") | Unused local name (row, then row_2, …) |
ctx.name("class") | A name the node declared in sn_names() (MY_ADDON_OT_say_hello, my_addon.say_hello, on_save_post, …), see core/naming.py |
ctx.dev | True in the live add-on, False when exporting. Prefer a helper over checking this |
Errors
raise NodeError("message") shows the message on the node; it emits no code.
Example
def emit(self, ctx):
item = ctx.var("item")
ctx.code(f"""
for {item} in {ctx.input("list", default="[]")}:
{ctx.flow("loop", outputs={"item": item})}
{ctx.flow("next")}
""")compiles, with a Print node in the loop, to
for item_1 in context.scene.objects:
print(item_1.name)