Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1.  
  2. import bpy
  3. from mathutils import Quaternion
  4. from chordata.nodes.basenode import ChordataBaseNode, set_dirty_flag
  5. from chordata.ops import engine
  6. from chordata.utils import gui
  7.  
  8.  
  9. from chordata.copp_server.osc4py3_buildparse import oscbuildparse as oscparse
  10. from chordata.copp_server.COPP_server import COPP_Message, COPP_Base_packet
  11. import socket
  12.  
  13.  
  14. class CustomSettings(bpy.types.PropertyGroup):
  15.     """Property group for the custom node settings"""
  16.     do_transmit: bpy.props.BoolProperty(
  17.         name="forward incoming msgs", default=False, update=set_dirty_flag)
  18.    
  19.     forward_ip: bpy.props.StringProperty(
  20.         name="Forward IP", default="127.0.0.1")
  21.     forward_port: bpy.props.IntProperty(name="Forward Port", default=7000)
  22.    
  23.  
  24.  
  25. # =========================================
  26. # =           CUSTOM NODE CLASS           =
  27. # =========================================
  28.  
  29. class CustomNode(ChordataBaseNode):
  30.     '''Modify this class to create a node with custom behaviour'''
  31.     bl_idname = 'CustomNodeType'
  32.     bl_label = "Custom Node"
  33.  
  34.     # === Property Group Pointer ===================================================
  35.     # the settings in this property group will be exposed to the Chordata engine
  36.     # through the property `id_settings`
  37.     settings: bpy.props.PointerProperty(type=CustomSettings)
  38.     # ==============================================================================
  39.  
  40.     def init(self, context):
  41.         """This function is executed on node creation"""
  42.         self.width = 350.0
  43.         self.inputs.new('DataStreamSocketType', "data_in")
  44. #        self.outputs.new('DataStreamSocketType', "data_out")
  45.  
  46.     def draw_buttons(self, context, layout):
  47.         """Here you can draw the GUI of the node"""
  48.         layout.prop(self.settings, "forward_ip", text="Forward IP")
  49.         layout.prop(self.settings, "forward_port", text="Forward Port")
  50.  
  51.         layout.prop(self.settings, "do_transmit", icon="RIGHTARROW_THIN", expand=True)
  52.  
  53.  
  54.     def draw_label(self):
  55.         return "Custom Node (forward RAW)"
  56.  
  57.  
  58.     @engine.datatarget_handler(engine.DataTarget.RAW)
  59.     def out_raw(self, packet):
  60.         """Each Chordata Node can handle one or more DataTargets coming from the capture stream.
  61.        Node methods should be decorated with one or more engine.datatarget_handler to made them
  62.        available to the Chordata engine."""
  63.        
  64.         sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  65.         server_address = (self.id_settings.forward_ip, self.id_settings.forward_port)
  66.  
  67.         raw_pkt = bytearray()
  68.  
  69.         if isinstance(packet, COPP_Base_packet):
  70.            
  71.             bun = oscparse.OSCBundle(oscparse.OSC_IMMEDIATELY, packet._get_elements())
  72.  
  73.             oscparse._encode_bundle(bun, raw_pkt, {})
  74.  
  75.         elif isinstance(packet, COPP_Message):
  76.             oscparse._encode_message(packet, raw_pkt, {})
  77.  
  78.         sock.sendto(raw_pkt, server_address)
  79.  
  80.  
  81. #    @engine.helper_method
  82. #    def _output(self, label, msg):
  83. #        """If you need to create subroutines, to call from the main datatarget_handlers
  84. #        they should be decorated as engine.helper_method"""
  85.  
  86. #        #Print the contents of the msg
  87. #        print(label, msg.payload, "with subtarget", msg.subtarget )
  88.  
  89.  
  90. # ======  End of CUSTOM NODE CLASS  =======
  91.  
  92. # the following lines tell blender to apply the modifications on the custom node
  93.  
  94. to_register = (CustomSettings, CustomNode)
  95.  
  96. if __name__ == '__main__':
  97.     from bpy.utils import register_class
  98.     for cls in to_register:
  99.         register_class(cls)