1
$\begingroup$

I'm new to Blender API and i'm trying to get to know it. I made a Panel with buttons for easy testing and i'm doing simple algorithms on cubes to gain experience.

The thing is i think i'm missing something with references to values. If you run the code everything is very explicit in the panel, if you test the "sort" function, the actual thing happening is nothing close to any sorting, because while i try to modify the values in the function and print them, nothing happens... Even if i do something as simple as modifying the dimensions, if i print the values right before and after the line where it happens, the console shows nothing happened.

Thanks everyone for the help, here is the code :

import bpy,os,math


#### PART CODE ###"

#return volume of cube
def volume_cube(cube):
    return cube.dimensions[0]*cube.dimensions[1]*cube.dimensions[2]


#display volumes
def display_volumes(liste):
    print("dipslay")
    for i in range(len(liste)):
        print(i, " : ", volume_cube(liste[i]))

#place cubes corner to corner
def ordonner_cubes(liste_cube):
    liste_cube[0].location[0] = 0.0
    liste_cube[0].location[1] = 0.0
    liste_cube[0].location[2] = 0.0
    for i in range(len(liste_cube)-1) :
        liste_cube[i+1].location[0] = liste_cube[i].location[0] + liste_cube[i+1].dimensions[0]/2 + liste_cube[i].dimensions[0]/2
        liste_cube[i+1].location[1] = liste_cube[i].location[0] + liste_cube[i+1].dimensions[1]/2 + liste_cube[i].dimensions[1]/2
        liste_cube[i+1].location[2] = liste_cube[i].location[0] + liste_cube[i+1].dimensions[2]/2 + liste_cube[i].dimensions[2]/2


    
def sort_cube_list(liste) :
    print("------------------")
    v=[]
    for i in liste:
        v.append(i.dimensions)
    
    for i in range(len(liste)-1):
        if (volume_cube(liste[i]))>volume_cube(liste[i+1]):
            print("i : ",i)
            print("liste[",i,"].dimensions : ", liste[i].dimensions)
            print("v[",i+1,"] : ", v[i+1])
            liste[i].dimensions=v[i+1]
            liste[i].data.update()
            print("liste[",i,"].dimensions : ", liste[i].dimensions)
            print("liste[",i+1,"].dimensions : ", liste[i+1].dimensions)
            print("v[",i,"] : ", v[i])
            liste[i+1].dimensions = v[i]
            liste[i+1].data.update()
            print("liste[",i+1,"].dimensions : ", liste[i+1].dimensions)
            v[i] = liste[i].dimensions
            v[i+1] = liste[i+1].dimensions
    print("fin for")
    for i in range(len(liste)): 
        print("liste :", liste[i].dimensions)

def reset_cubes(liste):

    for i in range(len(liste)):
        liste[i].dimensions = [len(liste)-i,len(liste)-i,len(liste)-i]
   
##########################################



#CONVENTION
#MYADDON_OT -> Operator (genre une fonction)
#MYADDON_PT -> Panel (le petit menu sur la droite)

#bpy.ops.wm.console_toggle()

#BOUTTON FOR RESETING
##############################
class CUBES_OT_reset_cubes(bpy.types.Operator):
    bl_idname = "object.perso_reset_cubes"
    bl_label = "Reset Cubes"
    bl_description = "Allows to reset the cubes"
    
    def execute(self, context):
        liste_cube =  bpy.data.objects
        reset_cubes(liste_cube)
        
        return {'FINISHED'}
    
#BOUTTON FOR PLACING
##############################
class CUBES_OT_place_cubes(bpy.types.Operator):
    bl_idname = "object.perso_place_cubes"
    bl_label = "Place Cubes"
    bl_description = "Allows to place the cubes"
    
    def execute(self, context):
        liste_cube = bpy.data.objects
        ordonner_cubes(liste_cube)
       
        return {'FINISHED'}

#BOUTTON FOR SORTING
##############################
class CUBES_OT_sort_cubes(bpy.types.Operator):
    
    bl_idname = "object.perso_sort_cubes"
    bl_label = "Sort Cubes"
    bl_description = "Allows to sort the cubes"
    
    def execute(self, context):
        liste_cube = bpy.data.objects
        sort_cube_list(liste_cube)
        print("Fin Fonction")
        for i in range(len(liste_cube)): 
            print("liste_cube :", liste_cube[i].dimensions)
            
        return {'FINISHED'}

#BOUTTON FOR DISPLAY
##############################
class CUBES_OT_display_volumes(bpy.types.Operator):
    
    bl_idname = "object.perso_display_volumes"
    bl_label = "Display Volumes"
    bl_description = "Allows to display volumes of cubes"
    
    def execute(self, context):
        liste_cube = bpy.data.objects
        display_volumes(liste_cube)
                
        return {'FINISHED'}
    


#BUTTON FOR CLEARING CONSOLE
##############################
class CUBES_OT_clear_console(bpy.types.Operator):
    bl_idname = "object.perso_clear_console"
    bl_label = "Clear Console"
    bl_description = "Allows to Clear Console"
    
    def execute(self, context):
        os.system('cls')
        return {'FINISHED'}
    


class SandboxPanel(bpy.types.Panel):
    bl_label = "Sandbox Panel"
    bl_idname = "PERSO_PT_layout"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_category = 'Sandbox Addon'
    
    def draw(self,context):
        
        layout = self.layout
        
        row = layout.row()
        row.label(text = "Add Shape", icon = 'LIGHTPROBE_CUBEMAP')
        row.operator("mesh.primitive_cube_add")
        row.operator("mesh.primitive_uv_sphere_add")
        
        row = layout.row()
        row.label(text = "Erase console", icon = 'GPBRUSH_ERASE_STROKE')
        row.operator("object.perso_clear_console")
        
        row = layout.row()
        row.label(text = "Display Volumes", icon = 'CUBE')
        row.operator("object.perso_display_volumes")
        
        row = layout.row()
        row.label(text = "Place Cubes", icon = 'EMPTY_AXIS')
        row.operator("object.perso_place_cubes")
        
        
        row = layout.row()
        row.label(text = "Sort Cubes", icon = 'SORTALPHA')
        row.operator("object.perso_sort_cubes")
        
        row = layout.row()
        row.label(text = "Reset Cubes", icon = 'TRASH')
        row.operator("object.perso_reset_cubes")
        
        
def register():
    bpy.utils.register_class(SandboxPanel)
    bpy.utils.register_class(CUBES_OT_clear_console)
    bpy.utils.register_class(CUBES_OT_display_volumes)
    bpy.utils.register_class(CUBES_OT_sort_cubes)
    bpy.utils.register_class(CUBES_OT_place_cubes)
    bpy.utils.register_class(CUBES_OT_reset_cubes)
    
def unregister():
    bpy.utils.unregister_class(SandboxPanel)  
    bpy.utils.unregister_class(CUBES_OT_clear_console)
    bpy.utils.unregister_class(CUBES_OT_display_volumes)
    bpy.utils.unregister_class(CUBES_OT_sort_cubes)
    bpy.utils.unregister_class(CUBES_OT_place_cubes)    
    bpy.utils.unregister_class(CUBES_OT_reset_cubes)
    
if __name__ == "__main__":
    register()    
$\endgroup$
0

Browse other questions tagged or ask your own question.