1
$\begingroup$

I'm trying doing bvh retargeting using blender script. After I delete bvh using bpy.ops.object.delete(), the bvh data is still in memory, but I have many bvh file to deal with. So I reopen blender file using bpy.ops.wm.open_mainfile(filepath=bpy.data.filepath), then some errors occur.

This is the framework of my code:

import bpy
import os

def import_bvh(src_path):
    bpy.ops.import_anim.bvh(filepath=src_path, axis_forward='Y', axis_up='Z')


for i in range(2):
    print("------iterating..", i)
    src_path = ["F:/01_01.bvh", "F:/01_02.bvh"]
    import_bvh(src_path[i])     
    print("import success")
    bpy.ops.object.select_all(action="DESELECT")
    print("do something with bvh")
    bpy.ops.object.select_all(action="SELECT")
    bpy.ops.object.delete()
    bpy.ops.wm.save_as_mainfile(filepath=bpy.data.filepath)
    bpy.ops.wm.open_mainfile(filepath=bpy.data.filepath)
    print("end")

And this is the console output, any suggestions would help:)

------iterating.. 0
        parsing bvh 'F:/01_01.bvh'...   zero length node found: Hips
0.1406
        importing to blender... zero length bone found: Spine1
        zero length bone found: LeftHand
        zero length bone found: RightHand
Done in 0.5342

import success
do something with bvh
read blend: F:\CAPG\mm_lzj\code\retarget_bvh\test_res\test_open_blend.blend
end
------iterating.. 1
        parsing bvh 'F:/01_02.bvh'...   zero length node found: Hips
0.1446
Traceback (most recent call last):
  File "E:\ProgramFiles\blender-2.78c-windows64\2.78\scripts\addons\io_anim_bvh\__init__.py", line 140, in execute
    return import_bvh.load(context, report=self.report, **keywords)
  File "E:\ProgramFiles\blender-2.78c-windows64\2.78\scripts\addons\io_anim_bvh\import_bvh.py", line 669, in load
    use_fps_scale=use_fps_scale,
  File "E:\ProgramFiles\blender-2.78c-windows64\2.78\scripts\addons\io_anim_bvh\import_bvh.py", line 394, in bvh_node_dict2armature
    bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
  File "E:\ProgramFiles\blender-2.78c-windows64\2.78\scripts\modules\bpy\ops.py", line 189, in __call__
    ret = op_call(self.idname_py(), None, kw)
RuntimeError: Operator bpy.ops.object.mode_set.poll() failed, context is incorrect
$\endgroup$
  • 1
    $\begingroup$ Due to saving and opening a file in your loop with operators, naffs the context for the next iteration. Seriously consider the need of saving and reopening a file in script like above. Can you undo easily? Could lead to a "whoops I've deleted everything and saved over the top" Saving a timestamped back up with save_as would be my recommendation. $\endgroup$ – batFINGER Nov 24 '17 at 12:30
  • $\begingroup$ I'm going to deal with a sequence of bvh files, that's why I use the loop to delete old bvh('01_01.bvh') and import new bvh('01_02.bvh'). So delete all old data is what I actually need, but I don't know how to delete the data while maintain the correct context. Maybe the question is not that clear. $\endgroup$ – Johnson Lai Nov 24 '17 at 12:43
  • $\begingroup$ The import creates a rig object, armature and action. Assuming you want to save only action, use bpy.data.objects.remove(obj) and bpy.data.armatures.remove(rig) where obj is context object and rig is context object data after import would suffice IMO. $\endgroup$ – batFINGER Nov 24 '17 at 14:11
  • $\begingroup$ Pure remove or delete method cannot remove data from memory. After I process 10 bvh files(10/ 2000), blender will crash because of Out-of-Memory.So I try to reopen blend file, but the context isn't correct(I'm new to blender, I don't understand why and not find a way by googling). Do you have any advice that I can process one file each time with no previous memory left in blender?Thanks:-) $\endgroup$ – Johnson Lai Nov 24 '17 at 16:42
  • $\begingroup$ How attached are you to your code above? I wouldn't do it that way, it's prob not the bvh data that's filling your memory and crashing blender, more likely the undo stack that (AFAIK still) keeps a "snapshot" of each operation. In old wip addon the importer was modified for multi file selection, to import a cmu chapter in one fell swoop. Even on my shitbox machine It didn't crash. $\endgroup$ – batFINGER Nov 24 '17 at 17:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.