1
$\begingroup$

I have made the following python script which contains one function, this function creates one simple mesh.

import bpy
import mathutils
from itertools import repeat


def add_single_brick(brick_width, brick_length, brick_height):


    mesh_name = "brick" 
    collection_name = bpy.data.collections.new("BrickCollection")
    bpy.context.scene.collection.children.link(collection_name)

    vertices_brick = [  (0,0,0),
                    (0,brick_width,0),
                    (brick_length,brick_width,0),
                    (brick_length,0,0),
                    
                    (0,0,brick_height),
                    (0,brick_width,brick_height),
                    (brick_length,brick_width,brick_height),
                    (brick_length,0,brick_height)
                    ]

    edges = []

    faces = [(0,1,2,3),
         (4,5,6,7),
         (0,4,5,1), 
         (1,5,6,2),
         (2,6,7,3),
         (3,7,4,0)
         ]


    new_mesh = bpy.data.meshes.new('brick_mesh')
    new_mesh.from_pydata(vertices_brick, edges, faces)
    new_mesh.update()


    # make object from mesh
    new_object = bpy.data.objects.new(mesh_name, new_mesh)
    collection_name.objects.link(new_object) 


brick_length = 0.21
brick_width = 0.1
brick_height = 0.05    

add_single_brick(brick_width=brick_width, brick_length=brick_length, brick_height=brick_height)

I call the function at the end of the script to create the mesh within a collection I called 'BrickCollection' enter image description here

The mesh is in this collection:

enter image description here

When I run the script again with other variables in the parameters, I want the script to update the existing mesh and not make a new mesh.

I found this post 3: When to use bmesh.update_edit_mesh and when Mesh.update? as in which I understand I should start using the bmesh module

I also found this post

I am getting quite confused, now I have two simple questions:

  • Is there any simple python example of mesh being overwritten within a collection when running the python script again with different parameters?
  • Does the bmesh module also work with modifiers?
$\endgroup$
1
  • $\begingroup$ You don't want to create a new object if you want to update the mesh in place. You would have to edit the original object's mesh in place to do that. On the other hand, you could create the new object, then delete the original, and then rename the new object to the old object's name. $\endgroup$ Commented Oct 5, 2021 at 17:59

1 Answer 1

1
$\begingroup$

I solved it by making a function which checks if the collection already exists, if it exists it removes the content of the collection before calling the function which makes creates the mesh.

def remove_brick_collection():

    bpy.ops.object.select_all(action='DESELECT')

    if (bool(bpy.data.collections.get(name_collection))) == True:
  
        collection = bpy.data.collections.get(name_collection)
        bpy.data.collections.remove(collection)
    
    else:
        print ("no collection found")

at the end of the script:

remove_brick_collection()
add_single_brick(brick_width=1, brick_length=2, brick_height=3)
$\endgroup$
1
  • $\begingroup$ You have to remove the mesh as well, not just the collection. Otherwise it keeps creating new meshes and adding them to the file. You can't see them in the GUI but they are there if you look in `bpy.data.meshes'. $\endgroup$
    – N. Virgo
    Commented Oct 15, 2023 at 6:13

You must log in to answer this question.

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