diff --git a/Sofa/Component/Constraint/Lagrangian/Model/src/sofa/component/constraint/lagrangian/model/FixedLagrangianConstraint.inl b/Sofa/Component/Constraint/Lagrangian/Model/src/sofa/component/constraint/lagrangian/model/FixedLagrangianConstraint.inl index 2e7bf6bfd514..cb6bbd9694e8 100644 --- a/Sofa/Component/Constraint/Lagrangian/Model/src/sofa/component/constraint/lagrangian/model/FixedLagrangianConstraint.inl +++ b/Sofa/Component/Constraint/Lagrangian/Model/src/sofa/component/constraint/lagrangian/model/FixedLagrangianConstraint.inl @@ -39,18 +39,7 @@ FixedLagrangianConstraint::FixedLagrangianConstraint(MechanicalState* template void FixedLagrangianConstraint::init() { - Inherit1::init(); - - if(this->mstate.get()) - { - this->d_componentState.setValue(sofa::core::objectmodel::ComponentState::Valid); - } - else - { - msg_error(this)<<"Data \'mstate\' not specified, unable to find a compatible mechanical state in the current context"; - this->d_componentState.setValue(sofa::core::objectmodel::ComponentState::Invalid); - } } diff --git a/Sofa/framework/Core/src/sofa/core/behavior/Constraint.h b/Sofa/framework/Core/src/sofa/core/behavior/Constraint.h index 3de988e60fd4..dd3dbb446093 100644 --- a/Sofa/framework/Core/src/sofa/core/behavior/Constraint.h +++ b/Sofa/framework/Core/src/sofa/core/behavior/Constraint.h @@ -59,6 +59,8 @@ class Constraint : public BaseConstraint, public SingleStateAccessor Constraint(MechanicalState *mm = nullptr); ~Constraint() override; + + virtual void init() override; public: Data endTime; ///< Time when the constraint becomes inactive (-1 for infinitely active) virtual bool isActive() const; ///< if false, the constraint does nothing diff --git a/Sofa/framework/Core/src/sofa/core/behavior/Constraint.inl b/Sofa/framework/Core/src/sofa/core/behavior/Constraint.inl index c6b0402ec95e..e80274dd83a2 100644 --- a/Sofa/framework/Core/src/sofa/core/behavior/Constraint.inl +++ b/Sofa/framework/Core/src/sofa/core/behavior/Constraint.inl @@ -47,6 +47,13 @@ bool Constraint::isActive() const return endTime.getValue()>getContext()->getTime(); } +template +void Constraint::init() +{ + Inherit1::init(); + Inherit2::init(); +} + template void Constraint::getConstraintViolation(const ConstraintParams* cParams, linearalgebra::BaseVector *v) { diff --git a/Sofa/framework/Core/src/sofa/core/behavior/SingleStateAccessor.h b/Sofa/framework/Core/src/sofa/core/behavior/SingleStateAccessor.h index dc748d9897af..e64052c82b7f 100644 --- a/Sofa/framework/Core/src/sofa/core/behavior/SingleStateAccessor.h +++ b/Sofa/framework/Core/src/sofa/core/behavior/SingleStateAccessor.h @@ -41,6 +41,8 @@ class SingleStateAccessor : public virtual StateAccessor { Inherit1::init(); + d_componentState.setValue(sofa::core::objectmodel::ComponentState::Valid); + if (!mstate.get()) { mstate.set(dynamic_cast< MechanicalState* >(getContext()->getMechanicalState())); @@ -48,6 +50,7 @@ class SingleStateAccessor : public virtual StateAccessor msg_error_when(!mstate) << "No compatible MechanicalState found in the current context. " "This may be because there is no MechanicalState in the local context, " "or because the type is not compatible."; + d_componentState.setValue(sofa::core::objectmodel::ComponentState::Invalid); } l_mechanicalStates.clear();