SofaPython3 icon indicating copy to clipboard operation
SofaPython3 copied to clipboard

[Binding/Sofa.Core] Call init() by default when creating a python object

Open damienmarchal opened this issue 3 years ago • 9 comments

The default behavior should be the most common use case while more rare scenario should be accessible but with explicit call api (opt-in vs opt-out strategy).

Currently the default case is:

   m = r.addObject("MechanicalObject", position=[1,2,3])
   m.init()
   l.rest_position() ## Rest position is created from position at init

This is a bit weird as one may expect that once "created" in python the object is immediately accessible in an inited state (not doing so imply user to call init() at every lines).

After the PR the default case become:

   m = r.addObject("MechanicalObject", position=[1,2,3])
   l.rest_position()

But for rare use case where it makes sense the no init behavior can still be done explicitly:

   m = r.addObject("MechanicalObject", position=[1,2,3], __noInit=True)
   l.rest_position() # Rest position is "uninited"
   m.init()          # You can control when you init the object
   l.rest_position() #

Unit tests are provided to validate the behavior.

The PR use the Sofa.future module to let users to enable/disable the behavior. By default, the SofaPython behavior is preserved (so no auto-init)

If you want to test you just need to do

from Sofa.Lifetime import __feature__

def createScene(root)
     with __feature__("object_auto_init", True):
               root.addObject(....)

This PR depends on #184

damienmarchal avatar Sep 10 '21 11:09 damienmarchal

@AlbanOdot @jnbrunet @RobinEnjalbert what do you think of this auto-init feature? Your input would be valuable to us!

hugtalbot avatar Oct 27 '21 09:10 hugtalbot

@hugtalbot so as there was no answer it will be time to plan updating and merging ;)

damienmarchal avatar Apr 11 '22 11:04 damienmarchal

Related to the need for init: https://github.com/sofa-framework/sofa/issues/3012

damienmarchal avatar Jun 09 '22 10:06 damienmarchal

Reviews taken into account.

However, this scene often crashes with such error:

double free or corruption (out)

########## SIG 6 - SIGABRT: usually caused by an abort() or assert() ##########

I am suspecting the engine TextureInterpolation

hugtalbot avatar Jun 17 '22 10:06 hugtalbot

@hugtalbot

Thanks for the feedback, can you provide a scene that crash (or elaborate a bit more).

As the risk of incompatibility with some component we don't have time to fix is high this is why this PR rely on the use of #184

     with __feature__("autoinit"): 
              blabblah

damienmarchal avatar Jun 17 '22 10:06 damienmarchal

the scene here crashes @damienmarchal quite randomly.

What I wrote regarding the init should work fine, shouldn't it?

hugtalbot avatar Jun 17 '22 10:06 hugtalbot

@hugtalbot Sorry I'm lost, what do you mean by "here" ?

damienmarchal avatar Jun 17 '22 10:06 damienmarchal

sorry I initially posted in the wrong PR (I was targeting #204) ! sorry for perturbating this one!

hugtalbot avatar Jun 17 '22 12:06 hugtalbot

To clarify the current situation of this PR, it depends on https://github.com/sofa-framework/SofaPython3/pull/184 which is still in wip. So, let's focus on https://github.com/sofa-framework/SofaPython3/pull/184 to move forward on this one.

alxbilger avatar Jun 21 '22 14:06 alxbilger