Python多线程在Blend中关闭线程

2024-10-03 15:30:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个小问题,在blender游戏引擎中有一个多线程的python脚本。 它工作正常,但当我停止游戏时,它会引发一些异常,有时会崩溃。在

from bge import logic
import time
from threading import Thread

def init():

    if not hasattr(logic, 'init'):
        logic.init = 0
        logic.thread = new()
        logic.thread.start()

    logic.thread.restart()

class new(Thread):
    def __init__(self):
        self.Thread = Thread
        self.Thread.__init__(self)
    def run(self):
        number = 0
        while 1:
            number += 1
            print(number)
            try:
                main()
                time.sleep(0.1)
            except:
                break

    def restart(self):
        self.Thread.__init__(self)

def main(): #this part isn't important now ...
    cam = bge.logic.getCurrentScene().active_camera
    obj = bge.logic.getCurrentController().owner
    obj.worldPosition.x = cam.worldPosition.x
    obj.worldPosition.y = cam.worldPosition.y

控制台写入:

^{pr2}$

如果有人能找出问题所在,我会很高兴的。 谢谢你


Tags: fromimportselfobj游戏numbertimeinit
1条回答
网友
1楼 · 发布于 2024-10-03 15:30:05

这是一个well-known limitation of Python scripting in Blender。在

问题是Blender在你的线程之前把Python撕碎了。你可以尝试做的是以某种方式注册Blender(或者你的游戏)正在退出,通知你的线程,然后从主线程^{}它。在

相关问题 更多 >