Exemple #1
0
def createScene(rootNode):

    import math

    def animation(target, factor):
        target.angleIn = math.cos(factor * 2 * math.pi)

    Scene(rootNode)

    rootNode.dt = 0.003
    rootNode.gravity = [0., -9810., 0.]
    rootNode.addObject('VisualStyle', displayFlags='showBehaviorModels')

    # Use these components on top of the scene to solve the constraint 'StopperConstraint'.
    rootNode.addObject('FreeMotionAnimationLoop')
    rootNode.addObject('GenericConstraintSolver',
                       maxIterations=1e3,
                       tolerance=1e-5)

    simulation = rootNode.addChild('Simulation')
    simulation.addObject('EulerImplicitSolver',
                         rayleighStiffness=0.1,
                         rayleighMass=0.1)
    simulation.addObject('CGLinearSolver', name='precond')

    ServoMotor(simulation, showWheel=True)
    animate(animation, {'target': simulation.ServoMotor},
            duration=5.,
            mode='loop')

    return rootNode
Exemple #2
0
def creteScene(rootNode):

    import math

    def animation(target, factor):
        target.angle = math.cos(factor * 2 * math.pi)
        target.setX(math.cos(factor * 2 * math.pi))

    Scene(rootNode)

    rootNode.dt = 0.003
    rootNode.gravity = [0., -9810., 0.]
    rootNode.addObject("VisualStyle", displayFlags="showBehaviorModels")

    # Use these components on top of the scene to solve the constraint "StopperConstraint".
    rootNode.addObject("FreeMotionAnimationLoop")
    rootNode.addObject("GenericConstraintSolver",
                       maxIterations=1e3,
                       tolerance=1e-5)

    simulation = rootNode.addChild("Simulation")
    simulation.addObject("EulerImplicitSolver",
                         rayleighStiffness=0.1,
                         rayleighMass=0.1)
    simulation.addObject("CGLinearSolver", name="precond")

    servomotor = ServoMotor(simulation, showWheel=True)
    animate(animation, {"target": servomotor}, duration=5., mode="loop")

    return rootNode
Exemple #3
0
    def initTripod(self, key):
        if key == Key.A and self.serialportctrl.state == "init":
            self.serialportctrl.state = "no-comm"
            animate(setupanimation, {"actuators": self.actuators, "step": 35.0, "angularstep": -1.4965}, duration=0.2)

        # Inclusion of the keystroke to start data sending = establishing communication ('comm')
        if key == Key.B and self.serialportctrl.state == "no-comm":
            self.serialportctrl.state = "comm"
Exemple #4
0
def createScene(rootNode):
    from splib3.animation import animate
    from stlib3.scene import Scene
    scene = Scene(rootNode)
    scene.addObject("EulerImplicitSolver")
    scene.addObject("SparseLDLSolver")
    scene.VisualStyle.displayFlags = "showBehavior"

    arm1 = ActuatedArm(scene, name="arm1", translation=[-2.0, 0.0, 0.0])
    arm1.addObject("FixedConstraint")

    def myanimate(target, factor):
        target.angle = factor

    animate(myanimate, {"target": arm1.ServoMotor},
            duration=0.5,
            mode="pingpong")
Exemple #5
0
def createScene(rootNode):
    from splib3.animation import animate
    from splib3.animation.easing import LinearRamp
    from splib3.scenegraph import get

    from stlib3.scene import MainHeader
    scene = Scene(rootNode)
    s = DefaultSolver(rootNode)

    # Test a assembly that also implements a KinematicMotorController
    # The angle of the KinematicMotorController is dynamically changed using a
    # animation function
    servomotor = ServoMotor(rootNode, translation=[2, 0, 0])

    def myAnimation(motorctrl, factor):
        motorctrl.angle = LinearRamp(-3.14 / 2, 3.14 / 2, factor)

    animate(myAnimation, {"motorctrl": servomotor.node},
            duration=1.0,
            mode="pingpong")
def createScene(rootNode):
    from splib3.animation import animate
    from stlib3.scene import Scene
    import math

    scene = Scene(rootNode)
    scene.VisualStyle.displayFlags = 'showBehavior'
    rootNode.dt = 0.003
    rootNode.gravity = [0., -9810., 0.]

    simulation = rootNode.addChild('Simulation')
    simulation.addObject('EulerImplicitSolver',
                         rayleighStiffness=0.1,
                         rayleighMass=0.1)
    simulation.addObject('CGLinearSolver', name='precond')

    arm = ActuatedArm(simulation,
                      name='ActuatedArm',
                      translation=[0.0, 0.0, 0.0])

    def myanimate(target, factor):
        target.angleIn = math.cos(factor * 2 * math.pi)

    animate(myanimate, {'target': arm.ServoMotor}, duration=5, mode='loop')
Exemple #7
0
 def initTripod(self, key):
     if key == Key.A:
         animate(setupanimation,
                 {"actuators": self.actuators, "step": 35.0, "angularstep": -1.4965},
                 duration=0.2,
                 onDone=saveTripodPosition)
Exemple #8
0
def saveElements(node, dt, forcefield):
    '''
    **Depending on the forcefield will go search for the right kind
    of elements (tetrahedron/triangles...) to save**

    +------------+-----------+-------------------------------------------------------------------------+
    | argument   | type      | definition                                                              |
    +============+===========+=========================================================================+
    | node       | Sofa.node | from which node will search to save elements                            |
    +------------+-----------+-------------------------------------------------------------------------+
    | dt         | sc        | time step of our SOFA scene                                             |
    +------------+-----------+-------------------------------------------------------------------------+
    | forcefield | list(str) || list of path to the forcefield working on the elements we want to save |
    |            |           || see :py:obj:`.forcefield`                                              |
    +------------+-----------+-------------------------------------------------------------------------+

    After determining what to save we will add an animation with a *duration* of 0 that will
    be executed only once when the scene is launched saving the elements.

    To do that we use :py:func:`splib.animation.animate`
    **of the** `STLIB <https://github.com/SofaDefrost/STLIB>`_ **SOFA plugin**
    '''

    import numpy as np

    #print('--------------------->  Gonna Try to Save the Elements')
    def save(node, container, valueType, **param):
        global tmp
        elements = container.findData(valueType).value
        np.savetxt('reducedFF_' + node.name.value + '_' + str(tmp) + '_' +
                   valueType + '_elmts.txt',
                   elements,
                   fmt='%i')
        tmp += 1
        print('save : ' + 'elmts_' + node.name.value + ' from ' +
              container.name.value + ' with value Type ' + valueType)

    # print('--------------------->  ',forcefield)
    for objPath in forcefield:
        nodePath = '/'.join(objPath.split('/')[:-1])
        # print(nodePath,objPath)
        #print("----------->", type(node))
        obj = get(node, objPath[1:])
        currentNode = get(node, nodePath[1:])

        if obj.getClassName() == 'HyperReducedRestShapeSpringsForceField':
            container = obj
        elif obj.getClassName() == 'HyperReducedHexahedronFEMForceField':
            container = searchObjectClassInGraphScene(currentNode,
                                                      'RegularGridTopology')[0]
        else:
            container = getContainer(currentNode)
        # print(container)
        if obj.getClassName() in forceFieldImplemented and container:
            valueType = forceFieldImplemented[obj.getClassName()]

            # print('--------------------->  ',valueType)
            if valueType:
                animate(
                    save, {
                        "node": currentNode,
                        'container': container,
                        'valueType': valueType,
                        'startTime': 0
                    }, 0)
Exemple #9
0
def addAnimation(node, phase, timeExe, dt, listObjToAnimate):
    '''
    **Add/or not animations defined by** :py:class:`.ObjToAnimate` **to the** 
    :py:obj:`splib.animation.AnimationManagerController` **thanks to** :py:func:`splib.animation.animate`
    **of the** `STLIB <https://github.com/SofaDefrost/STLIB>`_ **SOFA plugin**

    +------------------+---------------------------------+----------------------------------------------------------------------------+
    | argument         | type                            | definition                                                                 |
    +==================+=================================+============================================================================+
    | node             | Sofa.node                       | from which node will search & add animation                                |
    +------------------+---------------------------------+----------------------------------------------------------------------------+
    | phase            | list(int)                       || list of 0/1 that according to its index will activate/desactivate         |
    |                  |                                 || a :py:class:`.ObjToAnimate` contained in *listObjToAnimate*               |
    +------------------+---------------------------------+----------------------------------------------------------------------------+
    | timeExe          | sc                              || correspond to the total SOFA execution duration the animation will occure,|
    |                  |                                 || determined with *nbIterations* (of :py:class:`.ReductionAnimations`)      |
    |                  |                                 || multiply by the *dt* of the current scene                                 |
    +------------------+---------------------------------+----------------------------------------------------------------------------+
    | dt               | sc                              | time step of our SOFA scene                                                |
    +------------------+---------------------------------+----------------------------------------------------------------------------+
    | listObjToAnimate | list(:py:class:`.ObjToAnimate`) | list conaining all the ObjToAnimate that will be use to shake our model    |
    +------------------+---------------------------------+----------------------------------------------------------------------------+

    Thanks to the location parameters of an :py:class:`.ObjToAnimate`, we find the component or Sofa.node it will animate.
    *If its a Sofa.node we search something to animate by default CableConstraint/SurfacePressureConstraint.*

    '''

    toAnimate = []
    for obj in listObjToAnimate:
        nodeFound = get(node, obj.location)
        # print(nodeFound.name)
        toAnimate.append(nodeFound)

    if len(toAnimate) != len(listObjToAnimate):
        raise Exception("All Obj/Node to animate haven't been found")

    tmp = 0
    for objToAnimate in listObjToAnimate:
        if phase[tmp]:
            # print("----------------------------------> ",objToAnimate)
            if type(toAnimate[tmp]).__name__ == "Node":
                objToAnimate.item = toAnimate[tmp]
                for obj in objToAnimate.item.objects:
                    # print(obj.getClassName())
                    if obj.getClassName(
                    ) == 'CableConstraint' or obj.getClassName(
                    ) == 'SurfacePressureConstraint':
                        objToAnimate.item = obj
                        objToAnimate.params["dataToWorkOn"] = 'value'

            else:
                objToAnimate.item = toAnimate[tmp]

            if objToAnimate.item:
                objToAnimate.duration = timeExe

                animate(objToAnimate.animFct, {
                    'objToAnimate': objToAnimate,
                    'dt': dt
                }, objToAnimate.duration)
                print("Animate " + objToAnimate.location + " of type " +
                      objToAnimate.item.getClassName() +
                      "\nwith parameters :\n" + str(objToAnimate.params))

            else:
                print("Found Nothing to animate in " +
                      str(objToAnimate.location))

        tmp += 1