A script for exporting models - context.scene empty after open_mainfile

Hi all,
Pulling my hair out here :(. I’m trying to write a script to loop over a load of models and export each one (in Ogre XML format). The trouble is that after opening a file, bpy.context.scene is empty, and so the Ogre exporter crashes.

I know that bpy.context should be avoided in Python scripts, but is there any way around this? Here’s the script:

for i in range(1,num_objects+1):  
    # Open the file - works fine
    bpy.ops.wm.open_mainfile(filepath=filenames[i] + ".blend");
    # Export it - crashes because bpy.context.scene is empty
    bpy.ops.ogre.export(...);

Is there a way to populate bpy.context.scene after loading the model? Or avoid needing to?

I’m using Blender 2.61, and here’s the error from blender2ogre in case it helps:

File "C:\Users\me\AppData\Roaming\Blender Foundation\Blender\2.61\script
s\addons\io_export_ogreDotScene.py", line 2661, in ogre_export
    for ob in bpy.context.scene.objects:
AttributeError: 'NoneType' object has no attribute 'objects'

Much appreciated,
Peter.

Can’t you loop over the objects in bpy.data.objects?

import bpy

for ob in bpy.data.objects:
    print(ob.name)

Thanks ThomasL - yes I can, but I’m not sure how this helps? To clarify - I have hundreds of separate .blend files, I need to open and export each one in turn.

Sorry if this is an obvious question, I’m quite new to blender scripting!

Thanks,
Peter.

If you are always dealing with the current scene or the first scene, you can make an assumption and access scene #0 directly.

Try replacing bpy.context.scene with bpy.data.scenes[0].

Thanks Atom,
I did try this, but it gives the error:

AttributeError: bpy_struct: attribute "scene" from "Context" is read-only

Any other suggestions?

Many thanks.

Sorry, I was being dense. Trying what you suggested properly now…

Great! That now seems to have worked, although I’m just getting lots of warnings, which are making me a little concerned for the accuracy of the saved models. It would be nice to clear them up if possible - would you mind telling me if there’s an easy solution to any of them?

Firstly it is creating a .mesh.xml file, which looks fine, but strangely I’m getting the error:

[Ogre Tools Wrapper] D:\myfolder\1.mesh.xml
WARNING: can not find OgreXMLConverter (can not convert XXX.mesh.xml to XXX.mesh

I’m also getting a couple of these:

AttributeError: ‘Context’ object has no attribute ‘active_object’

And a handful of these:

RuntimeError: Operator bpy.ops.wm.call_menu.poll() failed, context is incorrect

Complete trace below.

Thanks again,
Peter.


Traceback (most recent call last):
  File "C:\Users\me\AppData\Roaming\Blender Foundation\Blender\2.61\script
s\addons\io_export_ogreDotScene.py", line 2543, in poll
    if context.active_object and context.mode != 'EDIT_MESH': return True
AttributeError: 'Context' object has no attribute 'active_object'

location:C:\blender-2.61-release-windows64\2.61\scripts\modules\bpy\ops.py:180

location:C:\blender-2.61-release-windows64\2.61\scripts\modules\bpy\ops.py:180
Traceback (most recent call last):
  File "C:\Users\me\AppData\Roaming\Blender Foundation\Blender\2.61\script
s\addons\io_export_ogreDotScene.py", line 2543, in poll
    if context.active_object and context.mode != 'EDIT_MESH': return True
AttributeError: 'Context' object has no attribute 'active_object'

location:C:\blender-2.61-release-windows64\2.61\scripts\modules\bpy\ops.py:180

location:C:\blender-2.61-release-windows64\2.61\scripts\modules\bpy\ops.py:180
ogre export-> D:\models\1.scene
saved D:\models\1.material
--------------- exporting root -> <bpy_struct, Object("1")>
mesh to Ogre mesh XML format 1
creating document...
    writing shared geometry
 time:  0.14399981498718262
    writing submeshes
[Ogre Tools Wrapper] D:\models\1.mesh.xml
WARNING: can not find OgreXMLConverter (can not convert XXX.mesh.xml to XXX.mesh

********************************************************************************

TIME:  0.2089998722076416
ogre scene dumped D:\models\1.scene
Traceback (most recent call last):
  File "C:\Users\me\AppData\Roaming\Blender Foundation\Blender\2.61\script
s\addons\io_export_ogreDotScene.py", line 2549, in execute
    def execute(self, context): self.ogre_export(  self.filepath, context ); ret
urn {'FINISHED'}
  File "C:\Users\me\AppData\Roaming\Blender Foundation\Blender\2.61\script
s\addons\io_export_ogreDotScene.py", line 2812, in ogre_export
    bpy.ops.wm.call_menu( name='MiniReport' )
  File "C:\blender-2.61-release-windows64\2.61\scripts\modules\bpy\ops.py", line
 180, in __call__
    ret = op_call(self.idname_py(), None, kw)
RuntimeError: Operator bpy.ops.wm.call_menu.poll() failed, context is incorrect

location:C:\blender-2.61-release-windows64\2.61\scripts\modules\bpy\ops.py:180

location:C:\blender-2.61-release-windows64\2.61\scripts\modules\bpy\ops.py:180
Error: Traceback (most recent call last):
  File "C:\Users\me\AppData\Roaming\Blender Foundation\Blender\2.61\script
s\addons\io_export_ogreDotScene.py", line 2549, in execute
    def execute(self, context): self.ogre_export(  self.filepath, context ); ret
urn {'FINISHED'}
  File "C:\Users\me\AppData\Roaming\Blender Foundation\Blender\2.61\script
s\addons\io_export_ogreDotScene.py", line 2812, in ogre_export
    bpy.ops.wm.call_menu( name='MiniReport' )
  File "C:\blender-2.61-release-windows64\2.61\scripts\modules\bpy\ops.py", line
 180, in __call__
    ret = op_call(self.idname_py(), None, kw)
RuntimeError: Operator bpy.ops.wm.call_menu.poll() failed, context is incorrect

location:C:\blender-2.61-release-windows64\2.61\scripts\modules\bpy\ops.py:180


Traceback (most recent call last):
  File "\preprocess.py", line 46, in <module>
  File "C:\blender-2.61-release-windows64\2.61\scripts\modules\bpy\ops.py", line
 180, in __call__
    ret = op_call(self.idname_py(), None, kw)
RuntimeError: Error: Traceback (most recent call last):
  File "C:\Users\me\AppData\Roaming\Blender Foundation\Blender\2.61\script
s\addons\io_export_ogreDotScene.py", line 2549, in execute
    def execute(self, context): self.ogre_export(  self.filepath, context ); ret
urn {'FINISHED'}
  File "C:\Users\me\AppData\Roaming\Blender Foundation\Blender\2.61\script
s\addons\io_export_ogreDotScene.py", line 2812, in ogre_export
    bpy.ops.wm.call_menu( name='MiniReport' )
  File "C:\blender-2.61-release-windows64\2.61\scripts\modules\bpy\ops.py", line
 180, in __call__
    ret = op_call(self.idname_py(), None, kw)
RuntimeError: Operator bpy.ops.wm.call_menu.poll() failed, context is incorrect

location:C:\blender-2.61-release-windows64\2.61\scripts\modules\bpy\ops.py:180

That makes sense, you effectively moved the context error down the pipe. So now you are getting more context errors further in the process.

Have you viewed the tips for command line rendering?
http://www.blender.org/documentation/blender_python_api_2_61_release/info_tips_and_tricks.html?highlight=command%20line

Maybe you could set up a batch that passes the source BLEND file, and your custom script…?

That page with tips was extremely useful - I think RTFM is the phrase :stuck_out_tongue:

Commenting out offending lines has dealt with most issues, although it would be good to eventually have a streamlined set of exporters which are easily batchable…

Thanks for the help,
Peter.