0

I'm working on a highly detailed "random building generator". It's working so far, with a variable to pick the # of floors. 10 floors takes about 10 seconds 20 floors takes about 70 seconds 100 floors... after 5 minutes, I closed it out.

The thing I noticed is it's only using one thread. Each floor can be created with it's own thread without interfering with other threads. Within the loop that'll execute 100 times, how do I put "buildLayout" function call into threads, without breaking my PC?

Would this work? (Don't want to generate more threads than my PC can actually handle) import threading for floor in range (0, 100): thread = threading.Thread(target=buildLayout(self, context, offset, outsideWallObj, outsideDoorObj, outsideWindowObj)) threads.append(thread) thread.start()

for thread in threads: thread.join()

My biggest concern is I only want 1 thread running per core. Does python handle this automagically, or do I need to find the core count? If I need core count, how do I obtain it?

Current code listed below.

            for floor in range (0, bpy.context.scene.user_vars.buildingHeight):
                xPos = 0
                yPos = 0
                zPos = floor*4-1 

                offset = (xPos, yPos, zPos)
                offset = Vector(offset)
                buildLayout(self, context, offset, outsideWallObj, outsideDoorObj, outsideWindowObj)
Mike
  • 420
  • 2
  • 11
  • Threading is probably not the answer. See https://blender.stackexchange.com/questions/7358/python-performance-with-blender-operators chances are you code is inefficient and over-reliant on operators. – batFINGER Dec 14 '19 at 08:28
  • It's just one that's slowing the code down: "bpy.ops.object.modifier_apply(modifier='bool 1')" How do I accomplish this without an "operator"? I have to apply the bool modifier to the wall, to add a window. But I need to delete the original window, or I have thousands of invisible meshes laying around, taking up memory. And regardless, I still want to do multi-threading. – Mike Dec 14 '19 at 09:42
  • Unfortunately, that answer you linked doesn't answer my question. My question specifically: If I loop 100 times, with each one creating a thread, will it automatically wait until a core is available to create a new thread? – Mike Dec 14 '19 at 10:20
  • That is off topic too, more python than blender related A question for stackoverflow for example. – batFINGER Dec 14 '19 at 10:32
  • I tried the following, no errors, but it does not use multiple threads for some reason. Still only uses a max of about 5% of my CPU and RAM.

    import threading threads = [] for floor in range (0, vars.buildingHeight): thread = threading.Thread(target=buildLayout(varsY)) threads.append(thread) thread.start()

                    #buildLayout(self, context, offset, outsideWallObj, outsideDoorObj, outsideWindowObj)
                    bpy.ops.object.select_all(action='DESELECT')
                for thread in threads: thread.join()
    
    – Mike Dec 14 '19 at 10:35
  • Interesting: bpy.ops.object.modifier_apply(modifier='bool 1') absolutely will not function within a thread, even if it's the only thread running. If two threads are running, blender crashes. Thanks for the assist again. – Mike Dec 14 '19 at 11:06
  • Another interesting tidbit specific to this situation: From researching, bpy.ops may iterate over every object in the scene. Since I had thousands of objects in larger buildings, this caused extreme slowdowns. So, as each item was added to the scene, I used the object join with the floor. This left 1 object per floor. This cut spawn time on 20 story building from 70s to 10s, even though more work is being done. – Mike Dec 14 '19 at 12:03
  • Thank you so much BatFinger! With all your help, I now can generate a random 100 story building in under 4 minutes. :D – Mike Dec 14 '19 at 12:27

0 Answers0