1
$\begingroup$

It is not possible to set the color of the object added from the file. What am I doing wrong?

import bpy
import os
from pathlib import Path


file_path = 'd:/BlendetSkin002/280154.blend'      
inner_path = 'Object'           
object_name = 'scirt01'         


def append_scirt( path: Path):
    bpy.ops.wm.append(
            filepath=os.path.join(file_path, inner_path, object_name),
            directory=os.path.join(file_path, inner_path),
            filename=object_name,
            active_collection = True
            )
    rgba_color = (220, 0, 220, 0.99)
    material = bpy.data.materials.new(f"scirt01")
    material.diffuse_color = rgba_color
    bpy.context.active_object.data.materials.append(material)
    bpy.context.object.active_material.diffuse_color = rgba_color


append_scirt( Path(f'd:/BlendetSkin002/280154.blend'))
$\endgroup$
1
  • $\begingroup$ Do you want to replace the material of the appended object or just change the color in its material (this would mean you need some assumptions about the imported material)? In any case, diffuse_color is only for the viewport. $\endgroup$
    – taiyo
    Aug 29 at 21:15

1 Answer 1

1
$\begingroup$

When you append an object from another file it is not fully selected, as seen in the orange outline. It has to have yellow outline to be active. That's why bpy.context.object and bpy.context.active_object return nothing. After appending just add the line bpy.context.view_layer.objects.active = bpy.context.scene.objects.get(object_name) to make it the active object.

import bpy
import os
from pathlib import Path

file_path = 'd:/BlendetSkin002/280154.blend'      
inner_path = 'Object'           
object_name = 'scirt01'         

def append_scirt( path: Path):
    bpy.ops.wm.append(
            filepath=os.path.join(file_path, inner_path, object_name),
            directory=os.path.join(file_path, inner_path),
            filename=object_name,
            active_collection = True
            )
    bpy.context.view_layer.objects.active = bpy.context.scene.objects.get(object_name)
    rgba_color = (220, 0, 220, 0.99)
    material = bpy.data.materials.new(f"scirt01")
    material.diffuse_color = rgba_color
    bpy.context.active_object.data.materials.append(material)
    bpy.context.object.active_material.diffuse_color = rgba_color

append_scirt(Path(file_path))
$\endgroup$

You must log in to answer this question.

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