0
$\begingroup$

I'm doing a project with python in blender and let me summarize its process.

  1. Import a .pmx file(kind of 3d model)

        start = time.time()
        bpy.ops.mmd_tools.import_model(
            filepath=str(model_path), 
            files=[{
                "name":os.path.basename(model_path), 
                "name":os.path.basename(model_path)
            }], 
            directory=os.path.dirname(model_path), 
            types={'MESH', 'ARMATURE', 'MORPHS'}, 
            scale=0.08, 
            log_level='WARNING')
        #This import_model function is from addon.
        print("import time :", time.time() - start)
    
  2. Analyze and append some data to a global variable 'list[]'

  3. Delete every data(Object, Meshes, Materials, Textures, images, texts and collections), and even Purge_all.

    def clean_blender():
    
    for dataset in [bpy.data.objects, bpy.data.meshes,
                    bpy.data.materials, bpy.data.textures, bpy.data.images, bpy.data.texts,
                    bpy.data.collections]:
        for key, value in dataset.items():
            if key != 'camera' and key != 'light' and value.name != 'Text':
                dataset.remove(value, do_unlink = True)
    

    This clean_blender() deletes every data from my scene, and purge_all() delete all orphan datas.

    def purge_all():
        area = bpy.context.area
        old_type = area.type
        area.type = 'OUTLINER'
        bpy.context.space_data.display_mode = 'ORPHAN_DATA'
        bpy.ops.outliner.orphans_purge()
        area.type = old_type
    
    1. Iterate 1 ~ 3 for every 3d models.

But I figured out that each time for "importing" model gets longer, and I checked it via time.time() by iterating just only one file in While 1:. And the time spent on importing(1 Loop) got longer overtime. I've tried many other options

  1. No 'For loop', iterate manually like import(file_list[0]) import(file_list[0]) import(file_list[0])...
  2. import gc and gc.collect()
  3. modify api and make dummy_view_layer_update() to run view_layer_update() only at the end of my script

However It was all same. But the weird thing is that after I exit my script and get back to blender, everything is deleted successfully. And when re - run script, then the speed gets back as fast as the first time I ran my program and gets slower again.

So my conclusion is that Process 3 - 'Delete every data' works fine after the script exits, but not properly during running(Maybe because blender is blocked while running script?) . It is just a guess, and I can't even figure out what should I do.

Or is it just an innate problem of blender python?

Thanks.

$\endgroup$
4
  • $\begingroup$ Could try seeing what's at the top of the profile, here's the boilerplate I use. $\endgroup$
    – scurest
    Dec 10, 2021 at 4:02
  • $\begingroup$ If all else fails, you could also try read_homefile to get a new file after each import. You'd probably have to move some of your code to run in a handler that runs after the new file is loaded. $\endgroup$
    – scurest
    Dec 10, 2021 at 4:05
  • $\begingroup$ @scurest Thanks for your idea, but it seems no problem in profiling. And when I read_homefile, it resets everything including my script, and it stops. Because I have to iterate about 5000 files, I think it isn't a proper solution. $\endgroup$
    – user136892
    Dec 10, 2021 at 4:29
  • $\begingroup$ You can try using batch_remove if you have a lot of objects, might speed things a bit. docs.blender.org/api/current/… It take a list of IDs as input Also you might want to explore application timers to delay the execution of next loop for a bit, maybe there is a thread that never has time to finish when deleting things. idk :) $\endgroup$
    – Gorgious
    Dec 10, 2021 at 8:32

0

You must log in to answer this question.

Browse other questions tagged .