Retrieve selected element raises attribute error


I’m trying to retrieve a selected element from a model and then modify its formatting.

I use the following code:

# include needed for the capella modeller API
if False:
    from simplified_api.capella import *
# include needed for utilities
if False:
    from utilities.CapellaPlatform import *
# SAM_path = '/SAMSEM/SAMSEM.aird'
# aird_path = SAM_path

# retrieve the Element from the current selection and its aird model path
selected_elem = CapellaElement(CapellaPlatform.getFirstSelectedElement())
aird_path = '/' + CapellaPlatform.getModelPath(selected_elem)

# instantiate Capella model
model = CapellaModel()

# get the SystemEngineering and print its name
se = model.get_system_engineering()
print('Model accessed:'+ se.get_name())

# start transactions to make modifications in the model

    #do the work
    print('I am trying')
    # if something went wrong rollback the transaction
    # if everything is ok commit the transaction

When I run this, get the following error:

org.eclipse.ease.ScriptExecutionException: Traceback (most recent call last):
  File "workspace://Python4Capella/MBSEcodes/", line 17, in <module>
    # sys.argv[2:] - optional - paths to prepend on sys.path
  File "workspace://Python4Capella/simplified_api/", line 336, in __init__
AttributeError: Passed object is not compatible with CapellaElement: L/Python4Capella/MBSEcodes/

Any help on how to solve this issue?
Also, I would appreciate any help around how to access the element’s formatting attributes, e.g., its name font size?


When you started your script the selected element was the script itself (its workspace resource) L/Python4Capella/MBSEcodes/ And you tried to wrap it to a CapellaElement Python class, each constructor has a check to make sure only compatible elements are passed to the constructor (either Python or Java objects).

To use the current selection you probably want to run you script from a popup menu using an EASE magic header at the beginning of your script:

# name                 :
# script-type          : Python
# description          :
# popup                : enableFor(

Then select an element from the Capella model in the package explorer (resource tree on the left). Right click on it and select the menu. You can learn more in the tips and tricks section of the documentation. There is also some examples in the sample scripts, for instance this one.

Thanks Yvan,

I was able to create the popup menu, however, what I’m trying to do is to modify the formatting of certain element’s representation in a certain diagram. So I wanted to select the element representation form the diagram then access its formatting then modify it, let’s say modify the font size.

Any guidance on how to that?

When I right click the element representation in a diagram and select “paperUtils”, I get the following error:

org.eclipse.ease.ScriptExecutionException: Traceback (most recent call last):
  File "file:///Users/ea/Library/CloudStorage/OneDrive-UNSW/Researchia/CSC_works/MBSE/MBSEcodes/", line 20, in <module>
    import code as _pyease_code
  File "workspace://Python4Capella/simplified_api/", line 336, in __init__
AttributeError: Passed object is not compatible with CapellaElement: DNodeEditPart( org.eclipse.gmf.runtime.notation.impl.NodeImpl@3b8a1d56 (visible: true, type: 2001, mutable: false) )

I understand now that the selection from diagram is not a CapellaELement, but I have no clue how to access the right type?

I tried the following snippet:

specific_cls = EObject.get_class(CapellaPlatform.getFirstSelectedElement())
selected = None
if specific_cls is not None:
    selected = specific_cls(CapellaPLatform.getFirstSelectedElement())

but that didn’t work :frowning:

1 Like

There is no API to do that with Python4Capella, but you can still can the Java API from your python script. In this thread you will find how to declare your menu or elements from a diagram. You can then check this thread to change the color of an element. For the layout you can check his thread.

If you change the style of a diagram element for instance its color, you will also need to mark the feature as customized, otherwise it will be reseted after a refresh of the diagram:


an other example to set a workspace image and change the label position:

    style = representation_element.getOwnedStyle()
    helper =
    helper.updateStyle(style, "In-Flight Entertainment System/music.svg")

The PyDev editor will complain that some symbols are not recognized (for instance org), but as runtime EASY will find them. You can remove this error by following this documentation.