Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Script works in text editor but doesn't do anything when button pressed #19

Closed
mrgesy opened this issue Aug 9, 2023 · 4 comments
Closed
Labels
bug Something isn't working
Milestone

Comments

@mrgesy
Copy link

mrgesy commented Aug 9, 2023

Hey. What's wrong with my script? It works on selected objects in the text editor but when I load it into your button , it does nothing. Here's the script:

import bpy
import random
import math

def find_similar_object(selected_object, objects_to_compare, threshold_volume, threshold_center):
    for obj in objects_to_compare:
        if obj != selected_object:
            if abs(selected_object.dimensions[0] * selected_object.dimensions[1] * selected_object.dimensions[2] - obj.dimensions[0] * obj.dimensions[1] * obj.dimensions[2]) <= threshold_volume:
                center_dist = (selected_object.location - obj.location).length
                if center_dist <= threshold_center:
                    return obj
    return None

def assign_new_name(obj, new_name):
    obj.name = new_name

def calculate_center_of_geometry(obj):
    bpy.ops.object.select_all(action='DESELECT')
    obj.select_set(True)
    bpy.context.view_layer.objects.active = obj
    
    # Store current object's location and origin point
    original_location = obj.location.copy()
    original_origin = obj.location.copy()  # Store original origin
    
    # Set the origin to the center of geometry
    bpy.ops.object.origin_set(type='ORIGIN_GEOMETRY', center='BOUNDS')
    
    # Calculate the center of geometry
    center_of_geometry = obj.location.copy()
    
    # Restore original origin
    obj.location = original_origin
    
    # Restore original location
    obj.location = original_location
    
    return center_of_geometry

def has_high_vertex_count(obj, other_obj):
    return len(obj.data.vertices) > len(other_obj.data.vertices)

def has_bevel_modifier(obj):
    return any(mod.type == 'BEVEL' for mod in obj.modifiers)

def has_subdivision_modifier(obj):
    return any(mod.type == 'SUBSURF' for mod in obj.modifiers)

def has_hp_material(obj):
    return any(mat.name == "HP" for mat in obj.data.materials)

def main():
    threshold_volume = 0.01
    threshold_center = 0.1
    
    selected_objects = bpy.context.selected_objects
    
    for i, selected_object in enumerate(selected_objects):
        if i < len(selected_objects) - 1:
            remaining_objects = selected_objects[i + 1:]
            
            similar_object = find_similar_object(selected_object, remaining_objects, threshold_volume, threshold_center)
            
            if similar_object:
                similar_objects = [selected_object, similar_object]
                
                random_name = ''.join(random.choice('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ') for _ in range(10))
                
                for obj in similar_objects:
                    assign_new_name(obj, random_name)
                
                if has_high_vertex_count(similar_objects[0], similar_objects[1]) or \
                   has_bevel_modifier(similar_objects[0]) or \
                   has_subdivision_modifier(similar_objects[0]) or \
                   has_hp_material(similar_objects[0]):
                    assign_new_name(similar_objects[0], random_name + "_high")
                    assign_new_name(similar_objects[1], random_name + "_low")
                else:
                    assign_new_name(similar_objects[1], random_name + "_high")
                    assign_new_name(similar_objects[0], random_name + "_low")
    
    bpy.ops.object.select_all(action='DESELECT')
    for obj in selected_objects:
        obj.select_set(True)

if __name__ == "__main__":
    main()

Version Information:

  • Blender version 3.3.0
  • Addon 2.1.6

Script is supposed to auto-name selected meshes with a random name depending on which mesh is high poly and which is low poly in a group of meshes.

@mrgesy mrgesy added the bug Something isn't working label Aug 9, 2023
@RivinHD
Copy link
Owner

RivinHD commented Aug 9, 2023

Yes, looks like an issue of this add-on. Can confirm.

@RivinHD
Copy link
Owner

RivinHD commented Aug 9, 2023

The Problem is the last line:

if __name__ == "__main__":
    main()

replace it with

main()

and the script should work.
Apparently, the Add-on never sets the __name__ of the script to main.

@RivinHD
Copy link
Owner

RivinHD commented Aug 9, 2023

This will be fixed in the next version

@RivinHD RivinHD added this to the Version 2.2.0 milestone Aug 9, 2023
@RivinHD RivinHD mentioned this issue Sep 8, 2023
@RivinHD
Copy link
Owner

RivinHD commented Sep 8, 2023

Fixed in Version 2.2.0

@RivinHD RivinHD closed this as completed Sep 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants