I'm doing a project with python in blender and let me summarize its process.
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)
Analyze and append some data to a global variable '
list[]
'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, andpurge_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
- 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
- No 'For loop', iterate manually like
import(file_list[0])
import(file_list[0])
import(file_list[0])
... import gc
andgc.collect()
- modify api and make
dummy_view_layer_update()
to runview_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.
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$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$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$