Page

To install the pyserial package using Blender's bundled Python, follow these steps:

  1. Ensure pip is installed:

    /Applications/Blender.app/Contents/Resources/3.6/python/bin/python3.10 -m ensurepip
  2. Install pyserial using pip:

    /Applications/Blender.app/Contents/Resources/3.6/python/bin/python3.10 -m pip install pyserial
import serial
import bpy
import time

ser = serial.Serial('/dev/cu.usbmodem11201', 115200)
time.sleep(3)

ser.write(b'Get Distance\n')
start = float(ser.readline().decode().strip())

cube = bpy.context.selected_objects[0]

for x in range(100):
    ser.write(b'Get Distance\n')
    time.sleep(0.05)
    current = float(ser.readline().decode().strip())
    print(current)
    cube.location.z = start - current
    bpy.ops.wm.redraw_timer(type='DRAW_WIN_SWAP', iterations=1)

ser.close()

Last updated