Joining objects via python vs join via ui -> difference ?!

Hello,

when i create 2 cubes via script and join them there, the result differs from
creating the cubes only and joining them via ctrl j in the ui ? :spin:



import bpy

def cubes():
    mesh = bpy.data.meshes.new("Cube")
    mesh.vertices.add(8)
    mesh.vertices[0].co = Vector((1, 1, -1))
    mesh.vertices[1].co = Vector((1, -1, -1))
    mesh.vertices[2].co = Vector((-1, -1, -1))
    mesh.vertices[3].co = Vector((-1, 1, -1))
    mesh.vertices[4].co = Vector((1, 1, 1))
    mesh.vertices[5].co = Vector((1, -1, 1))
    mesh.vertices[6].co = Vector((-1, -1, 1))
    mesh.vertices[7].co = Vector((-1, 1, 1))
    
    mesh.faces.add(6)
    mesh.faces[0].vertices_raw = [0, 1, 2, 3]
    mesh.faces[1].vertices_raw = [4, 5, 6, 7]
    mesh.faces[2].vertices_raw = [0, 1, 5, 4]
    mesh.faces[3].vertices_raw = [3, 2, 6, 7]
    mesh.faces[4].vertices_raw = [0, 3, 7, 4]
    mesh.faces[5].vertices_raw = [1, 2, 6, 5]
    
    scn = bpy.context.scene  
    cube = bpy.data.objects.new("Cube", mesh)
    scn.objects.link(cube)
    
    cube2 = bpy.data.objects.new("Cube2", mesh)
    cube2.location[0] = cube.location[0] + 4
    scn.objects.link(cube2)
    
    cube.select = True
    cube2.select = True
    scn.objects.active = cube2   
#  bpy.ops.object.join()
     
cubes()

Plz try this once with the commented line, then uncomment it.
The results: 2 (visible) cubes with ctrl j, only 1 with the join function.
But WHY ?

I want to make a simple tree generator which creates linked duplicate objects of a start mesh at the endpoints. With more iterations, many objects will be created. Those
should be joined into one object at the end, but automatically within a script.
Joining the objects manually gives a correct result, but automatically joining not.

Thats why my example with the cubes.

scorpion81