-
Notifications
You must be signed in to change notification settings - Fork 48
[SofaPython3] Add a LinkPath object in both the binding and plugin. #265
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
[SofaPython3] Add a LinkPath object in both the binding and plugin. #265
Conversation
95fec4d
to
071000f
Compare
071000f
to
091f07f
Compare
This PR really deserve some attention because the use of string based link is very constraining :) |
The existing syntax to make links in Sofa is the following one: ```python node.addObject("MeshObjLoader", name="loader") node.addObject("MechanicalObject", position=node.loader.position.getLinkPath()) ``` In addition to be very verbose the getLinkPath() is returning a string (eg: "@/node/loader.position") Representing linkpath with string has drawback as it force the two objects to be part of the same simulation graph which is not always the case. Example of code that does not work as expected: ```python def MyPrefab(target): node = Sofa.Core.Node("MyPrefab") node.addObject("MeshObjLoader", name="loader") node2.addObject("MechanicalObject", position=target) return node def createScene(root): root.addObject("MeshObjLoader", name="loader") root.addChild( MyPrefab(loader.position.getLinkPath()) ) ``` It does not work because getLinkPath is returning a string, this string is then used to makes a path query into the scene-graph to locate the object or the data field to attach to. But in this case as the object issuing the query is not in the same graph the query fails. To solve this issue this PR introduce a LinkPath object which is pointing to a sofa object. This LinkPath object is exposed in python and can be used to make parenting between objects and data without the need of graph to path then path to graph conversion (so it is also much faster.. but we don't really care here right ?). For consistency with the "value" in data field the PR expose this link path with a dedicated "linkpath" attribute. So the code is now the following: ```python root.addObject("MeshObjLoader", name="loader") root.addChild("MechanicalObject", name="p1", position=root.loader.position.value) # Copy the value root.addChild("MechanicalObject", name="p2", position=root.loader.position.linkpath) # Make a link between data without string query root.addObject("Mapping", name="loader", input=p1.linkpath) ```
eb83067
to
2484296
Compare
…ptr on a smart pointer.
@damienmarchal do you aim at deprecating getLinkPath() ? Examples of SP3 could be also updated with this new API |
Given that str(myobj.linkpath) produce the same as myobj.getLinkPath() yes we could safely deprecate getLinkPath() (and we probably should). But given:
The transition workflow I think is wise would be:
|
Co-authored-by: Alex Bilger <[email protected]>
It seems that MacOS CI is broken |
@hugtalbot so what ? |
This PR is adding a new feature linkpath that is a "non string" approach to make link between data or objects. node.addObject("toto", position=otherObject.position.linkpath) # makes a link without path conversion explicitely
node.addObject("toto", position=otherObject.position.value) # makes a copy to initialize instead of: node.addObject("toto", position=otherObject.position) # makes a link without path conversion implicitely
node.addObject("toto", position=otherObject.position.value) # makes a copy to initialize Hugo suggested to deprecate getLinkPath()... I'm not sure it is the proper PR to do that because it is not wise to deprecate a long standing feature by a modernized replacement. There are an additional point of dicussion: # The existing version in SofaPython3... is to makes a link with a stringyfied path (so generation and parse) and to work this imply being in the same graph for path resolution suceed at init and so on).
node.addObject("toto", position=position.getLinkPaht())
# The existing version in InSimo is functionally strictly equivalent to getLinkPath() except that it use a python attribute to make the syntax more "slick"
node.addObject("toto", position=position.path)
# The version in this PR is *not* a replacement of getLinkPath it is superset.
node.addObject("toto", position=position.linkpath) # makes a link without path conversion
node.addObject("toto", position=str(position.linkpath)) # makes a link with path conversion An open question is wether we prefer to do: node.addObject("toto", position=position.linkpath) # makes a link without path conversion
node.addObject("toto", position=str(position.linkpath)) # makes a link with path conversion or to add a "shortcut" for str(position.linkpath) node.addObject("toto", position=position.linkpath) # makes a link without path conversion
node.addObject("toto", position=str(position.linkpath)) # makes a link with path conversion
node.addObject("toto", position=position.path) # makes a link with path conversion In general I think it is better to learnability and API understanding to have a single way of doing things instead of two. Otherwise people needs acquire the knowledge that linkpath and path are not equivalent. |
Principle ok, but we need to create an issue keeping track of the change |
PR has some conflicts poke @damienmarchal |
Hi @hugtalbot, thank for pointing I will look at that after the release. |
I like your proposal:
with a deprecation of |
This PR is following the discussion in #197
The existing syntax to make links in SofaPython3 is the following one:
In addition to be very verbose the getLinkPath() is returning a string (eg: "@/node/loader.position").
Representing linkpath with string has the drawback of forcing the two objects to be part of the same simulation graph which is not always the case.
Example of code that does not work as expected:
It does not work because string returned by getLinkPath() is a path query into the scene-graph to locate the object or the data
field to attach not into the one of the graph the destination object is.
To solve this issue this PR introduce a LinkPath object which is dedicated to point to a sofa object.
This LinkPath object is exposed in python and can be used to make parenting between objects and data without the need of graph to path then path to graph conversion (so it is also much faster.. but we don't really care here right ?).
For consistency with the "value" in data field the PR expose the LinkPath with a dedicated "linkpath" attribute.
So the code is now the following: