Hi, sorry it took terribly long to respond and provide you with a test-case, but here is a little snippet that reproduces the error I am encountering. Just copy paste it and run it. When running, press 1 to see that flattening is okay in the main-thread. Close the program and run it again, but this time press 2 to see that flattening is not okay [the program freezes] in the sub-thread.
from direct.showbase.ShowBase import ShowBase
from direct.task import Task
from panda3d.core import *
from direct.gui.DirectGui import *
from direct.stdpy import threading2 as threading
from Queue import Queue
#thread class, where the flattening will be done:
class node_instance_creater(threading.Thread):
def __init__(self, queue):
threading.Thread.__init__(self)
self.queue = queue
def run(self):
Thread.considerYield()
while True:
dat=self.queue.get()
#flatten it:
print "COMMENCE FLATTENING: ",dat.getName()
dat.flattenStrong()
print "YOU WON'T SEE ME CAUSE THE PROGRAM HANGS :("
self.queue.task_done()
#main class:
class freezer(ShowBase):
def __init__(self):
ShowBase.__init__(self)
#administrative stuff...
array = GeomVertexArrayFormat()
array.addColumn(InternalName.make('vertex'), 3,Geom.NTFloat32, Geom.CPoint)
array.addColumn(InternalName.make('texcoord'), 2,Geom.NTFloat32, Geom.CTexcoord)
array.addColumn(InternalName.make('normal'), 3,Geom.NTFloat32, Geom.CNormal)
array.addColumn(InternalName.make('color'), 4,Geom.NTFloat32, Geom.CColor)
format = GeomVertexFormat()
format.addArray(array)
format = GeomVertexFormat.registerFormat(format)
self.vdata = GeomVertexData('name', format, Geom.UHStatic)
self.vertex = GeomVertexWriter(self.vdata, 'vertex')
self.normal = GeomVertexWriter(self.vdata, 'normal')
self.color = GeomVertexWriter(self.vdata, 'color')
self.texcoord = GeomVertexWriter(self.vdata, 'texcoord')
self.geom = Geom(self.vdata)
#model vertex-data:
vertex_data=[LPoint3f(12, 0, -40), LPoint3f(20, 0, -40), LPoint3f(12, 0, -36),
LPoint3f(20, 0, -36), LPoint3f(12, 24, -40), LPoint3f(20, 24, -40), LPoint3f(12, 24, -36), LPoint3f(20, 24, -36), LPoint3f(-20, 0, -32), LPoint3f(-16, 0, -32),
LPoint3f(-20, 0, -28), LPoint3f(-16, 0, -28), LPoint3f(-20, 24, -32), LPoint3f(-16, 24, -32), LPoint3f(-20, 24, -28), LPoint3f(-16, 24, -28), LPoint3f(16, 0, -24), LPoint3f(20, 0, -24), LPoint3f(16, 0, -20), LPoint3f(20, 0, -20), LPoint3f(16, 24, -24), LPoint3f(20, 24, -24), LPoint3f(16, 24, -20), LPoint3f(20, 24, -20), LPoint3f(-16, 0, -20), LPoint3f(4, 0, -20), LPoint3f(-16, 0, -16), LPoint3f(4, 0, -16), LPoint3f(-16, 24, -20), LPoint3f(4, 24, -20), LPoint3f(-16, 24, -16),
LPoint3f(4, 24, -16), LPoint3f(-16, 0, -12), LPoint3f(-12, 0, -12), LPoint3f(-16, 0, -8), LPoint3f(-12, 0, -8), LPoint3f(-16, 24, -12), LPoint3f(-12, 24, -12),
LPoint3f(-16, 24, -8), LPoint3f(-12, 24, -8), LPoint3f(-32, 0, -8), LPoint3f(-28, 0, -8), LPoint3f(-32, 0, -4), LPoint3f(-28, 0, -4), LPoint3f(-32, 24, -8), LPoint3f(-28, 24, -8), LPoint3f(-32, 24, -4), LPoint3f(-28, 24, -4), LPoint3f(-8,0, 8), LPoint3f(0, 0, 8), LPoint3f(-8, 0, 12), LPoint3f(0, 0, 12), LPoint3f(-8,24, 8), LPoint3f(0, 24, 8), LPoint3f(-8, 24, 12), LPoint3f(0, 24, 12),LPoint3f(-28, 0, -24), LPoint3f(-24, 0, -24), LPoint3f(-28, 0, -16),
LPoint3f(-24, 0, -16), LPoint3f(-28, 24, -24), LPoint3f(-24, 24, -24), LPoint3f(-28, 24, -16), LPoint3f(-24, 24, -16), LPoint3f(-28, 0, -12), LPoint3f(-24, 0,-12), LPoint3f(-28, 0, 0), LPoint3f(-24, 0, 0), LPoint3f(-28, 24, -12), LPoint3f(-24, 24, -12), LPoint3f(-28, 24, 0), LPoint3f(-24, 24, 0), LPoint3f(-24, 0, -32), LPoint3f(-20, 0, -32), LPoint3f(-24, 0, -8), LPoint3f(-20, 0, -8), LPoint3f(-24, 24, -32), LPoint3f(-20, 24, -32), LPoint3f(-24, 24, -8), LPoint3f(-20, 24, -8), LPoint3f(-24, 0, -4), LPoint3f(-20, 0, -4), LPoint3f(-24, 0, 4), LPoint3f(-20, 0, 4), LPoint3f(-24, 24, -4), LPoint3f(-20, 24, -4), LPoint3f(-24, 24, 4), LPoint3f(-20, 24, 4), LPoint3f(-20, 0, -20), LPoint3f(-16, 0, -20), LPoint3f(-20,0, -8), LPoint3f(-16, 0, -8), LPoint3f(-20, 24, -20), LPoint3f(-16, 24, -20), LPoint3f(-20, 24, -8), LPoint3f(-16, 24, -8), LPoint3f(-20, 0, 0), LPoint3f(-16, 0, 0), LPoint3f(-20, 0, 20), LPoint3f(-16, 0, 20), LPoint3f(-20, 24, 0), LPoint3f(-16, 24, 0), LPoint3f(-20, 24, 20), LPoint3f(-16, 24, 20), LPoint3f(-16, 0, 4),
LPoint3f(-12, 0, 4), LPoint3f(-16, 0, 12), LPoint3f(-12, 0, 12), LPoint3f(-16,24, 4), LPoint3f(-12, 24, 4), LPoint3f(-16, 24, 12), LPoint3f(-12, 24, 12), LPoint3f(-16, 0, 16), LPoint3f(-12, 0, 16), LPoint3f(-16, 0, 24), LPoint3f(-12, 0, 24), LPoint3f(-16, 24, 16), LPoint3f(-12, 24, 16), LPoint3f(-16, 24, 24), LPoint3f(-12, 24, 24), LPoint3f(-12, 0, -12), LPoint3f(-8, 0, -12), LPoint3f(-12, 0, 12), LPoint3f(-8, 0, 12), LPoint3f(-12, 24, -12), LPoint3f(-8, 24, -12), LPoint3f(-12, 24, 12), LPoint3f(-8, 24, 12), LPoint3f(-12, 0, 20), LPoint3f(-8, 0, 20), LPoint3f(-12, 0, 28), LPoint3f(-8, 0, 28), LPoint3f(-12, 24, 20), LPoint3f(-8, 24, 20), LPoint3f(-12, 24, 28), LPoint3f(-8, 24, 28), LPoint3f(-8, 0, 24), LPoint3f(-4, 0, 24), LPoint3f(-8, 0, 44), LPoint3f(-4, 0, 44), LPoint3f(-8, 24, 24), LPoint3f(-4, 24, 24), LPoint3f(-8, 24, 44), LPoint3f(-4, 24, 44), LPoint3f(-4, 0,36), LPoint3f(0, 0, 36), LPoint3f(-4, 0, 44), LPoint3f(0, 0, 44), LPoint3f(-4, 24, 36), LPoint3f(0, 24, 36), LPoint3f(-4, 24, 44), LPoint3f(0, 24, 44), LPoint3f(0, 0, 8), LPoint3f(4, 0, 8), LPoint3f(0, 0, 24), LPoint3f(4, 0, 24), LPoint3f(0, 24, 8), LPoint3f(4, 24, 8), LPoint3f(0, 24, 24), LPoint3f(4, 24, 24), LPoint3f(0, 0, 32), LPoint3f(4, 0, 32), LPoint3f(0, 0, 40), LPoint3f(4, 0, 40), LPoint3f(0, 24, 32), LPoint3f(4, 24, 32), LPoint3f(0, 24, 40), LPoint3f(4, 24, 40), LPoint3f(4, 0, -28), LPoint3f(8, 0, -28), LPoint3f(4, 0, -12), LPoint3f(8, 0, -12),
LPoint3f(4, 24, -28), LPoint3f(8, 24, -28), LPoint3f(4, 24, -12), LPoint3f(8, 24, -12), LPoint3f(4, 0, 4), LPoint3f(8, 0, 4), LPoint3f(4, 0, 24), LPoint3f(8, 0,24), LPoint3f(4, 24, 4), LPoint3f(8, 24, 4), LPoint3f(4, 24, 24), LPoint3f(8, 24, 24), LPoint3f(4, 0, 28), LPoint3f(8, 0, 28), LPoint3f(4, 0, 36), LPoint3f(8,0, 36), LPoint3f(4, 24, 28), LPoint3f(8, 24, 28), LPoint3f(4, 24, 36), LPoint3f(8, 24, 36), LPoint3f(8, 0, -40), LPoint3f(12, 0, -40), LPoint3f(8, 0, -16), LPoint3f(12, 0, -16), LPoint3f(8, 24, -40), LPoint3f(12, 24, -40), LPoint3f(8, 24, -16), LPoint3f(12, 24, -16), LPoint3f(8, 0, 0), LPoint3f(12, 0, 0), LPoint3f(8, 0, 8), LPoint3f(12, 0, 8), LPoint3f(8, 24, 0), LPoint3f(12, 24, 0), LPoint3f(8, 24, 8), LPoint3f(12, 24, 8), LPoint3f(8, 0, 12), LPoint3f(12, 0, 12), LPoint3f(8,0, 32), LPoint3f(12, 0, 32), LPoint3f(8, 24, 12), LPoint3f(12, 24, 12), LPoint3f(8, 24, 32), LPoint3f(12, 24, 32), LPoint3f(12, 0, -28), LPoint3f(16, 0, -28),
LPoint3f(12, 0, 4), LPoint3f(16, 0, 4), LPoint3f(12, 24, -28), LPoint3f(16, 24,-28), LPoint3f(12, 24, 4), LPoint3f(16, 24, 4), LPoint3f(12, 0, 12), LPoint3f(16, 0, 12), LPoint3f(12, 0, 20), LPoint3f(16, 0, 20), LPoint3f(12, 24, 12), LPoint3f(16, 24, 12), LPoint3f(12, 24, 20), LPoint3f(16, 24, 20), LPoint3f(16, 0, -8),
LPoint3f(20, 0, -8), LPoint3f(16, 0, 0), LPoint3f(20, 0, 0), LPoint3f(16, 24, -8), LPoint3f(20, 24, -8), LPoint3f(16, 24, 0), LPoint3f(20, 24, 0), LPoint3f(20,0, -40), LPoint3f(24, 0, -40), LPoint3f(20, 0, -20), LPoint3f(24, 0, -20), LPoint3f(20, 24, -40), LPoint3f(24, 24, -40), LPoint3f(20, 24, -20), LPoint3f(24, 24, -20)]
#screenText:
Instructions = OnscreenText(text="Press 1 to flatten in main thread.\n\nPress 2 to flatten in other thread.",
style=1, fg=(0, 0, 0, 1), pos=(0.06, -0.08),
align=TextNode.ALeft, scale=.05,
parent=base.a2dTopLeft)
#some light:
dlight = DirectionalLight('dlight')
lens=PerspectiveLens()
dlight.setLens(lens)
dlight.setColor(VBase4(0.4,0.2,0.1, 1))
dlnp = render.attachNewNode(dlight)
dlnp.setHpr(0, -60, 0)
render.setLight(dlnp)
alight = AmbientLight('alight')
alight.setColor(VBase4(0.15,0.15,0.15, 1))
alnp = render.attachNewNode(alight)
render.setLight(alnp)
pntLight = PointLight("point")
pntLight.setColor(Vec4(0.4,0.2,0.1, 1.0))
pntNode = render.attachNewNode(pntLight)
render.setLight(pntNode)
#move the camera back a bit:
base.cam.setPos(0,-200,0)
#generate the data:
counter=0
global instance_queue
instance_queue=Queue(maxsize=0)
for v_p in range(0,len(vertex_data),8):
prim = GeomTriangles(Geom.UHStatic)
#the top and bottom:
self.draw_faces(prim,[vertex_data[v_p+2],vertex_data[v_p+3],vertex_data[v_p+6],vertex_data[v_p+7]],counter)
self.draw_faces(prim,[vertex_data[v_p],vertex_data[v_p+1],vertex_data[v_p+4],vertex_data[v_p+5]],counter+4)
#the back and front:
self.draw_faces(prim,[vertex_data[v_p],vertex_data[v_p+1],vertex_data[v_p+2],vertex_data[v_p+3]],counter+8)
self.draw_faces(prim,[vertex_data[v_p+4],vertex_data[v_p+5],vertex_data[v_p+6],vertex_data[v_p+7]],counter+12)
#the left and right:
self.draw_faces(prim,[vertex_data[v_p+4],vertex_data[v_p],vertex_data[v_p+6],vertex_data[v_p+2]],counter+16)
self.draw_faces(prim,[vertex_data[v_p+5],vertex_data[v_p+1],vertex_data[v_p+7],vertex_data[v_p+3]],counter+20)
self.geom.addPrimitive(prim)
counter+=24
node = GeomNode("Wild GeoDude Appeared!")
node.addGeom(self.geom)
self.nodePathaqq = render.attachNewNode(node)
self.nodePathaqq.setTransparency(TransparencyAttrib.MAlpha,1)
self.nodePathaqq.setTwoSided(True)
#start the other thread:
self.thread_starter_inst()
#the accepts:
self.accept('1',self.flattenModel,[0])
self.accept('2',self.flattenModel,[1])
def myNormalize(self,myVec):
myVec.normalize()
return myVec
def draw_faces(self,prim_dat,array_sent,numbr):
#1.vertex:
self.vertex.addData3f(array_sent[0].x,array_sent[0].y, array_sent[0].z)
self.normal.addData3f(self.myNormalize(Vec3(2*array_sent[0].x-1,2*array_sent[0].y-1,2*array_sent[0].z-1)))
self.color.addData4f(1, 1, 1, 1)
self.texcoord.addData2f(1, 0)
#2.vertex:
self.vertex.addData3f(array_sent[1].x,array_sent[1].y, array_sent[1].z)
self.normal.addData3f(self.myNormalize(Vec3(2*array_sent[1].x-1,2*array_sent[1].y-1,2*array_sent[1].z-1)))
self.color.addData4f(1, 1, 1, 1)
self.texcoord.addData2f(1, 1)
#3.vertex:
self.vertex.addData3f(array_sent[2].x,array_sent[2].y, array_sent[2].z)
self.normal.addData3f(self.myNormalize(Vec3(2*array_sent[2].x-1,2*array_sent[2].y-1,2*array_sent[2].z-1)))
self.color.addData4f(1, 1, 1, 1)
self.texcoord.addData2f(0, 1)
#4.vertex:
self.vertex.addData3f(array_sent[3].x,array_sent[3].y, array_sent[3].z)
self.normal.addData3f(self.myNormalize(Vec3(2*array_sent[3].x-1,2*array_sent[3].y-1,2*array_sent[3].z-1)))
self.color.addData4f(1, 1, 1, 1)
self.texcoord.addData2f(0, 0)
#add to primitive:
one=numbr
two=numbr+1
three=numbr+2
four=numbr+3
prim_dat.addVertices(one, two, three)
prim_dat.addVertices(two, three, four)
#this method starts the other thread:
def thread_starter_inst(self):
global instance_queue
self.thread_s=node_instance_creater(instance_queue)
self.thread_s.setDaemon(True)
self.thread_s.start()
#this method does the flattening, either in the main
#thread, or the other thread.
def flattenModel(self,Fmode):
if(Fmode==0):
#flatten in the main thread, so just do it!!:
print "FLATTENING IN MAIN THREAD: "
self.nodePathaqq.flattenStrong()
print "MODEL FLATTENED!"
elif(Fmode==1):
#flatten in the other thread, so add it to the queue:
global instance_queue
instance_queue.put(self.nodePathaqq)
instance_queue.join()
runner=freezer()
runner.run()
If indentation is a bother, then I also attached a copy of the code with this post. I doubt that it’s my code causing the freeze, but if it is, please point it out.
Thanks.
freeze_test_case.zip (2.77 KB)