0
$\begingroup$

I have a grid of objects that each have 2 separate shells in them (cube and sphere) enter image description here What I want to do is delete the mesh in front, which would be all vertices where their Y position is less than 5. And then select the rest of the vertices and move them along the Y axis by -15.

For the deletion part I have found this script in another question:

import bpy
import bmesh

ob = bpy.context.object
assert ob.type == "MESH"
mat = ob.matrix_world
me = ob.data

global_z_plane = False

if me.is_editmode:
    bm = bmesh.from_edit_mesh(me)
else:
    bm = bmesh.new()
    bm.from_mesh(me)

for v in bm.verts:
    co = mat * v.co if global_z_plane else v.co
    if co.z < 0:
        bm.verts.remove(v)

if bm.is_wrapped:
    bmesh.update_edit_mesh(me)
else:
    bm.to_mesh(me)
    me.update()

This seems to work if I change the Z for a Y and the 0 for 5 but only for the selected object. I need it to do it with all objects in a collection with a specific name like 'Collection'. I've tried grafting together the code from another question I made previously, but I don't really know enough python to make it work. Can anyone help me with this?

$\endgroup$
7
  • $\begingroup$ Sounds like a possible XY problem to me. Why do you need this? What's the actual goal that you want to achieve by deleting them and moving them? What's the context? $\endgroup$ Commented Sep 24, 2023 at 15:41
  • $\begingroup$ @Martynas Žiemys the grid is the result of merging the spheres to the objects in front of them. The objects are a sort of asset library and have other objects linked to them in the file so when I need to change the meshes in the library but still retain the same names and links the best way I've found is to just merge the new meshes to them and delete the old ones, then move the new one forward. It's not like it takes a lot of time to do it manually but if there's an automatic way that'd be appreciated. $\endgroup$
    – Cornivius
    Commented Sep 24, 2023 at 16:16
  • $\begingroup$ The code I used to merge them is from this question: blender.stackexchange.com/questions/300848/… but if I try to copy the lines that I think select the objects in the collection it does not work $\endgroup$
    – Cornivius
    Commented Sep 24, 2023 at 16:17
  • $\begingroup$ Can't you just replace object's data? $\endgroup$ Commented Sep 24, 2023 at 20:05
  • $\begingroup$ Like if you have Cube and Suzanne bpy.data.objects["Cube"].data = bpy.data.objects["Suzanne"].data $\endgroup$ Commented Sep 24, 2023 at 20:12

1 Answer 1

0
$\begingroup$

you have to adapt the "coll1" to your collection name you need.

import bpy
import bmesh

for object in bpy.data.collections["coll1"].all_objects:
    
    if object.type == 'MESH':
        if object.mode == 'OBJECT':
            bm = bmesh.new()
            bm.from_mesh(object.data)
        else:
            bm = bmesh.from_edit_mesh(object.data)


        bm.verts.ensure_lookup_table()
        bm.faces.ensure_lookup_table()

        for vert in bm.verts:
            print(vert.co)
            if vert.co.y <= 5:
                bm.verts.remove(vert)
            else:
                vert.co.y += -15

        if object.mode == 'OBJECT':
            bm.to_mesh(object.data)
            bm.free()
        else:
            bmesh.update_edit_mesh(object.data)
    
$\endgroup$

You must log in to answer this question.

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