I currently am writing python operator whith the task of appending the current scene into a new file and then linking it back into the original file. There is one problem: When loading a new file the bpy.context changes and I do not quite know how to reload it. This leads to the error "context is incorrect" when trying to append the original scene into the new file. Here is how my code looks so far:
class BPC_main_Outsource(bpy.types.Operator):
bl_idname = "scene.outsource"
bl_label = "Outsource"
def execute(self, context):
compScene = bpy.context.scene #get scene
bpy.ops.wm.save_mainfile() #save current file
blendFile = bpy.data.filepath #get path of current file
apFilename = compScene.name #determine scene's name
bpy.ops.wm.read_homefile() #load a new file
oldScene = bpy.context.scene #get current scene in new file
blendFile = blendFile.replace("\\", r"/")
apPath = "//" + bpy.path.basename(blendFile) + "/Scene/" + apFilename #build append path
apDirectory = blendFile + "/Scene/" #build append directory
bpy.ops.wm.append(filepath=apPath, directory=apDirectory, filename=apFilename) #append scene from main file
bpy.ops.scene.delete() #delete old scene in new file
#TODO: save file, open original file, link back
return {"FINISHED"}
Any help on how to fix this would be greatly appreciated. Thanks in advance.