scenerygraphics / sciview

sciview is a tool for visualization and interaction with ND image and mesh data
BSD 2-Clause "Simplified" License
66 stars 17 forks source link

SciView.addNode, Node.addChild, Node.(set)Parent ambiguity #497

Closed moreApi closed 1 year ago

moreApi commented 1 year ago

@xulman and I had a chat during lunch and he mentioned his struggels with adding childs to parent nodes in sciView.

In SciView/Java there is a setParent method generated which looks like it is on the same level as addChild. And worst of it all, both are, used alone, wrong. In SciView one has to use SciView.addNode (which directly adds the node under scene, so no other parent) or addChild + SciView.publishNode to add a node in away that it shows up in the inspector and is known to imageJs services.

In scenery and kotlin this is a bit more clear. There is the addChild method and the parent property. This hints that a child should be added by using the function.

possible solutions on the scenery side

possible solutions on the sciView side

kephale commented 1 year ago

I think SciView.addChild could be removed. This is API simplification, so it is great, and I don't see any usages.

I like the idea of listening to scene graph changes and calling publishNode automatically.

kephale commented 1 year ago

Since this deals with API, I'd like to resolve it for 1.0.0. I agree this is an important point.

moreApi commented 1 year ago

@skalarproduktraum wants to do this task but he needs time to mull about it

kephale commented 1 year ago

Ok, this is related: https://github.com/scenerygraphics/sciview/issues/504

xulman commented 1 year ago

sciview (not scenery) user's perspective: (based on my fresh experience of a newcomer ('cause I have realized I have forgotten most of the sciview coding sadly))

xulman commented 1 year ago

my take would be a bit anti-Kotlin chatty:

exemplified on Spheres:

shortcuts:

Notice the policy: explicit creation and explicit adding -- often I prefer to first prepare the object before I introduce it into the scene

but that's breaking API... let me come back with a better proposal in a while

kephale commented 1 year ago

I found myself doing this in a script yesterday: https://github.com/scenerygraphics/sciview/blob/7fcf71f58ff7771b339cf19fc229bb500c59346d/src/main/resources/sc/iview/scripts/find_and_count_cells_cardona.py#L89-L93

kephale commented 1 year ago

Just brainstorming:

One major point for me though: convenience functions that do everything in one call are useful when you're in SciJava scripting land, where types (let along generics) becomes frustrating (e.g. in dynamic languages).

Convenience create-and-add functions

More custom usage

TBH, I think the example I linked above is pretty reasonable. If you want to do something custom in the scene graph, then do it with scenery and use sciview to publish the node.

In theory if there was a real need to add child nodes and publish them in sciview simultaneously, then we could add some extension functions to Node.

[That said, this needs to be clearly presented and documented, because this hasn't been a usage pattern that we have been promoting yet.]

[edit again:]

This leads me to suggest that we should clean up API on the sciview side, and add more examples/documentation of both cases.

xulman commented 1 year ago

my problem was that I, until you sent the post a while ago, failed to have a nodes hanged under some another node of mine (which was in the root of the scene) and have them visible in the inspector... I tried several combinations of setting parent, adding child,... and was getting either duplicitly visibly items or none.... I was missing the publishNode() trick

I agree that the example is good and I like it actually (I can get solo basic obj, incrementally set it up, and only then decide to make it available in the scene)... just failed to get the whole construction using the current API

kephale commented 1 year ago

@xulman it is 100% sciview's fault not yours.

xulman commented 1 year ago

no one's to blame (except myself)

kephale commented 1 year ago

Let's just blame the software 👼

moreApi commented 1 year ago

'publishNode()' is 3 days old. There was no way to do this properly before^^

xulman commented 1 year ago

I like the pattern, I understand the API must stay (not break), and I'm okay with that (if I may say).

Just thinking of adding one/two convenience functions to make it crystal clear for newcomers:

the latter is a helper to easier find the "publishing code", the block would then look more node-ish:

node = new Sphere()
node.spatial()....foo...
node.material().... blah...\
node.publishUnderParent( someParent );

(@kephale me back from the lunch... sorry for the delays)

kephale commented 1 year ago

'publishNode()' is 3 days old. There was no way to do this properly before^^

You know an API change is good when you already assume it has been around forever :D

kephale commented 1 year ago

The new methods that @xulman suggests sound great to me and would be pretty straightforward as extension functions

xulman commented 1 year ago

can I do PR?

kephale commented 1 year ago

That would be wonderful!

kephale commented 1 year ago

Maybe let @skalarproduktraum know you're diving into it.

xulman commented 1 year ago

I was about to code this and file the PR and while thinking about it, it is again hitting the wall between scenery and sciview...

the proposal of removing setParent() -- this is purely a scenery thing

adding child.publishUnderParent( someParent ); or parentNode.addChildAndShowIt(child) { this.addChild(child); getSciView().publishNode(child); } is wanting to extend the functionality of Node (in scenery) and then reach out to publishing the node (which I understood is mainly sciview thing)

so, I'm thinking of creating just one SciView method that takes both childNode and parentNode as params... but discovered (and they are actually noted in the original/very first post above):

I don't understand this 3rd param and failed to fake it anyhow (like providing a function that does nothing)...

nevertheless, feels like addNode() has everything needed:

(this is the SciView.addNode() after the block param was removed: Screenshot_20230619_184322 this adds two spheres, later being a child of the former, and both are displayed in the inspector panel, yay!

xulman commented 1 year ago

SciView.addChild(n) we should maybe leave (for API compatibility)... it's just adding nodes to the root, and not displaying them, but we have now the explicit re-read trigger SciView.publishNode(n).. perhaps we should add SciView.addChild( n, underThisParent ) to have it complete

so user would either be using the mighty addNode() or will be walking smaller explicit steps via addChild() and eventually publishNode()

xulman commented 1 year ago

equivalent to the addNode() as explained just above: Screenshot_20230619_191940

the code lives here... would need to do something about the now-testing addNodeB()

kephale commented 1 year ago

I am open to deprecating SciView.addChild.

kephale commented 1 year ago

I only really use SciView.addNode (except for the convenience functions like addSphere etc.).

I share @xulman's frustration with the block function argument. It is really hard to use that properly from Java, and probably even worse from SciJava scripting languages.

xulman commented 1 year ago

it's not so bad at all once the programmer recalls/understands everything...


just to explain: I'm clearly not the smartest programmer, which is good here :-) (-:, I can easily play the role of an everyday user (the programer user), and I'm reporting around what one might come accross (confusion can easily turn into roadblock and loss of a sciview user)

kephale commented 1 year ago

Closed by https://github.com/scenerygraphics/sciview/pull/521

I expect any related ideas would be captured by https://github.com/scenerygraphics/sciview/issues/489