Search code examples
pythonmultithreadingcrashpanda3d

Panda3D Python threading crashes


Overview: I've created a small Minecraft'like generation script with Python and Panda3D, I've put it on a thread so I can use Time.Sleep() and other functions but any ideas why the thread crashes?

What I've Done: I've created a small Minecraft'like generation script with Python and Panda3D.

The Problem: I can't use threading with Panda3D.

Here is all the code I've created:

from direct.showbase.ShowBase import ShowBase
from panda3d.core import *
import perlin, colorsys, time
import thread

scaleX = 0.05
scaleZ = 0.05
blockSize = 1
size = 16
bottom = -70



waiter = 0.001

sn = perlin.SimplexNoise()

def threaded_generator():
    for x in range(size):
            for z in range(size):
                    time.sleep(waiter)
                    y = sn.noise2(x*scaleX, z*scaleZ)
                    # Load the environment model.
                    cube = loader.loadModel("Test")
                    # Reparent the model to render.
                    cube.reparentTo(render)
                    # Apply scale and position transforms on the model.
                    cube.setScale(blockSize, blockSize, blockSize)
                    cube.setPos(x*blockSize, z*blockSize, round(y))
                    cy = round(y)
                    while cy > bottom:
                        cy -= 1
                        cube = loader.loadModel("Test")
                        # Reparent the model to render.
                        cube.reparentTo(render)
                        # Apply scale and position transforms on the model.
                        cube.setScale(blockSize, blockSize, blockSize)
                        cube.setPos(x*blockSize, z*blockSize, round(cy))


class MyApp(ShowBase):

    def __init__(self):
        ShowBase.__init__(self)
        #Ambient
        alight = AmbientLight('alight')
        alight.setColor(VBase4(0.2, 0.2, 0.2, 1))
        alnp = render.attachNewNode(alight)
        render.setLight(alnp)
        #Directional
        dlight = DirectionalLight('dlight')
        dlight.setColor(VBase4(0.8, 0.8, 0.5, 1))
        dlnp = render.attachNewNode(dlight)
        dlnp.setHpr(0, -60, 0)
        render.setLight(dlnp)
        #Cubes 
        print("Ding!")
        thread.start_new_thread(threaded_generator, ())

app = MyApp()
app.run()

For me it starts generating for a bit,

then it stops, it doesn't seem to finish, sometimes it stops fairly quickly and sometimes it stops after a few seconds.

~Coolq :)


Solution

  • From the Panda3D manual you have to use the threads provided from Panda3D, beacause of the underlying c++ structure.

    Check for threads:

    from panda3d.core import Thread
    print Thread.isThreadingSupported()
    

    Use of thread

    # WRONG:
    import thread
    # RIGHT:
    from direct.stdpy import thread
    

    Use of threading

    # WRONG:
    import threading
    # RIGHT:
    from direct.stdpy import threading
    # ALSO RIGHT:
    from direct.stdpy import threading2 as threading