Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ArrayPlug improvements #6015

Merged
merged 10 commits into from
Aug 28, 2024
Merged

ArrayPlug improvements #6015

merged 10 commits into from
Aug 28, 2024

Conversation

johnhaddon
Copy link
Member

This improves the ArrayPlug API so that we can create zero-length arrays which are still aware of the type of their elements, and can automatically resize to create new such elements. It is motivated by #5954, which has just that need.

This isn't without its wrinkles, since it turns out we have several ArrayPlug clients that are violating the constraint that all elements by identical. But I think it's a big step forward for the ArrayPlug itself, so I've fixed one client (CreateViews) and documented a path forwards for the others.

- Fix error when resizing removed a plug with an input connection. In this case, the "resize when inputs change" behaviour was kicking in during the outer resize and messing everything up.
- Fix failure to resize output plugs, due to attempting to add an input plug as a child.
A minimum size of 0 is not currently allowed, and will be silently clamped to 1. But we're about to allow it, and for backwards compatibility we need to keep `Switch` creating arrays of the same size.
Also remove unused imports from CreateViewsTest.
We achieve this by explicitly storing a prototype for the elements rather than using the first child as the prototype. At the same time, we require all clients to pass the prototype to the constructor, so that we are always in a valid state following construction.

The serialisation now also correctly passes the prototype on construction, and uses `resize()` to add any required children instead of using a series of `addChild()` calls as before. This reflects how we want people to use the API, and makes for a more compact serialisation too.

The one wrinkle in this is support for legacy serialisations, which didn't provide an element prototype to the constructor, and used `addChild()` instead of `resize()`. We continue to support these with some special cases internally, whereby `m_elementPrototype` isn't initialised until the first child is added.
`ArrayPlugSerialiser::childNeedsConstruction()` return false now, so the existence of the Dynamic flag on its children is irrelevant. We're one step closer to eventually getting rid of the flag entirely.
Our `startup/Gaffer/arrayPlugCompatibility.py` shim was keeping these working, but we want to provide a good example.
Passing `nullptr` is no longer allowed except to support the loading of legacy files. Unfortunately, all client code that is adding views is doing it via `addChild( NameValuePlug( ... ) )` rather than `resize()` or `next()`, meaning the elements are not forced to match the prototype exactly. And all existing serialisations are doing the same. And because `NameValuePlug( "left", ... )` produces a plug with a default value of "left", the array elements being created are not homogeneous. That breaks the new ArrayPlug serialisation, which assumes that a simple `resize()` is sufficient to recreate all children.

We deal with this using a compatibility shim that detects the bad child additions and resets the default value while maintaining the current value.
And outline a possible path forwards. In practice, we're currently getting away with the violations because the serialisations for these nodes all call `addQuery()` to resize the arrays upfront, so that the arrays already have the required size before the `resize()` call is executed.
@danieldresser-ie
Copy link
Contributor

I've looked through this code fairly carefully, and it all looks good to me. The main concern with this sort of thing is always weird backwards compatibility issues ... but I can't think of anything you haven't handled. Seems like it's probably good.

Oh, and sorry for the mess with CreateViews, that one definitely looks like it was my fault.

@johnhaddon johnhaddon merged commit ac96930 into GafferHQ:main Aug 28, 2024
5 of 6 checks passed
@johnhaddon johnhaddon deleted the arrayPlugElements branch October 1, 2024 09:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Pending release
Development

Successfully merging this pull request may close these issues.

2 participants