Hi, all.
I’m trying to write a script that will open blend file, cut out some faces and save it as different blend file, in a loop several times. Basically I need to break my model into several pieces and save them all in different files. However I’m seeing a weird behavior when using bpy.ops.wm.open_mainfile.
def cutFaces(ppath, pfile, fields):
for id in fields:
faces = fields[id]
# Open file
bpy.ops.wm.open_mainfile(filepath = ppath + '\\' + pfile)
obj = bpy.data.objects[0]
# Switch to edit mode
bpy.ops.object.mode_set(mode = 'EDIT')
# Select all the faces except for faces in the list
mesh = bmesh.from_edit_mesh(obj.data)
for face in mesh.faces:
face.select = face.index in faces
bmesh.update_edit_mesh(obj.data)
del mesh
bpy.ops.wm.save_mainfile(filepath = ppath + 'fields' + id + '.blend')
I then create a .py file with following code to call the function
import bpy
import finalization
finalization.cutFaces(r'D:\Work', 'data.blend', {1: [1,2,3,4,5,6,7,8]})
print('Success')
Then I run blender in background and executing the script
c:\Program Files\Blender Foundation\Blender>blender.exe -b -P "D:\Work\finalizeLayout.py"
And getting this:
File "c:\Program Files\Blender Foundation\Blender\2.76\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
Now when I remove bpy.ops.wm.open_mainfile from the script and open the file in blender via command line, it works fine.
c:\Program Files\Blender Foundation\Blender>blender.exe "D:\work\data.blend" -b -P "D:\Work\finalizeLayout.py"
What am I missing?
Thanks in advance.