Pop and push explanation

Hi,

Could anyone explain what (gimp-context-push) at the start of a script is for ?
There’s usually (gimp-context-pop) at the end too. I don’t understand what the
description in the guide means…

Pushes a context to the top of the plug-in’s context stack.

Thanks

I do not know much about GIMP’s internals, but I suspect the scripting machinery was implemented by software engineers, and I know a bit about software engineering. I assume that “push” and “pop” refer to the abstract data type known as a “stack”.

Think of a stack of plates in a restaurant. You can put a plate onto the top of the stack, in which case you “push” it down on top of the other plates in the stack. All the other plates are still present, but are temporarily inaccessible. Or, you can lift the top plate off the stack, in which case it “pops” off, and the remaining plates in the stack rise. The next plate becomes accessible. It was the plate which was on top of the stack when you “pushed” your plate on top of it.

So, I assume that the statement (gimp-context-push) makes a copy of some data which tells GIMP how to behave, e.g. what brush is selected, what are the current foreground and background colours, which layers are visible, that sort of thing. This data is a “context” for GIMP’s response to commands. The statement pushes this context data onto a stack of context data entries. Your script then executes, and possibly makes changes to the context. For example, it might change the foreground colour. Then the statement (gimp-context-pop) pops the context data off the stack, and sets GIMP’s context to what is stored in the popped data.

The net effect is to ensure that inconsequential changes your script makes to GIMP’s context are erased when your script finishes. Obviously you probably want the intentional results of your script to persist. I do not know what is included and left out of in “GIMP context” well enough to explain how that happens.

I hope this helps,
—Jim DeLaHunt

1 Like

Thank you, it does help, understood. :slight_smile:

It saves all the “context” data, that are settings used by paint ops: FG/BG colors, brush, pattern, dynamics, path stroke, (more or less everything that can be retrieved by a (gimp-context-get-something)):

You are supposed to do the corresponding (gimp-context-pop) before your script exits.

In between, you do whatever you want with the context without having to worry about saving individual settings.

The advantage of push/pop operations is that they can be used by nested calls, if your script calls another script/plugin that also does its own push/pop when this script/plugin returns the context is exactly as you set it before calling it.

Of course if the script needs to alter a setting for the user then this should be done outside of the push/pop.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.