0

Is it generally bad practice to load a blender file within a script? I'm running into lots of trouble when doing so. For example here I want to create fresh file, then import a fbx and then save it:

def create_empty_scene():
    bpy.ops.wm.open_mainfile(filepath=r"D:\empty.blend")

def import_fbx_and_save_as_blend(fbx_filepath): create_empty_scene() bpy.ops.import_scene.fbx(filepath=fbx_filepath, )

This gives me an error wihin the fbx import:

  File "C:\Program Files\Blender Foundation\Blender 3.1\3.1\scripts\addons\io_scene_fbx\import_fbx.py", line 2849, in _
    root_helper.build_hierarchy(fbx_tmpl, settings, scene, view_layer)
  File "C:\Program Files\Blender Foundation\Blender 3.1\3.1\scripts\addons\io_scene_fbx\import_fbx.py", line 2284, in build_hierarchy
    child.build_hierarchy(fbx_tmpl, settings, scene, view_layer)
  File "C:\Program Files\Blender Foundation\Blender 3.1\3.1\scripts\addons\io_scene_fbx\import_fbx.py", line 2244, in build_hierarchy
    bpy.ops.object.mode_set(mode='EDIT')
  File "C:\Program Files\Blender Foundation\Blender 3.1\3.1\scripts\modules\bpy\ops.py", line 132, in __call__
    ret = _op_call(self.idname_py(), None, kw)
RuntimeError: Operator bpy.ops.object.mode_set.poll() Context missing active object

Something even simpler does also not work:

 bpy.ops.wm.open_mainfile(filepath=r"D:\cube.blend")
 cube = bpy.context.scene.objects["Cube"]
 cube.select_set(True)
 print(bpy.context.selected_objects)

This results in an error regarding the context: AttributeError: 'Context' object has no attribute 'selected_objects'

all these scripts work completely fine when no scene is loaded before. I have the same happening when running this code via an operator or not.

In pipeline scripts I'm used to just load a file or refresh a scene etc. Is this a no-go in Blender or am I maybe missing something? I have noticed that appending files is working.

I'm fairly new to Blender scripting so excuse this trivial question.

Duarte Farrajota Ramos
  • 59,425
  • 39
  • 130
  • 187
  • Operators in bpy.ops are for interactive use through the UI and should be avoided in scripts because they depend on context. Using them through scripts may yield unexpected results at best, or fail entirely at worst. Manipulate data directly from bpy.data instead. – Duarte Farrajota Ramos Jun 01 '22 at 10:01
  • mh, so how would I import a fbx in some batch process? Is that a problem with the fbx importer in this case? – Sebastian Schoellhammer Jun 01 '22 at 10:12
  • I suggest adding a little timer right after loading the new file (0.1 second should do it but it depends on your rig), the context takes a bit of time to get initialized correctly. https://docs.blender.org/api/current/bpy.app.timers.html – Gorgious Jun 01 '22 at 10:26
  • thanks for the suggestion @Gorgious! That would mean running eveything in a thread though I suppose.. I really wish blender would simply not return from loading if the program is not ready, that would alleviate the need. – Sebastian Schoellhammer Jun 01 '22 at 11:27
  • I tried to package this functionality using threads but this is sadly crashing blender https://gist.github.com/sschoellhammer/b355b3313542eed69620d0549cd40fb0 I guess calling any ops like loading in a thread is a no go as well? – Sebastian Schoellhammer Jun 01 '22 at 12:45
  • What you're trying won't work because open_mainfile results in a fresh start, so your call to import_scene.fbx will never be invoked. – Marty Fouts Jun 01 '22 at 15:15
  • @MartyFouts mh well it seems to be invoked, hence the error.. but how does it usually work when you need to load a file then? What's best practice there? – Sebastian Schoellhammer Jun 01 '22 at 16:12
  • The usual case is to treat files like libraries and use the library interface to read from them, pulling out individual objects, or materials, or whatever. – Marty Fouts Jun 01 '22 at 16:15
  • thank you @MartyFouts, appending worked without problems indeed. I guess the missing piece of the puzzle for me now would be how to wipe a scene completely clean again. (just to make it work today, I turned to running blender headless and restarting for each file, not really ideal but at least I got something to batch) – Sebastian Schoellhammer Jun 01 '22 at 18:51

0 Answers0