1
$\begingroup$

Context:

I am working on a tree generation script and I am pretty happy with how it turned out, but I encountered an issue when adding leaves to my tree. The leaves themselves have no modifiers and for for the sake of this question could be replaced with the default cube(their vertice number does not exceed 30).

The Problem:

Initially I thought that the coping and placing the leaves to their respective branches on the tree took the longest to complete, but after I ran some tests it turns out that actually joining the leaves to the tree is what took the longest. (Almost 3 or 4 times more than copying and moving them around)

I used the below function to join my leaves to the tree:

def JoinTreeObjectsWithTree(tree):
    global treeObjects
    #join leaf with Trunk
    tree.select_set(True)
    bpy.context.view_layer.objects.active = tree
    for treeObject in treeObjects:
        treeObject.select_set(True)

    bpy.ops.object.join()
    bpy.ops.object.select_all(action='DESELECT')
    treeObjects = []

The tree objects array is the actual array where the leaves reside and the tree is well the main body of the tree as an object.

Is there a way to speed this up?

$\endgroup$
5
  • 1
    $\begingroup$ Related blender.stackexchange.com/a/166446/15543 (part 1 is same as answer below) If the result desired is to have a single object possibly quicker to add to mesh directly without creating separate objects first. $\endgroup$ – batFINGER May 9 at 16:22
  • $\begingroup$ I kind of want the power too change the material of the leaves and the trunk separatley later one, the method that I am using above lets me do this, if I add the mesh directly it will not let me. $\endgroup$ – TobyB May 10 at 5:10
  • $\begingroup$ Oh well since blender 2.9 using my method will make the trunk and leaves have only one material.... $\endgroup$ – TobyB May 10 at 7:59
  • $\begingroup$ Disagree entirely re not being able to set different materials when creating as a single mesh. If N leaves each having F faces are added after trunk mesh then the last N * F faces of mesh are leaves. Can't see any reason "Blender will not let you" change the material index of these faces. $\endgroup$ – batFINGER May 10 at 12:24
  • $\begingroup$ @batFINGER yeah ur right, I just tried it and the thing was that if the main trunk did not have a material assigned to it it will just pull one from the leaves when exporting making the whole tree green, but if it has one then it works fine. $\endgroup$ – TobyB May 10 at 12:46
3
$\begingroup$

I've been using this, which has featured on stack before.

def merge_objects(name, ob_list):
    ctx = context.copy()
    ob_list[0].name = name
    ctx["active_object"] = ob_list[0]
    ctx["selected_editable_objects"] = ob_list
    bpy.ops.object.join(ctx)
    return data.objects[name]

Naming is optional, but requires the tree as the first object in the list to keep its name.

def JoinTreeObjectsWithTree(tree, treeObjects):
    name = tree.name
    treeObjects.insert(0, tree)
    ctx = context.copy()
    ctx["active_object"] = treeObjects[0]
    ctx["selected_editable_objects"] = treeObjects
    bpy.ops.object.join(ctx)
    return data.objects[name]

finalOb = JoinTreeObjectsWithTree(tree, treeObjects)
$\endgroup$
1
  • $\begingroup$ I'll try this, even though I do not really get it why it would be faster, other than that for loop which shouldn't be that much of a problem. $\endgroup$ – TobyB May 10 at 12:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.