【问题标题】:Serial signal from arduino to blender从 arduino 到搅拌机的串行信号
【发布时间】:2019-10-19 05:13:54
【问题描述】:

我有一个用搅拌机创建的动画(只是一个改变位置的对象),当我在 arduino 上按下一个按钮时,我想开始播放动画。我是 python 新手并试图理解它。

我与 arduino 建立了连接,它可以工作。

这里是python代码:

#part of the code was found from Olav3D tutorials on youtube"
import serial
import bpy

try:
    serial = serial.Serial("COM3", 9600, timeout = 10)
    print("Connection succeed")
except:
    print("Connection failed")

positions = (5,0,-2), (0,0,2), (0,0,5), (0,0,1), (0,0,7), (0,0,5)


ob = bpy.data.objects['Sphere']
frame_num = 0

x = serial.read(size=1)

for position in positions:
        bpy.context.scene.frame_set(frame_num)
        ob.location = position
        ob.keyframe_insert(data_path="location",index=-1)
        frame_num+=20
        print("Next position---")

当我点击“运行脚本”时,似乎一切正常,我可以看到连接和下一个位置消息出现,但我的 Sphere 没有移动。有人可以向我解释为什么球体没有移动,当我按下按钮时如何实现启动动画?我必须添加什么才能做到这一点?

【问题讨论】:

标签: python animation scripting arduino blender


【解决方案1】:

虽然脚本有效,但结果并不是您想要的。该脚本只是为球体创建了许多关键帧。虽然它读取(一次)串行输入,但它不会对此做任何事情,因此对按钮按下没有响应。

要响应来自 arduino 的输入,您需要不断读取串行输入并根据该输入选择一个动作。

理论上你想要的是这样的:

while True:
    x = serial.read(size=1)
    if x == 'p':
        # this will play/pause
        bpy.ops.screen.animation_play()

缺点是该循环将接管搅拌机并阻止任何窗口更新。为了解决这个问题,您可以将循环中的步骤放入模态运算符中,blender 将重复调用运算符中的模态方法并在每次调用之间更新屏幕。

从搅拌机中包含的modal operator template 开始,将模态方法更改为类似

def modal(self, context, event):
    x = serial.read(size=1)
    if x == 'p':
        # this will play/pause
        bpy.ops.screen.animation_play()
    if x == 's':
        bpy.ops.screen.animation_cancel()
    if event.type == 'ESC':
        # press esc to stop the operator
        return {'CANCELLED'}
    return {'RUNNING_MODAL'}

您可以在 blender.stackexchange 找到一些更完整的与 arduino 交互的示例

【讨论】:

    猜你喜欢
    • 2013-06-27
    • 2020-01-06
    • 2015-06-13
    • 1970-01-01
    • 2016-01-19
    • 2012-11-15
    • 1970-01-01
    • 2014-01-15
    • 2014-06-07
    相关资源
    最近更新 更多