Skip to content

How to move an object using keyboard events in Python? #4730

Discussion options

You must be logged in to vote

Hi @zhilengqilaiya
Here is a script to move (translate along x) a rigid frame:

import sys
import Sofa


class MoveRigidParticles(Sofa.Core.Controller):

    def __init__(self, *args, **kwargs):
        # These are needed (and the normal way to override from a python class)
        Sofa.Core.Controller.__init__(self, *args, **kwargs)
        self.RigidBody = kwargs.get("RigidBody")

    def onKeypressedEvent(self, event):
        key = event['key']

        if key=="+" :
            with self.RigidBody.position.writeableArray() as wa:
                wa[0][0] += 0.01

        if key=="-" :
            with self.RigidBody.position.writeableArray() as wa:
                wa[0][0] -= 0.01

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@zhilengqilaiya
Comment options

Answer selected by zhilengqilaiya
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants