カスタム Switchboard Device プラグインを作成する方法

Switchboard からデバイスをリモート制御する独自のデバイス プラグインを Python で作成する

プロジェクトのニーズと使用するデバイスに応じて、Switchboard のデバイス機能に対して追加や拡張を行うことができます。このページでは Python で独自のデバイス プラグインを作成する方法を説明します。C++ のナレッジがあれば、リスナーの拡張により受信するメッセージの種類を増やすことができます (ここでは説明しません)。

次の手順では、たたき台として Switchboard に対応した新しいデバイス プラグインである SampleDevice を作成する方法について説明します。

  1. このデバイス プラグインが Switchboard の読み込み時に検出されるように、命名規則「 <plugin_name>\plugin_<plugin_name>.py 」に従ってフォルダと Python ファイルを「 \Engine\Plugins\VirtualProduction\Switchboard\Source\Switchboard\switchboard\devices\ 」配下に作成します。

    • SampleDevice プラグインの場合は以下のファイルを作成します。\Engine\Plugins\VirtualProduction\Switchboard\Source\Switchboard\switchboard\devices\sampledevice\plugin_sampledevice.py

  2. \Engine\Plugins\VirtualProduction\Switchboard\Source\Switchboard\switchboard\devices\device_base.py」で定義された Device クラスを拡張して「plugin_sampledevice.py」ファイルとます。

    • device_base.py」から Device をインポートします。

    • Device を継承して、新しいクラス DeviceSampleDevice を作成します。

    • switchboard/switchboard_logging.py」からエラー報告に使用する LOGGER をインポートします。

        from switchboard.devices.device_base import Device
        from switchboard.switchboard_logging import LOGGER
    
        class DeviceSampleDevice(Device):`
            def __init__(self, name, ip_address, **kwargs):
            super().__init__(name, ip_address, **kwargs)

    このファイルを Switchboard から検出できることを確認します。Switchboard を起動して [Add Device] ドロップダウン メニューを展開します。一覧に SampleDevice が表示されます。

    Adding your custom device to Switchboard

  3. SampleDevice を Switchboard に追加するだけでは、ウィジェットがビューに作成されません。「plugin_sampledevice.py」で DeviceWidget を拡張して SampleDevice ウィジェットを作成します。

    • device_widget_base.py」から DeviceWidget をインポートします。

    • DeviceWidget を継承して、新しいクラス DeviceWidgetSampleDevice を作成します。

        from switchboard.devices.device_base import Device
        from switchboard.devices.device_widget_base import DeviceWidget
        from switchboard.switchboard_logging import LOGGER
    
        class DeviceSampleDevice(Device):
            def __init__(self, name, ip_address, **kwargs):
                super().__init__(name, ip_address, **kwargs)
    
        class DeviceWidgetSampleDevice(DeviceWidget):
            def __init__(self, name, device_hash, ip_address, icons, parent=None):
                super().__init__(name, device_hash, ip_address, icons, parent=parent)

    このウィジェットが Switchboard に表示されることを確認します。Switchboard を起動して SampleDevice を追加します。最小限の SampleDevice ウィジェットがビューに表示されます。

    Switchboard custom device widget

  4. AddDeviceDialog を継承する新しいクラスを作成し、それを DeviceSampleDevice クラスのスタティック変数 add_device_dialog に割り当てることにより、新しい SampleDevice を追加する時にカスタム ダイアログを作成します。

    • device_widget_base.py」から AddDeviceDialog をインポートします。

    • PySide2 から Qt モジュールをインポートします。

    • AddDeviceDialog を継承する新しいクラス AddSampleDeviceDialog を作成し、基本クラスのコンストラクタ呼び出しで device_type パラメータに "SampleDevice" を設定します。

    • この新しいクラスのコンストラクタで、QLineEdit テキスト フィールドをダイアログに追加します。

    • DeviceSampleDeviceadd_device_dialog スタティック変数を、この新しいクラスでオーバーライドします。

          from switchboard.devices.device_base import Device
          from switchboard.devices.device_widget_base import AddDeviceDialog, DeviceWidget
          from switchboard.switchboard_logging import LOGGER
      
          from PySide2 import QtWidgets, QtGui, QtCore
      
          class AddSampleDeviceDialog(AddDeviceDialog):
              def __init__(self, existing_devices, parent=None):
                  super().__init__(device_type="SampleDevice", existing_devices=existing_devices, parent=parent)
      
                  # Create QTWidgets to add to the form
                  self.additional_text_field = QtWidgets.QLineEdit(self)
      
                  # Append the new options to the QTWidgets.QFormLayout object defined in the parent class
                  self.form_layout.addRow("Additional Text", self.additional_text_field)
      
          class DeviceSampleDevice(Device):
              # Override the add device dialog object associated with the device plugin
              add_device_dialog = AddSampleDeviceDialog
      
              def __init__(self, name, ip_address, **kwargs):
                  super().__init__(name, ip_address, **kwargs)
      
          class DeviceWidgetSampleDevice(DeviceWidget):
              def __init__(self, name, device_hash, ip_address, icons, parent=None):
                  super().__init__(name, device_hash, ip_address, icons, parent=parent)

    この新しいデバイス ダイアログが Switchboard に表示されることを確認します。Switchboard を起動して SampleDevice を追加します。追加のテキスト フィールドがダイアログに表示されます。

    Switchboard adding custom device dialog

  5. Switchboard の右側にデバイスのウィジェットを配置し、他の拡張ダイアログにタブを追加して、より多くの情報を共有できます。基本クラス Device のクラス メソッド plug_into_ui をオーバーライドして、このタブを作成します。

    • QtWidgets.QWidget を継承して、新しいクラス SampleDeviceTabView を作成します。

    • DeviceSampleDevice にクラス メンバー tab_view を作成して、このウィジェットのインスタンスを保持します。

    • DeviceSampleDevice のクラス メソッド plug_into_ui をオーバーライドし、新しいクラス SampleDeviceTabViewtab_view を初期化します。

          from switchboard.devices.device_base import Device
          from switchboard.devices.device_widget_base import AddDeviceDialog, DeviceWidget
          from switchboard.switchboard_logging import LOGGER
      
          from PySide2 import QtWidgets, QtGui, QtCore
      
          class AddSampleDeviceDialog(AddDeviceDialog):
              def __init__(self, existing_devices, parent=None):
                  super().__init__(device_type="SampleDevice", existing_devices=existing_devices, parent=parent)
      
                  # Create QTWidgets to add to the form
                  self.additional_text_field = QtWidgets.QLineEdit(self)
      
                  # Append the new options to the QTWidgets.QFormLayout self.form_layout object defined in the parent class
                  self.form_layout.addRow("Additional Text", self.additional_text_field)
      
          class DeviceSampleDevice(Device):
              add_device_dialog = AddSampleDeviceDialog # Override the default dialog for the plugin
              tab_view = None
      
              def __init__(self, name, ip_address, **kwargs):
                  super().__init__(name, ip_address, **kwargs)
      
              @classmethod
              def plug_into_ui(cls, menubar, tabs):
                  ''' Implementation of base class function that allows plugin to inject UI elements.
                  '''
      
                  if not cls.tab_view:
                      cls.tab_view = SampleDeviceTabView(parent=tabs)
      
                  tabs.addTab(cls.tab_view, 'SampleDevice Tab')
      
          class DeviceWidgetSampleDevice(DeviceWidget):
              def __init__(self, name, device_hash, ip_address, icons, parent=None):
                  super().__init__(name, device_hash, ip_address, icons, parent=parent)
      
          class SampleDeviceTabView(QtWidgets.QWidget):
              def __init__(self, parent):
                  QtWidgets.QWidget.__init__(self, parent)

    Custom device added to Switchboard

ここまでの手順で、Switchboard の新しいデバイス プラグインの作成方法を説明しました。より高度なサンプルについては、Switchboard の nDisplay デバイス プラグインを参照してください。

Unreal Engine のドキュメントを改善するために協力をお願いします!どのような改善を望んでいるかご意見をお聞かせください。
調査に参加する
キャンセル