Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/BaseCamera.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def Sphere(rootNode, name, position, color):
#### Visualization of the sphere
sphereVisu = sphere.addChild("VisualModel")
sphereVisu.loader = sphereVisu.addObject('MeshObjLoader', name="loader",
filename="mesh/ball.obj", scale=0.5)
filename="mesh/ball.obj", scale=0.5)
sphereVisu.addObject('OglModel', name="model", src="@loader", color=color)
sphereVisu.addObject('RigidMapping')
return sphere
Expand Down
17 changes: 8 additions & 9 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
project(Examples)

set(EXAMPLES_FILES
${CMAKE_CURRENT_SOURCE_DIR}/BaseCamera.py
${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt
${CMAKE_CURRENT_SOURCE_DIR}/ControllerScene.py
${CMAKE_CURRENT_SOURCE_DIR}/easingSceneMatplotlib.py
${CMAKE_CURRENT_SOURCE_DIR}/easingScene.py
${CMAKE_CURRENT_SOURCE_DIR}/basic.py
${CMAKE_CURRENT_SOURCE_DIR}/basic-addGUI.py
${CMAKE_CURRENT_SOURCE_DIR}/emptyController.py
${CMAKE_CURRENT_SOURCE_DIR}/emptyDataEngine.py
${CMAKE_CURRENT_SOURCE_DIR}/emptyForceField.py
${CMAKE_CURRENT_SOURCE_DIR}/example-forcefield2.py
${CMAKE_CURRENT_SOURCE_DIR}/example-forcefield.py
${CMAKE_CURRENT_SOURCE_DIR}/example.pyscn
${CMAKE_CURRENT_SOURCE_DIR}/example-scriptcontroller.py
${CMAKE_CURRENT_SOURCE_DIR}/BaseCamera.py
${CMAKE_CURRENT_SOURCE_DIR}/easingSceneMatplotlib.py
${CMAKE_CURRENT_SOURCE_DIR}/easingScene.py
${CMAKE_CURRENT_SOURCE_DIR}/keyEvents.py
${CMAKE_CURRENT_SOURCE_DIR}/realTimeClockScene.py
${CMAKE_CURRENT_SOURCE_DIR}/ReadTheDocs_Example.py
${CMAKE_CURRENT_SOURCE_DIR}/pygame_renderingloop/backend_pygame.py
${CMAKE_CURRENT_SOURCE_DIR}/pygame_renderingloop/pygame_test.py
${CMAKE_CURRENT_SOURCE_DIR}/ReadTheDocs_Example.py
${CMAKE_CURRENT_SOURCE_DIR}/realTimeClockScene.py
${CMAKE_CURRENT_SOURCE_DIR}/SofaGui.py
${CMAKE_CURRENT_SOURCE_DIR}/test.scn
${CMAKE_CURRENT_SOURCE_DIR}/additional-examples/ControllerScene.py
)

add_custom_target(${PROJECT_NAME} SOURCES ${EXAMPLES_FILES})
Expand Down
21 changes: 0 additions & 21 deletions examples/SofaGui.py

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ def onKeypressedEvent(self, c):
print("You just switch to rotation control ")
self.move = 1


def createScene(root):

# rpath =os.environ["SOFA_ROOT"]+"../src/share/mesh/"
Expand All @@ -91,35 +92,41 @@ def createScene(root):

loader = root.addObject('MeshObjLoader', name='loader',
filename="mesh/liver.obj")
te = root.addObject(
"TransformEngine", name="te", input_position=loader.position.getLinkPath(), rotation=[0,0,0])
te = root.addObject("TransformEngine", name="te",
input_position=loader.position.getLinkPath(), rotation=[0,0,0])
mo = root.addObject("MechanicalObject", name="mo",
position=te.output_position.getLinkPath())

visu = root.addChild("Visu")
visu.addObject('OglModel', name="visu", src=loader.getLinkPath())
visu.addObject('IdentityMapping', name="BM", src=mo.getLinkPath())

# If script is loaded by SOFA (runSofa), this script must be added as a controller
if not _runAsPythonScript:
root.addObject(RotationController(name="MyController", engine=root.te))


def main():
# can be executed from terminal directly:

# Register all the common component in the factory.
# Load the required plugins
SofaRuntime.importPlugin("SofaOpenglVisual")
SofaRuntime.importPlugin("SofaGeneralEngine")

# Check and save if the script is called from python environment
global _runAsPythonScript
_runAsPythonScript = True
root = Sofa.Core.Node()

# Create and initialize the scene
root = Sofa.Core.Node()
createScene(root)
Sofa.Simulation.init(root)

# Run simulation
for i in range(0, 360):
Sofa.Simulation.animate(root, root.dt.value)
root.te.rotation[0] += 1
#print("For i = "+ str(i)+", we have : "+str(root.te.rotation.value[0]))

print("Last value is : "+ str(root.te.rotation.value[0]))


if __name__ == '__main__':
main()
62 changes: 62 additions & 0 deletions examples/basic-addGUI.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Required import for python
import Sofa
import SofaRuntime
import Sofa.Gui


# Choose in your script to activate or not the GUI
USE_GUI = True


def main():
# Make sure to load all SOFA libraries
SofaRuntime.importPlugin("SofaOpenglVisual")

#Create the root node
root = Sofa.Core.Node("root")
# Call the below 'createScene' function to create the scene graph
createScene(root)
Sofa.Simulation.init(root)

if not USE_GUI:
for iteration in range(10):
Sofa.Simulation.animate(root, root.dt.value)
else:
# Find out the supported GUIs
print ("Supported GUIs are: " + Sofa.Gui.GUIManager.ListSupportedGUI(","))
# Launch the GUI (qt or qglviewer)
Sofa.Gui.GUIManager.Init("myscene", "qglviewer")
Sofa.Gui.GUIManager.createGUI(root, __file__)
Sofa.Gui.GUIManager.SetDimension(1080, 1080)
# Initialization of the scene will be done here
Sofa.Gui.GUIManager.MainLoop(root)
Sofa.Gui.GUIManager.closeGUI()
print("GUI was closed")

print("Simulation is done.")


# Function called when the scene graph is being created
def createScene(root):
# Scene must now include a VisualLoop
root.addObject('DefaultVisualManagerLoop')

# Scene must now include a AnimationLoop
root.addObject('DefaultAnimationLoop')

# Add new nodes and objects in the scene
node1 = root.addChild("Node1")
node2 = root.addChild("Node2")

node1.addObject("MechanicalObject")
node1.addObject("OglModel")

node2.addObject("MechanicalObject")
node2.addObject("OglModel")

return root


# Function used only if this script is called from a python environment
if __name__ == '__main__':
main()
43 changes: 43 additions & 0 deletions examples/basic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Required import for python
import Sofa
import SofaRuntime


def main():
# Make sure to load all SOFA libraries
SofaRuntime.importPlugin("SofaOpenglVisual")

# Call the above function to create the scene graph
root = Sofa.Core.Node("root")
createScene(root)

# Once defined, initialization of the scene graph
Sofa.Simulation.init(root)

# Run the simulation for 10 steps
for iteration in range(10):
print(f'Iteration #{iteration}')
Sofa.Simulation.animate(root, root.dt.value)

print("Simulation made 10 time steps. Done")


# Function called when the scene graph is being created
def createScene(root):

# Add new nodes and objects in the scene
node1 = root.addChild("Node1")
node2 = root.addChild("Node2")

node1.addObject("MechanicalObject", position="0 0 0")
node1.addObject("OglModel")

node2.addObject("MechanicalObject", position="1 1 1")
node2.addObject("OglModel")

return root


# Function used only if this script is called from a python environment
if __name__ == '__main__':
main()
98 changes: 82 additions & 16 deletions examples/emptyController.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,110 @@
import Sofa.Core
import Sofa
import Sofa.Gui


# This python script shows the functions to be implemented
# in order to create your Controller in python
class EmptyController(Sofa.Core.Controller):
""" custom %EmptyController% component for SOFA """

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)

# Default BaseObject functions********************************
def init(self):
pass

def init():
def bwdInit():
pass

def reinit():
pass

# DEFAULT EVENTS:
def onAnimateBeginEvent(self, event):
""" called at the beginning of each time step """
# Default Events *********************************************
def onAnimateBeginEvent(self, event): # called at each begin of animation step
pass

def onAnimateEndEvent(self, event):
""" called at the end of each time step """
def onAnimateEndEvent(self, event): # called at each end of animation step
pass

def onKeypressedEvent(self, event):
""" called when a key release event is triggered from the UI """
pass
key = event['key']
if ord(key) == 19: # up
print("You pressed the Up key")

if ord(key) == 21: # down
print("You pressed the Down key")

if ord(key) == 18: # left
print("You pressed the Left key")

if ord(key) == 20: # right
print("You pressed the Right key")

def onKeyreleasedEvent(self, event):
""" called when a key release event is triggered from the UI """
pass
key = event['key']
if ord(key) == 19: # up
print("You released the Up key")

if ord(key) == 21: # down
print("You released the Down key")

if ord(key) == 18: # left
print("You released the Left key")

if ord(key) == 20: # right
print("You released the Right key")

def onMouseEvent(self, event):
""" called when a mouse event is triggered from the UI """
pass
if (event['State']== 0): # mouse moving
print("Mouse is moving (x,y) = "+str(event['mouseX'])+" , "+str(event['mouseY']))

if (event['State']==1): # left mouse clicked
print("Left mouse clicked")

if (event['State']==2): # left mouse released
print("Left mouse released")

if (event['State']==3): # right mouse released
print("Right mouse clicked")

if (event['State']==4): # right mouse released
print("Right mouse released")

if (event['State']==5): # wheel clicked
print("Mouse wheel clicked")

if (event['State']==6): # wheel released
print("Mouse wheel released")

def onScriptEvent(self, event):
""" catches events sent from other python scripts """
pass

def onEvent(self, event):
""" generic method called when no script method exists for the sent event """
pass


def createScene(root):
root.dt = 0.01
root.addObject('DefaultVisualManagerLoop')
root.addObject('DefaultAnimationLoop')

# Add our python controller in the scene
root.addObject( EmptyController(name="MyEmptyController") )


def main():
root=Sofa.Core.Node("root")
createScene(root)

Sofa.Gui.GUIManager.Init("myscene", "qglviewer")
Sofa.Gui.GUIManager.createGUI(root, __file__)
Sofa.Gui.GUIManager.SetDimension(1080, 1080)
Sofa.Gui.GUIManager.MainLoop(root)
Sofa.Gui.GUIManager.closeGUI()

print("End of simulation.")


if __name__ == '__main__':
main()
Loading