0
$\begingroup$

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 :/ 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 :)

$\endgroup$

1 Answer 1

0
$\begingroup$

First you should distinguish between active and selected objects. You can select multiple objects but only 1 object is the active one. The active element is the last one to be selected and will be highlighted in Outliner. See: Selections and the Active Object

There´s no need to toggle back and forth to copy and move objects. Make it simple:

import bpy

def func_duplicateNfinish_test(obj, params):
    (nameplate_size,xvalue,yvalue,zvalue,xscale,yscale,zscale,version,kind)=params
       
    new_obj = obj.copy()                   # copy obj with all data
    new_obj.name = kind + "_" + obj.name + "_style_" + version            
    bpy.context.collection.objects.link(new_obj)  # link new_obj to scene

    #move the object to the new coordinates and rescale
    new_obj.location[0] = float(xvalue)
    new_obj.location[1] = float(yvalue)
    new_obj.location[2] = float(zvalue)
    new_obj.scale[0] = float(xscale)
    new_obj.scale[1] = float(yscale)
    new_obj.scale[2] = float(zscale)

    bpy.ops.object.select_all(action='DESELECT')      # deselect all
    bpy.context.view_layer.objects.active = bpy.data.objects[new_obj.name]  # new_obj is active
    new_obj.select_set(True)    # new_obj is selected

# testcall
params = ("nameplate_size",5 ,5, 5, 2, 2, 2, "version", "kind") # example
if bpy.context.active_object is not None:
    obj = bpy.context.active_object        # get active obj
    func_duplicateNfinish_test(obj, params)
$\endgroup$

You must log in to answer this question.

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