.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_examples/50-advanced/00-events.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr__examples_50-advanced_00-events.py: .. _ref_events: Asynchronous event support ========================== Every attribute change may have an event callback associated with it. This example explores different connection mechanisms and different mechanisms for getting data values. .. GENERATED FROM PYTHON SOURCE LINES 14-18 Start an EnSight session ------------------------ Launch and connect to an instance of EnSight. This example uses a local EnSight installation. .. GENERATED FROM PYTHON SOURCE LINES 18-27 .. code-block:: default from urllib.parse import parse_qs, urlparse from IPython.display import display from ansys.pyensight.core import LocalLauncher from ipywidgets import widgets session = LocalLauncher().start() .. GENERATED FROM PYTHON SOURCE LINES 28-35 Register an event callback -------------------------- The simplest case for registering an event callback is for a specific attribute on a specific object. This example registers an event callback to the ``ensight.objs.core`` object. Whenever the ``PARTS`` attribute changes, the callback function is called when a dataset is loaded. Every callback function includes a string that is returned as a parameter to the callback function. .. GENERATED FROM PYTHON SOURCE LINES 35-47 .. code-block:: default partlist_disp = widgets.HTML() display(partlist_disp) def part_list(name: str): partlist_disp.value = f"Event: {name}" session.add_callback(session.ensight.objs.core, "partlist_name", ["PARTS"], part_list) .. GENERATED FROM PYTHON SOURCE LINES 48-60 Load a dataset -------------- This code loads some data included in the EnSight installation and brings up an interactive viewer for the scene. .. image:: /_static/00_events_0.png The callback string is ``grpc://.../partlist_name?enum=PARTS&uid=220``. The callback is in the form of a URI, where "partlist_name" is the string from the call to the :func:`add_callback` method. The name of the attribute is always returned in ``enum``, and the ID of the object is returned in ``uid``. .. GENERATED FROM PYTHON SOURCE LINES 60-65 .. code-block:: default session.load_data(f"{session.cei_home}/ensight{session.cei_suffix}/data/guard_rail/crash.case") render = session.show("remote") .. GENERATED FROM PYTHON SOURCE LINES 66-76 Register an event callback on a class ------------------------------------- Events can be associated with classes as well. This code associates a callback with all part objects, listening to both the ``VISIBLE`` and ``COLORBYRGB`` attributes. The ``urllib`` module is used to parse out the returned value. .. image:: /_static/00_events_1.png After running this code, the cell value calls out the change in the color of the windshield. .. GENERATED FROM PYTHON SOURCE LINES 76-95 .. code-block:: default part_disp = widgets.HTML() display(part_disp) def part_event(uri: str): p = urlparse(uri) q = parse_qs(p.query) obj = session.ensight.objs.wrap_id(int(q["uid"][0])) value = obj.getattr(q["enum"][0]) part_disp.value = f"Part: {obj}, Attribute: {q['enum'][0]} Value: {value}" attribs = [session.ensight.objs.enums.VISIBLE, session.ensight.objs.enums.COLORBYRGB] session.add_callback("'ENS_PART'", "partattr", attribs, part_event) session.ensight.objs.core.PARTS["hood"][0].COLORBYRGB = [1.0, 0.0, 0.0] .. GENERATED FROM PYTHON SOURCE LINES 96-102 Trigger with the ``VISIBLE`` attribute -------------------------------------- This code triggers the same callback when changes are madee to the ``VISIBLE`` attribute. .. image:: /_static/00_events_2.png .. GENERATED FROM PYTHON SOURCE LINES 102-106 .. code-block:: default session.ensight.objs.core.parts["windshields"].set_attr(session.ensight.objs.enums.VISIBLE, True) .. GENERATED FROM PYTHON SOURCE LINES 107-117 Trigger with a callback macro ----------------------------- This code extends the previous example by using the macro mechanism to capture both visibility and RGB color values. The ``name`` string includes a mechanism for including target object values directly in the returned URI. This mechanism avoids the need to make PyEnSight calls from within a callback function. This can avoid reentrancy and performance issues. This approach is more efficient than the approach used in the previous example. .. image:: /_static/00_events_3.png .. GENERATED FROM PYTHON SOURCE LINES 117-137 .. code-block:: default macro_disp = widgets.HTML() display(macro_disp) def macro_event(uri: str): p = urlparse(uri) q = parse_qs(p.query) obj = session.ensight.objs.wrap_id(int(q["uid"][0])) obj.getattr(q["enum"][0]) macro_disp.value = f"Part: {obj}, Attr: {q['enum'][0]} Visible: {q['visible']} RGB: {q['rgb']}" attribs = [session.ensight.objs.enums.VISIBLE, session.ensight.objs.enums.COLORBYRGB] name = "partmacro?visible={{VISIBLE}}&rgb={{COLORBYRGB}}" session.add_callback("'ENS_PART'", name, attribs, macro_event) session.ensight.objs.core.PARTS["hood"][0].COLORBYRGB = [0.0, 1.0, 0.0] .. GENERATED FROM PYTHON SOURCE LINES 138-141 Close the session ----------------- Close the connection and shut down the EnSight instance. .. GENERATED FROM PYTHON SOURCE LINES 141-144 .. code-block:: default # sphinx_gallery_thumbnail_path = '_static/00_events_3.png' session.close() .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.000 seconds) .. _sphx_glr_download__examples_50-advanced_00-events.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 00-events.py <00-events.py>` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 00-events.ipynb <00-events.ipynb>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_