Version: 9.7.0
Using salome.py module


The Python module salome.py provides a functionality to access main SALOME features from the Python console (either embedded in GUI desktop or external one).

To use salome.py module, import it into the Python interpreter and initialize it by calling salome_init() function:

import salome
salome.salome_init()

The salome.py Python module provides a set of variables and functions allowing access to different elements of the current SALOME session. This page gives a short description of most useful variables and functions.

  • orb Reference to the CORBA::ORB instance

This variable can be used to initialize different CORBA-related elements of the SALOME session (for example, naming service, etc). For example, to get an access to the SALOME naming service, you can use the following commands:

The orb variable is also useful when it is necessary to convert CORBA reference object to its string representation (IOR) and vice versa:

studyIOR = salome.orb.object_to_string(salome.myStudy)
study = salome.orb.string_to_object(studyIOR)
is_same = salome.myStudy._is_equivalent(study) # is_same = True
  • naming_service SALOME naming service instance

This variable can be used to register/find objects created in a distributed environment. For example, to get access to the SALOME Module Catalog server, use Resolve() method:

mc = salome.naming_service.Resolve('/Kernel/ModulCatalog')

Similarly, method Register() can be used to register objects in the naming service:

salome.naming_service.Register(myObject,'/My/Object/Path')
o = salome.naming_service.Resolve('/My/Object/Path')
is_same = myObject._is_equivalent(o) # is_same = True
  • lcc Life Cycle CORBA class instance

This object can be used to get access to CORBA engine part of some SALOME module, available in the current SALOME session. The following code returns a reference to the Geometry module engine, loading it if necessary:

import GEOM
geom = salome.lcc.FindOrLoadComponent('FactoryServer', 'GEOM')

Note, that in the above example, "FactoryServer" is a name of the SALOME container, where Geometry module engine should be loaded.

  • myStudy Reference to the current (active) study

This variable can be used to manipulate with the date of the study: create data tree, assign attributes of different types to the objects in a data tree, create references between objects, etc.

Note, that the term "active" or "current" study does not make much sense outise the GUI Python console. When working in GUI, user always deals with one only top-level study, which desktop is currently on the top if the windows stack. This is what is called "active study". In TUI mode (without GUI or outside GUI), user has to manipulate with studies manually; no any special control for the life cycle of the study is done. In TUI mode, salome.muStudy variable is an instance of the first study created when you call salome_init() function.

The following code demonstrates some examples of salome.myStudy variable usage. For more details please refer to the SALOMEDS.idl file documentation.

# get study name
studyName = salome.myStudy._get_Name()
# open study from file /home/user/MyStudy.hdf
salome.myStudy.Open("/home/user/MyStudy.hdf")
# save study
salome.myStudy.Save(False, False) # not using multifile save mode
# save study in ASCII format
salome.myStudy.Save(True, True) # using multifile save mode
# save study with the new file path
salome.myStudy.SaveAs("/home/user/MyStudy.hdf", False, False)
# save study with the new file path in ASCII format
salome.myStudy.SaveAs("/home/user/MyStudy.hdf", False, True)
# clear study
salome.myStudy.Clear()
# init study
salome.myStudy.Init()
# find SALOMEDS component by its type
scomponent = FindComponent("MyComponent")
# find SALOMEDS component by its entry ID
scomponent = FindComponentID("0:1:1") # "0:1:1" is a component ID
# find SALOMEDS object by its name (first found object is returned)
sobject = salome.myStudy.FindObject("MyObject")
# find SALOMEDS object by its entry ID
sobject = salome.myStudy.FindObjectID() # "0:1:1:1" is an object ID
# find SALOMEDS object by its IOR attribute
sobject = salome.myStudy.FindObjectIOR(IOR)
# find SALOMEDS object by its path in the data tree
sobject = salome.myStudy.FindObjectByPath("/MyComponent/MyObject/MySubObject")
# get SALOMEDS object's path in a study data tree
sobject_path = salome.myStudy.GetObjectPath(sobject)
# get study properties
prop = salome.myStudy.GetProperties()
prop.GetCreationDate() # get creation date
prop.IsModified() # check if study has been modified (and not yet saved)
prop.SetLocked(True) # lock the study (prohibit modifications)
prop.IsLocked() # check if study is locked
# create objects with study builder
builder = salome.myStudy.NewBuilder() # create builder
comp = builder.NewComponent("MyComponent") # create a component of the "MyComponent" type
attrName = builder.FindOrCreateAttribute(comp, "AttributeName")
attrName.SetValue("MyComponent") # set name to the component
object = builder.NewObject(comp) # create new object, a child of the component
attrName = builder.FindOrCreateAttribute(object, "AttributeName")
attrName.SetValue("MyObject") # set name to the object
attrInt = builder.FindOrCreateAttribute(object, "AttributeInteger")
attrInt.SetValue(123) # assign integer attribute to the object
attrIOR = builder.FindOrCreateAttribute(object, "AttributeIOR")
attrIOR.SetValue(IOR) # assign IOR attribute to the object (to point to some CORBA object)
# iterate through objects of the data tree with child iterator
iter = salome.myStudy.NewChildIterator(comp) # initialize from the component
iter.InitEx(True) # init recursive mode
while iter.More():
c = iter.Value()
print(c.GetID())
iter.Next()
pass
# ...
  • myStudyName Name of the current (active) study

This variable contains the name of the current (active) study. It is an equivalent of salome.myStudy._get_Name() code.

This function prints the study data object tree to the terminal window. The output for each object includes its entry ID, name, IOR (if there is one) and referenced object ID (for references). I.e. this is the same data the user can see in the Object Browser columns.

salome.DumpStudy()

This function checks if the SObject with the specified entry ID exists in the current study and returns it. Otherwise None is returned.

sobject = salome.IDToSObject("0:1:1:1") # "0:1:1:1" is an object ID

Actually this function is just a shortcut to the following code:

sobject = salome.myStudy.FindObjectID("0:1:1:1")

This function checks if the SObject with the specified entry ID exists in the current study, then retrieves IOR attribute from it and, finally, if IOR is not empty, returns CORBA object corresponding to the found SObject:

object = salome.IDToObject("0:1:1:1") # "0:1:1:1" is an object ID

Actually this function is just a shortcut to the following code:

sobject = salome.myStudy.FindObjectID("0:1:1:1")
if sobject:
object = sobject.GetObject()
else:
object = None

This function finds an object in the current study which corresponds to the specified CORBA object (i.e. it has IOR attribute, pointing to the CORBA object). If there is no corresponding SALOMEDS object in the study, None is returned:

sobject = salome.ObjectToSObject(object)

Actually this function is just a shortcut to the following code:

ior = salome.orb.object_to_string(object)
sobject = salome.myStudy.FindObjectIOR(ior)

This function finds an object in the current study which corresponds to the specified CORBA object (i.e. it has IOR attribute, pointing to the CORBA object). If the object is found, its entry ID is returned, otherwise empty string is returned:

entry = salome.ObjectToID(object)

Actually this function is just a shortcut to the following code:

ior = salome.orb.object_to_string(object)
sobject = salome.myStudy.FindObjectIOR(ior)
if sobject:
entry = sobject.GetID()
else:
entry = ""

This function adds random numerical suffix to the passed string parameter ("Study" by default) and returns the resulting string:

name_1 = salome.generateName() # name_1 is something like "Study682"
name_1 = salome.generateName("Obj") # name_1 is something like "Obj32"

This function allows to obtain the version of the component data stored in the current study.

Note
This function does not provide a current version of the component being used but that one initially stored in the study document.

The first parameter specifies the name of the component. If the specified component data is not stored in the study, the result value is "no component data". If the version of data is undefined, the result is "unknown".

The second parameter (False by default), when set to True, allows to retrieve all versions of the component data stored in the study. This is useful to check if version information is valid (the data might be updated and/or re-stored in SALOME of versions different from initial one).

# get initial version of GEOM module data stored in the study
geom_version = salome.GetComponentVersion('GEOM')
# get all versions of GEOM module data stored in the study
all_geom_versions = salome.GetComponentVersion('GEOM', True)

This function is introduced in SALOME 6.6.0 (it does not work with studies created before version 6.6.0).

Note
The study should be initialized before calling this function (see salome.myStudy).