0
$\begingroup$

I have a small problem. I couldn't find anything even close to it in the blender stack exchange website.

I have a python code for duplicating the selected mesh as many times as specified by the user in a slider.

Now, if the user decreases the amount of steps in the slider, and tries to "Regenerate" (my button for duplicating), I want the extra meshes to delete themselves. I find no way of doing so. I tried finding the command for Ctrl+Z in the internet, but apparently, there is no such thing.

Is there any way to do this? If not, can anyone suggest an alternative?

This is my code currently:

scene = context.scene
myprops = context.scene.my_props

loc_x = myprops.x_location
loc_y = myprops.y_location
loc_z = myprops.z_location
steps = myprops.steps_counter
steps_done = 0

while steps_done<steps:
    obj = bpy.context.active_object
    bpy.ops.object.duplicate_move(OBJECT_OT_duplicate={"linked":False, "mode":'TRANSLATION'}, TRANSFORM_OT_translate={"value":(loc_x, loc_y, loc_z), "orient_axis_ortho":'X', "orient_type":'GLOBAL', "orient_matrix":((1, 0, 0), (0, 1, 0), (0, 0, 1)), "orient_matrix_type":'GLOBAL', "constraint_axis":(False, False, False), "mirror":False, "use_proportional_edit":False, "proportional_edit_falloff":'SMOOTH', "proportional_size":1, "use_proportional_connected":False, "use_proportional_projected":False, "snap":False, "snap_elements":{'INCREMENT'}, "use_snap_project":False, "snap_target":'CLOSEST', "use_snap_self":True, "use_snap_edit":True, "use_snap_nonedit":True, "use_snap_selectable_only":False, "use_snap_to_same_target":False, "snap_face_nearest_steps":1, "snap_point":(0, 0, 0), "snap_align":False, "snap_normal":(0, 0, 0), "gpencil_strokes":False, "cursor_transform":False, "texture_space":False, "remove_on_cancel":False, "view2d_edge_pan":False, "release_confirm":False, "use_accurate":False, "use_automerge_and_split":False})
    steps_done = steps_done + 1

I pass this code through an operator and link that operator to a button which does it for me.

$\endgroup$
2
  • 1
    $\begingroup$ Does this help? blender.stackexchange.com/questions/268787/… (I don't know python myself.) $\endgroup$
    – John Eason
    Mar 28, 2023 at 9:47
  • $\begingroup$ instead of looking for ctrl + z you should have looked for ùndo, which is the funktion assoziated with ctrl + z $\endgroup$
    – maddes8cht
    Mar 28, 2023 at 10:26

1 Answer 1

0
$\begingroup$

I would suggest to use Blenders bpy.ops.object.delete() method to delete the extra meshes. You can add this line of code before your while loop:

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

And then add this line of code inside your while loop:

if steps_done >= steps:
    bpy.ops.object.select_all(action='SELECT')
    bpy.ops.object.delete()

This should deselect all objects before the loop starts and then select all objects after the loop ends and delete them.

Haven't tried it, but this should at least point in the right direction.

$\endgroup$

You must log in to answer this question.

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