Writing your own Scripts

Navigation:  Toolboxes > Scripting Toolbox > Scripting in Python >

Writing your own Scripts

Previous pageReturn to chapter overviewNext page

Example

We will show you how to write your own scripts using a simple example. We assume that you have installed a scripting folder and its location is:

 

'C:\Users\yourusername\Documents\20simscripting'

 

Of course you can use own location. We will copy a 20-sim model to the scripting folder and write a script that will open this model in 20-sim and run a simulation.

 

1.Copy the example model FastManipulator.emx to the Octave\tutorials folder. E.g copy:

'C:\Program Files (x86)\20-sim 5.1\Models\Examples\Drivetrains\FastManipulator.emx'

to

'C:\Users\yourusername\Documents\20simscripting\Python\tutorials\FastManipulator.emx'

2.Open a text editor (e.g. notepad) and enter the following lines:

import controllab

xxsim = controllab.XXSim()

xxsim.connect()

xxsim.open_model('FastManipulator.emx')

xxsim.process_model()

xxsim.run()

xxsim.disconnect()

3.Save the text file as:

'C:\Users\yourusername\Documents\20simscripting\Python\tutorials\myscript.py'

4.Open 20-sim.
5.Open IPython.
6.In IPython, change the local working directory. Type in the command line:

cd 'C:\Users\yourusername\Documents\20simscripting\Python\tutorials'

7.In IPython, run your own script. Type in the command line:

run myscript

Now you will see the model being loaded in 20-sim and a simulation being run.

Writing your own scripts

In the tutorial folder there are more scripts. Use these as a template for writing your own scripts and follow the guidelines below:

Location

Create your own subfolder inside your scripting working folder. This allows you to update the 20-sim scripting files when new versions of 20-sim are released.

Functions

You can find help on scripting functions in the 20-sim Editor by selecting Help - Python Scripting API.