1
$\begingroup$

After moving from Blender 3.3 to 3.6.1 (on Macos en Unix), my scene-building python scripts fail to execute, not properly importing materials or collections when using bpy.ops.wm.append. The operator doesn't give any error, but just doesn't load anything. I have made two example scripts to isolate the problem. The first creates two cubes and gives both a unique material. This file is saved, to be used as a library. The second script tries to load a material or a collection using bpy.ops.wm.append, but both fail to do so.

The material loading script:

import bpy

MainDir = bpy.path.abspath("//")

def ImportMaterial(filename, Material, Linked=False):

    bpy.ops.wm.append(
        directory=MainDir+filename+"\\Material\\",
        filename=Material)

    return bpy.data.materials[Material] 

Data = ImportMaterial(
    filename='TestCollection.blend', 
    Material='Material_2'
    )
print(bpy.data.materials[0])

The collection loading script is:

import bpy

MainDir = bpy.path.abspath("//")

def ImportCollection(filename, Collection, Zpos=-100, Instanced=False, Linked=False):

    bpy.ops.wm.append(
        directory=MainDir+filename+"\\Collection\\",
        filename=Collection)
    
    return bpy.data.collections[Collection]

Data = ImportCollection(
    filename='TestCollection.blend', 
    Collection='Cubes'
    )
print(bpy.data.collections[0])

The script making the library file is:

import bpy

bpy.ops.mesh.primitive_cube_add(size=2, location=(0, 0, 0))
Cube_1 = bpy.context.object
Cube_1.name='Cube_1'

bpy.data.materials.new(name='Materials_1')
bpy.data.materials['Materials_1'].diffuse_color = (0, 1, 0, 1)
Cube_1.data.materials.append(bpy.data.materials['Materials_1'])

bpy.ops.object.move_to_collection(collection_index=0, is_new=True, new_collection_name="Cubes")

bpy.ops.mesh.primitive_cube_add(size=2, location=(-4, 4, 0))
Cube_2 = bpy.context.object
Cube_2.name='Cube_2'

bpy.data.materials.new(name='Materials_2')
bpy.data.materials['Materials_2'].diffuse_color = (1, 0, 0, 1)
Cube_2.data.materials.append(bpy.data.materials['Materials_2'])

bpy.ops.object.move_to_collection(collection_index=2)

The script are set up for Blender 3.6.1. The import works flawless in Blender 3.3, but does not work in 3.6.1. I would appreciate any help.

$\endgroup$
2
  • $\begingroup$ The first and second scripts worked well in my Blender 3.60 (windows). But I has changed the index, for example, materials[-1] or collections[-1]. $\endgroup$
    – tetii
    Jul 27, 2023 at 5:15
  • $\begingroup$ Are you on Windows? It is not a matter of indexing, as there are no materials at all after running the script on my Mac. $\endgroup$ Jul 27, 2023 at 5:26

2 Answers 2

0
$\begingroup$

To answer my own question, it turns out you have to use / rather then \\ in Blender 3.4 and later when adding the within-blender directory. So use filename+"/Collection/" rather then filename+"\\Collection\\".

$\endgroup$
4
  • 1
    $\begingroup$ no that is bad practice. you should use os.path.join() or the newer pathlib instead of raw string concatenation to ensure your code remains cross-platform and maintains compatibility with different file systems. $\endgroup$ Aug 28, 2023 at 14:46
  • $\begingroup$ Also for internal blender directories? $\endgroup$ Aug 29, 2023 at 17:27
  • $\begingroup$ what do you mean by "internal blender directories", can you give an example? $\endgroup$ Aug 30, 2023 at 6:01
  • $\begingroup$ If I understand this correctly, they are basically what you see if you switch in the outliner to the Blender file view. Or when you open a file using the menu through File-Append. $\endgroup$ Aug 31, 2023 at 8:46
0
$\begingroup$

For your case, directory=os.path.join(MainDir, "filename.blend", "Collection") is more correct and future proof. It seems that interior blender structures are accessed as os folders. I tested it within an add-on for Blender v3.3 and v3.6 with both Windows and macOS and everything works well.

$\endgroup$

You must log in to answer this question.

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