Script Example
The following example outlines the procedure for scripting MMO&B form sets; including the required options; recording a form set; example code and information on scripting the recorded form set. Details on setting Script Options and using the Script Editor can be accessed via the links.
Scripting MMO&B Form Sets
To begin, configure the script options for the form set recording example:
Script Options
Alternatively, click the Project tab to open the backstage menu and select Options | System | Script Options.
Recording the Form Set
The Record button is shown as 'active':
Select the appropriate option to run the target form set. This example uses the File | Edit Data | Calculate | Calculate (Expression) option:
Toggle the Script | Scripts | Record option OFF to end recording.
When prompted, name and save the recorded script in the appropriate folder. For example, “C:\Scripts\OffsetEastings.py”.
Note: To minimise downstream complications, do not include spaces in the name.
When the Object oriented script Recording Style option is set in the Script Options, the form set is recorded as a set of nested Python classes.
If the Auto load script after recording option is selected in the Script Options, the recorded script will be displayed in the Script Editor:
At this point, the form set can be re-run simply by selecting the Script | Scripts | Run Script option, or the Run icon in the toolbar, to execute the recorded script:
Scripting the Recorded Form Set
The recorded form set script can be customised as required, which may be sufficient for many simple tasks.
Integrating the Recorded Form Set
If the script will be integrated into a more significant set of scripts, possibly with recorded scripts from other form sets, it will be easier to update the scripts in the future if each recorded form set script is saved in a separate script file. The script for any of the included form sets can then be updated simply by recording a new script to the appropriate file. Unless new parameters are required for the updated script, the changes will be reflected immediately and no changes to any of the calling scripts will be required.
The Python import keyword facilitates this. The code below illustrates how the Scripts\OffsetEastings.py script can be called from another script file:
import MMpy
# import recorded form set class with descriptive alias
from Scripts.OffsetEastings import FormSet0 as OffsetEastingsForm
# create a new instance of the form set class
offset_eastings_form = OffsetEastingsForm()
# build and run the form set
offset_eastings_form.build().run()
Since the script is saved in Scripts\OffsetEastings.py, the name of the module to be imported is Scripts.OffsetEastings.
The form set is recorded as the FormSet0 class within this module. However, since every other form will also be recorded to its own script file as FormSet0, it is useful to assign a descriptive alias to each imported form set. In this example, the assigned alias is OffsetEastingsForm.
(The FormSet0 class is based on the MMpy.FormSetBuilder class, which is available from the MMOB Python API.)
The last lines in the example code create an instance of the imported form set class using the alias, and then build and run the form set.
Note: The following line should also be added to the top of the recorded script file (“C:\Scripts\OffsetEastings.py”) to support this functionality:
import MMpy
Setting Field Values
When the form set is recorded, the current field values are written to and set in the __init__ methods of the FormSet0 class and the classes nested within it.
The values recorded for FormSet0 in the example script are reproduced below:
def __init__(self):
self['File'] = 'Small Block Model' # type:filename
self['Type'] = 0 # type:choice
self['Use'] = False # type:Boolean
self['Filter'] = NestedFormSet0() # type:formset
self['Clear'] = True # type:Boolean
self['Overwrite'] = True # type:Boolean
self['Exprgrid'] = DataGrid1() # type:grid
self['CharMissing'] = False # type:Boolean
self['BlankMissing'] = True # type:Boolean
self['HalfMissing'] = False # type:Boolean
self['Modify'] = False # type:boolean
With the exception of the fields that create new instances of nested classes (NestedFormSet0 and DataGrid1 in this example), field values can be replaced as required after the instance of the form set class has been created by the calling script. For example, to change the name of the file that is processed and the expression applied to it:
import MMpy
from Scripts.OffsetEastings import FormSet0 as OffsetEastingsForm
offset_eastings_form = OffsetEastingsForm()
# replace field values here as required
offset_eastings_form['File'] = 'Another Block Model'
offset_eastings_form['Exprgrid'][0] = ['=[North]+2500.0', 'Offset North']
offset_eastings_form.build().run()