2
$\begingroup$

This is my first post so pardon any errors in formatting.

I'm trying to create a script that opens a file and links objects to it however when I attempt to link the objects it gives me a polling error. I looked up how to fix it and I found this code.

for area in bpy.context.screen.area:
    if area.type == 'VIEW_3D':
        override = bpy.context.copy()
        override['area'] = area
        bpy.ops.view3d.background_image_add(override, name="BG", filepath=r"image.png")
                break

it only works when the following code isn't present

bpy.ops.wm.open_mainfile()

When it is present I get

AttributeError: 'NoneType' object has no attribute 'area'

I have looked all over and could not find anyone else having this issue so I'm under the impression it is a very niche case so any help would be greatly appreciated. Below is similar code without all the extra filler I've written. The code in question is on lines 44-58.

import bpy

bl_info = {
    "name": "Link & Append",
    "author": "Exotic",
    "version": (0, 0, 1),
    "blender": (2, 7, 9),
    "location": "Render > sample",
    "description": "",
    "warning": "",
    "wiki_url": "",
    "tracker_url": "",
    "category": "System"}


class SampleMessage(bpy.types.Operator):
    """SampleMessage"""
    bl_idname = "button.message"
    bl_label = "Message"

    def execute(self, context):
        bpy.ops.wm.save_load_operator()
        return {'FINISHED'}

    def invoke(self, context, event):
        wm = context.window_manager
        return wm.invoke_props_dialog(self, width=400, height=400)

    def draw(self, context):
        layout = self.layout
        layout.label("WARNING", icon='ERROR')
        row = layout.row()
        row.label("Are you Sure?.")
        row = layout.row()
        row.label("Press OK")


class SaveNLoad(bpy.types.Operator):
    """save_load Operator"""
    bl_idname = "wm.save_load_operator"
    bl_label = "Save N' Load Operator"

    def execute(self, context):
        yourpath = "C:\\Your path here\\mother.blend"
        bpy.ops.wm.open_mainfile(filepath=yourpath)

        for area in bpy.context.screen.area:
            if area.type == 'VIEW_3D':
                override = bpy.context.copy()
                override['area'] = area
                bpy.ops.view3d.background_image_add(override, name="BG", filepath=r"image.png")
                break

        directory = "C:\\Your path here\\Cone.blend\\Object\\Cone.001"
        filename = "Cone.001"
        filepath = "C:\\Your file path\\Cone.blend\\Object\\Cone"

        bpy.ops.wm.link(filepath=filepath, directory=directory, filename=filename)
        return {'FINISHED'}


def add_object_button(self, context):
    self.layout.operator(
        SampleMessage.bl_idname,
        text=SampleMessage.__doc__,
        icon='BLENDER'
    )


def register():
    bpy.utils.register_class(SaveNLoad)
    bpy.utils.register_class(SampleMessage)
    bpy.types.VIEW3D_MT_object.append(add_object_button)


if __name__ == "__main__":
    register()

Any help would be greatly appreciated.

$\endgroup$
1