Getting crazy about activating/deactivating objects with python on a blender add on

Hey Guys. Working on my add on, and I’m slightly getting mad because I have things that gave different results and should not…
So I have scripts, in scripts, in scripts. When I execute 2 levels on blender, it works just fine and instantly. But when I add my last level, the same script that was instantaneous takes about 10 minutes.
For what I have found now, it might be that the way that I use to duplicate my object (copy it and affect the data, I don’t animate) is not enough to get the same object after.

def func_duplicateNfinish(obj,params):
    (nameplate_size,xvalue,yvalue,zvalue,xscale,yscale,zscale,version,kind)=params
    bpy.ops.object.select_all(action='DESELECT')
    active_obj=obj
    #initiate it's coordinate to 0
    bpy.context.view_layer.objects.active = bpy.data.objects["Empty"]
    if bpy.context.active_object is not None:
        bpy.context.scene.cursor.location = bpy.context.active_object.location
        bpy.context.view_layer.objects.active = active_obj
    active_obj.select_set(True)
    #use active object as base
    new_obj = bpy.context.active_object.copy()
    bpy.context.collection.objects.link(new_obj)
    new_obj.data = new_obj.data.copy()
    bpy.ops.object.select_all(action='DESELECT')
    bpy.context.view_layer.objects.active = bpy.data.objects[new_obj.name]

    new_obj.name= kind + "_" + obj.name + "_style_" + version

    bpy.ops.object.origin_set(type='ORIGIN_CURSOR', center='MEDIAN')
    #move the object to the new coordinates and rescale
    bpy.context.object.location[0] = float(xvalue)
    bpy.context.object.location[1] = float(yvalue)
    bpy.context.object.location[2] = float(zvalue)
    bpy.context.object.scale[0] = float(xscale)
    bpy.context.object.scale[1] = float(yscale)
    bpy.context.object.scale[2] = float(zscale)

Also I’m not 100% sure if the way I use to deactivate all active objects/activate the one I need is done the right way. here the example to deselect everything, and activate only an object ref_obj to work with :

bpy.ops.object.select_all(action='DESELECT')
bpy.context.view_layer.objects.active = ref_obj
ref_obj.select_set(True)

If you have propositions, I’m kind of tired to lose myself in things that should be easy to do :confused: I’m sure I’m missing things, but I worked hard. At first i was using the duplicate function to copy and I was always modifying my initial object and letting the copy… I finally succeeded to take the right object, but now the modifiers don’t work nicely.
Help me please :slight_smile:

I’m having some trouble understanding your post.

What do you mean by “levels” here?

What is your script supposed to do?

If you cannot create a minimal reproducible example, please show all of your code.