Click here to Skip to main content
16,021,181 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I have a recurring problem in blender with a python module I wrote. I have to take an existing object, make it active, then copy it's property into another object. It's been a pain in the ass since I tried to do so . For now the recurring error I get is :
Terminal
Error: Python: Traceback (most recent call last):
  File "C:\Users\Alexandre\AppData\Roaming\Blender Foundation\Blender\3.1\scripts\addons\Nameplate_bender\nbe_op.py", line 692, in execute
    finish_dupliker(active_obj,params)
  File "C:\Users\Alexandre\AppData\Roaming\Blender Foundation\Blender\3.1\scripts\addons\Nameplate_bender\nbe_op.py", line 657, in finish_dupliker
    create_full_large_element(active_obj,nameplate_size,mirror_model,autoremesh,decimate_replace)
  File "C:\Users\Alexandre\AppData\Roaming\Blender Foundation\Blender\3.1\scripts\addons\Nameplate_bender\nbe_op.py", line 418, in create_full_large_element
    func_duplicateNfinish(active_obj,decimate_replace_active,mirror_model_active,autoremesh_active,element)
  File "C:\Users\Alexandre\AppData\Roaming\Blender Foundation\Blender\3.1\scripts\addons\Nameplate_bender\nbe_op.py", line 331, in func_duplicateNfinish
    new_obj = bpy.context.active_object.copy()
AttributeError: 'NoneType' object has no attribute 'copy'

location: <unknown location>:-1



the code that is causing the error is this one:
Python
#duplicate_greeblie//used to make all the positioning and export// function that copy object, name it,move it, scale it, finish it
def func_duplicateNfinish(obj,decimate_replace,mirror_model,autoremesh,params):
    print_timestamp("func_duplicateNfinish")
    (nameplate_size,xvalue,yvalue,zvalue,xscale,yscale,zscale,xrotate,yrotate,zrotate,version,kind)=params
    active_obj=obj
    func_apply_transforms(active_obj)
    decimate_replace_active=decimate_replace
    mirror_model_active=mirror_model
    autoremesh_active=autoremesh
    func_activate_object(active_obj)
    #initiate it's coordinate to 0
    bpy.context.view_layer.objects.active = bpy.data.objects["Empty"]
    bpy.context.object.hide_set(False)
    if bpy.context.active_object is not None:
        #bpy.context.scene.cursor.location = bpy.context.active_object.location
        bpy.ops.view3d.snap_cursor_to_active()
        bpy.context.view_layer.objects.active = active_obj
    active_obj.select_set(True)
        
    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=bpy.context.view_layer.objects.active
    new_obj.name= nameplate_size + "_" + 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)
    bpy.context.object.rotation_euler[0] = float(xrotate)
    bpy.context.object.rotation_euler[1] = float(yrotate)
    bpy.context.object.rotation_euler[2] = float(zrotate)


    params= (nameplate_size,True,True,mirror_model_active,True,autoremesh_active,True,True,True,True,decimate_replace_active,True)
    #transformation et export final du greeblie
    finish_greeblie(new_obj,params)
    bpy.ops.object.select_all(action='DESELECT')


and the part that is really annoying, causing the error is this :
Python
active_obj.select_set(True)

new_obj = bpy.context.active_object.copy()


Any idea on how to pass all parameters of an object to a new one? and how to desactivate this error?

What I have tried:

I have tried running this script many times. It fails for reason I don't understand, and sometimes it runs, and I still don't understand why. I feel like I'm turning around, and I don't know how to solve it.
Posted
Comments
[no name] 23-Jun-23 5:25am    
The error message is clear, active_object has not been initialised so it cannot be used to call the copy method. So you need to debug the code to find out why that reference is still null.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900