0
$\begingroup$

I have linked file and I'm trying to use Edit Linked Library Addon via script to change the material
like this:

import bpy

bpy.context.view_layer.objects.active = bpy.data.objects['obj_name']
bpy.ops.object.edit_linked()
bpy.data.objects['obj_name'].active_material=bpy.data.materials['Material2']
bpy.ops.wm.return_to_original()

bpy.context.scene.render.filepath = "/../testing.png"
bpy.ops.render.opengl(animation=False, write_still=True, view_context=True)

but it gives the error:

RuntimeError: Operator bpy.ops.render.opengl.poll() failed, context is incorrect

What am I doing wrong?

Edit:
I inspected the source code and turns out the problem stems from:

bpy.ops.wm.open_mainfile(filepath=settings["original_file"])

The explanation given here as:

When you load a new file with open_mainfile the current file gets thrown out. That includes the context of the Python script you're running.

$\endgroup$

1 Answer 1

0
$\begingroup$

I took an alternative approach, it's a little hacky but it gets the job done:

import bpy,subprocess

file_path="/path/to/file.blend"

code="import bpy;bpy.data.objects['obj_name'].active_material=bpy.data.materials[',material_name'];bpy.ops.wm.save_as_mainfile(filepath=bpy.data.filepath)"
output = subprocess.check_output(f'blender "{file_path}" -b --python-expr "{code}"', shell=True)
bpy.data.libraries["Dummy.blend"].reload()
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .