Not exactly. The `nuke.Group` class implements both the .begin()/.end() methods
*and* the context manager interface. In other words, there are no node types
that have .begin()/.end() methods but will not work as context managers.
The basic inheritance tree for all of Nuke's Python node classes looks like:
nuke.Node
|- nuke.Viewer
`- nuke.Group # This class is where .begin()/.end() are defined
|- nuke.Gizmo
|- nuke.Root
`- nuke.Precomp
Because `nuke.Gizmo`, `nuke.Root`, and `nuke.Precomp` all inherit `nuke.Group`,
they all have the same group-related methods available (in addition to any they
define themselves).
Now, as far as using the methods vs. a with statement, it ultimately comes down
to personal preference. However, the `with` statement is considered more
"Pythonic," in that it is more concise and handles the call to .end()
automatically "on the way out".
-Nathan
From: Den Serras
Sent: Wednesday, November 18, 2015 12:16 PM
To: Nuke Python discussion
Subject: Re: [Nuke-python] nuke.zoom and in group button
Thanks, Nathin! So, if I understand correctly, the only advantage to
"group.begin()" over "with group" is that group.begin() will work with classes
that don't have __enter__ and __exit__. How often does that actually happen? I
prefer the with because it's shorter and easier to visually parse, but I want
to play it safe.
Den
On Wed, Nov 18, 2015 at 12:29 AM, Igor Majdandzic <[email protected]>
wrote:
Thanks Nathan,
very interesting insight.
Am 17.11.2015 um 19:32 schrieb Nathan Rusch:
These two snippets are pretty much functionally identical:
with group:
# Do something...
# ------
group.begin()
try:
# Do something...
finally:
group.end()
The `with`statement works with objects that implement Python's context
manager protocol
(https://docs.python.org/2/reference/datamodel.html#with-statement-context-managers).
Basically, if a class implements __enter__ and __exit__ methods, it can be
used as the context manager in a `with` statement. When the block inside the
`with` statement begins, the context manager's .__enter__() method is called,
and when the block exits, the context manager's .__exit__() method is called
unconditionally (even if an exception occurs in the block).
When using a Group node as a context manager, its .begin() method will be
called by .__enter__(), and .end() will be called by .__exit__(). Thus, this is
redundant:
with group:
group.begin()
# Do something...
group.end()
It's worth noting that Nuke currently always returns the current context to
the root node when a group context is exited, rather than to whatever context
it was in before the context was changed to the group, though this is logged as
a bug.
Hope that helps.
-Nathan
From: Justin GD
Sent: Tuesday, November 17, 2015 9:58 AM
To: Nuke Python discussion
Subject: Re: [Nuke-python] nuke.zoom and in group button
Hi all,
That's a good question;
I've been using the with statement as well with success.
I'm curious of what is the best method.
Cheers,
Justin
2015-11-17 17:24 GMT+00:00 Den Serras <[email protected]>:
This leads to a slightly tangential discussion... I used to always use:
with group:
(inner node stuff)
which generally seemed to work; but so many people use begin() that now I
play it safe and do:
with group:
group.begin()
(inner node stuff)
group.end()
But perhaps someone can explain what the theoretical and functional
differences are between "with group" and "group.begin()", and whether by
combining them I'm causing even more problems...
Thanks!
Den
On Tue, Nov 17, 2015 at 1:29 AM, Johan Forsgren <[email protected]>
wrote:
Root().begin() worked wonderfully, I had actually no idea that it existed
at all, thanks!
2015-11-16 18:05 GMT+01:00 Mike Frank <[email protected]>:
nuke.Root().begin()
_______________________________________________
Nuke-python mailing list
[email protected], http://forums.thefoundry.co.uk/
http://support.thefoundry.co.uk/cgi-bin/mailman/listinfo/nuke-python
_______________________________________________
Nuke-python mailing list
[email protected], http://forums.thefoundry.co.uk/
http://support.thefoundry.co.uk/cgi-bin/mailman/listinfo/nuke-python
----------------------------------------------------------------------------
_______________________________________________
Nuke-python mailing list
[email protected], http://forums.thefoundry.co.uk/
http://support.thefoundry.co.uk/cgi-bin/mailman/listinfo/nuke-python
_______________________________________________
Nuke-python mailing list
[email protected], http://forums.thefoundry.co.uk/
http://support.thefoundry.co.uk/cgi-bin/mailman/listinfo/nuke-python
_______________________________________________
Nuke-python mailing list
[email protected], http://forums.thefoundry.co.uk/
http://support.thefoundry.co.uk/cgi-bin/mailman/listinfo/nuke-python
--------------------------------------------------------------------------------
_______________________________________________
Nuke-python mailing list
[email protected], http://forums.thefoundry.co.uk/
http://support.thefoundry.co.uk/cgi-bin/mailman/listinfo/nuke-python
_______________________________________________
Nuke-python mailing list
[email protected], http://forums.thefoundry.co.uk/
http://support.thefoundry.co.uk/cgi-bin/mailman/listinfo/nuke-python