1
$\begingroup$

I need a python script that takes an object, moves it down, separates it into loose parts, sets the origins to geometry and deletes 1 object that is left below Z=0. So far I've cobbled together this code from different posts but when it comes to selecting the lower object all the results I get are talking about vertices and not objects themselves. Can anyone help me with this? At the end of this code all the objects are already selected and not in a specific collection so maybe deselecting anything above Z=0 would work better before deleting the last object. The code shouldn't affect non selected objects in the scene, not sure if that's relevant

import bpy

#Move down
bpy.ops.transform.translate(value=(0.0, 0.0, -20.0), orient_type='GLOBAL')

# Separate the selected objects by disconnected geometry
bpy.ops.mesh.separate(type="LOOSE")

# Get the selected objects
separated_obj_names = [obj.name for obj in bpy.context.view_layer.objects.selected]

#origins to geometry
bpy.ops.object.origin_set(
    type='ORIGIN_GEOMETRY',
    center='MEDIAN')
$\endgroup$
3
  • 1
    $\begingroup$ for o in bpy.context.selected_objects[:]: if o.location.z < 0: bpy.data.objects.remove(o) $\endgroup$ Jan 17 at 18:57
  • 1
    $\begingroup$ Thanks, works perfectly $\endgroup$
    – Cornivius
    Jan 17 at 22:40
  • $\begingroup$ @MarkusvonBroady That appears to be an answer. Please post it as such. $\endgroup$
    – TheLabCat
    May 15 at 11:39

1 Answer 1

1
$\begingroup$

You can try bpy.data.batch_remove([o for o in bpy.context.selected_objects if o.location[2] < 0]).

https://docs.blender.org/api/current/bpy.types.BlendData.html#bpy.types.BlendData.batch_remove

$\endgroup$

You must log in to answer this question.

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