processed parallel loading models - called from a directory


#parallel loading models
#dirk hochegger 2008
import direct.directbase.DirectStart
from direct.showbase import DirectObject
from threading import Thread
from direct.showbase.Loader import Loader
import os
from os.path import join, getsize
from pandac.PandaModules import Point3



class callingTHREAD (Thread):
    def __init__(self):
        Thread.__init__(self)
        print "im calling myself before i die"
        
        
        dir = str("modelCONTAINER/")
        
        files = []
        countLIST = []
        global inputLIST
        inputLIST = []
        
        for root, dirs, files in os.walk('modelCONTAINER'):
            print "files in directory","../",dir,"->", len(files)
            XX = len(files)
            files = os.listdir('modelCONTAINER') 
          
        
        d = -1
        for x in range(XX):
            d += 1 
            fileOPEN = dir + files [d]
            file1=open(fileOPEN)
            zeilen=file1.read()
            c = len(zeilen)
            #print "count file1 ->",c
            print "FILE ->",files [d],"ENTRY ->", d,"COUNT ->",c
            file1.close
            countLIST.append(c)
            
        countLIST.sort()
        countLIST.reverse()
        
        dx = -1
        for x in range(XX):
            dx += 1
            fileOPEN = dir + files [dx] 
            test=open(fileOPEN)
            input = test.read()
            inputLIST.append(input)
        


        obj1 = [None for drh in range(XX)]
        ml = -1 
        for x in range(XX):
            ml += 1
            file = files[ml]
            print file
            fileNAME = dir + file
            print fileNAME
            
            obj1[drh] = loader.loadModel(fileNAME)
            obj1[drh].reparentTo(render)
            obj1[drh].setPos(-3+ml,20+ml,-3+ml)
     
d = callingTHREAD()
run()


greetz
dirk

Note that loading models in separate threads is very risky and likely to crash unless Panda is specifically compiled to be thread-safe.

It is better to use loader.loadModel(filename, callback = myFunc), which will load the model in a sub-thread and then call the python function myFunc() when it is done, if Panda is compiled to be thread-safe. If Panda is not compiled to be thread-safe, then it will load the model inline and call myFunc() immediately.

David

is it ??? :smiley:

but i love to observe that my models are quicker loaded than panda global watch is set by text :stuck_out_tongue: so use my code by your own risk!

but if you are right i just can hope that they will do in the next build more for parallel processing !!! :slight_smile:

most peoples have now multi cores (ok not me :frowning: but soon i hope) it would be a shame if panda would have problems with that.

Panda does support multi-threading, but it has to be enabled at the compilation step, because if you enable it and don’t need it, it slows everything down a little bit. This is true for just about any software, and is the reason why (for instance) the Linux kernel is always available in SMP and UP versions.

We don’t provide a SMP version of Panda by default, but you’re welcome to get the source and build it yourself. Eventually, we will probably offer this by default. In the meantime, there’s not enough of a win to justify it.

We plan to be adding a lot of code in the future to take heavy advantage of multicore CPU’s, but for now all we have is just the minimum necessary to support things like you are trying to do.

David

thanks for your serious statement.