IToo slow to make cubes

Hi.
This is my code. I want to make may cubes.

I input data to move object, and when object move, cube make in real time position. so I want to make a lots of cube. But It is very slow to make cubes. you can ignore xml_maker… Thank you

I think problem line is…

        if bone_location:
            self.create_cube(bone_location)
``` import bpy import os import sys

current_dir = os.path.dirname(bpy.data.filepath)
sys.path.append(current_dir)
import xml_maker

class Maker:
def init(self):
self.count = 0
self.clear_cubes()

    self.obj_x = bpy.data.objects.get("x")
    self.obj_y = bpy.data.objects.get("y")
    self.obj_z = bpy.data.objects.get("z")

    self.xml_list = []
    self.initial_state()

def clear_cubes(self):
    """Remove all cubes from the scene."""
    for obj in bpy.data.objects:
        if obj.name.startswith("Cube"):
            bpy.data.objects.remove(obj, do_unlink=True)

def initial_state(self):
    """Reset the objects to their initial positions."""
    if self.obj_x: self.obj_x.location.x = 0
    if self.obj_y: self.obj_y.location.y = 0
    if self.obj_z: self.obj_z.location.z = 0

    bpy.context.view_layer.update()
    bpy.ops.wm.redraw_timer(type='DRAW_WIN_SWAP', iterations=1)

def create_cube(self, location):
    """Create a cube at the specified location."""
    bpy.ops.mesh.primitive_cube_add(location=location, size=10)
    cube = bpy.context.object
    material = bpy.data.materials.new(name="Cube Material")
    material.diffuse_color = (1.0, 0.0, 1.0, 1.0)
    cube.data.materials.append(material)
    print(f"Cube created at: {location}")

def get_xyz(self, x, y, z):
    """Store the XYZ coordinates in the XML list."""
    data = {'x': x, 'y': y, 'z': z}
    self.xml_list.append(data)
    return data

def get_bone_location(self):
    """Retrieve the world location of the bone's tail."""
    arm_name = "ik_tip"
    bone_name = "Bone"

    arm = bpy.data.objects.get(arm_name)
    if not arm or arm.type != 'ARMATURE':
        print(f"Armature '{arm_name}' not found.")
        return None

    pose_bone = arm.pose.bones.get(bone_name)
    if not pose_bone:
        print(f"Bone '{bone_name}' not found.")
        return None

    world_tail = arm.matrix_world @ pose_bone.tail
    return world_tail.x, world_tail.y, world_tail.z

def move_and_create_cubes(self):
    """Move the object and create cubes based on user input."""
    try:
        step = float(input("Step: "))
        if step == 0:
            raise ValueError("Step cannot be 0.")

        axis = input("Select Axis (x, y, z): ").lower()
        if axis not in ["x", "y", "z"]:
            raise ValueError("Invalid axis. Choose from 'x', 'y', 'z'.")

        goal = float(input(f"Target {axis}-coordinate: "))
    except ValueError as e:
        print(e)
        return

    print(f"Moving along {axis}-axis with step {step} towards {goal}.")
    self.count += 1

    start_coords = {
        'x': self.obj_x.location.x if self.obj_x else 0,
        'y': self.obj_y.location.y if self.obj_y else 0,
        'z': self.obj_z.location.z if self.obj_z else 0
    }

    current = start_coords[axis]
    while (step > 0 and current <= goal) or (step < 0 and current >= goal):
        if axis == "x" and self.obj_x:
            self.obj_x.location.x = current
        elif axis == "y" and self.obj_y:
            self.obj_y.location.y = current
        elif axis == "z" and self.obj_z:
            self.obj_z.location.z = current

        self.get_xyz(self.obj_x.location.x, self.obj_y.location.y, self.obj_z.location.z)

        bone_location = self.get_bone_location()
        if bone_location:
            self.create_cube(bone_location)

bpy.context.view_layer.update()

        current += step

    print(f"Movement along {axis}-axis complete.")

def save_xml(self):
    """Save the object's movement data to an XML file."""
    xml_maker.xml_creator(self.xml_list)
    print("Gantry locations saved to XML.")

def main():
ml = Maker()

while True:
    user_input = input("Move the gantry? (y/n): ").strip().lower()
    if user_input == "y":
        ml.move_and_create_cubes()
        bpy.context.view_layer.update()
    elif user_input == "n":
        ml.save_xml()
        print("Exiting program.")
        break
    else:
        print("Invalid input. Please enter 'y' or 'n'.")

if name == “main”:
main()

  1. Please format code like in the example below to keep it readable.

  1. What’s the problem with the code, what exactly is slow? Please narrow it down further to a simpler script to show which part is slow.

This is most definitely going to cause an infinite loop or at least make your script stuck after the user_input line, so I’m not surprised your script hangs. If i understood your script correctly you want to look up modal operators.