How to create an array of objects with offset in python I am trying to create an array of cubes with python.


So far, I have:

class RunSimulation(bpy.types.Operator):
"""Run CrowdMaster simulation"""
bl_idname = "scene.cm_run_simulation"
bl_label = "Run Simulation"
bl_options = {'REGISTER', 'UNDO'}

def execute(self, context):
    scene = context.scene
    groupObjs = bpy.data.groups[scene.agentGroup].objects
    halfAgents = scene.agentNumber // 2

    for object in groupObjs:
        if scene.groundObject == object.name:
            self.report({'ERROR'}, "The ground object must not be in the same group as the agent!")

    bpy.context.scene.objects.active.select = False

    if scene.positionType == "formation":
        if scene.formationPositionType == "array":
            for a in range(halfAgents):
                obj1 = bpy.data.objects[groupObjs[1].name]
                ground =  bpy.data.objects[scene.groundObject]
                offset_x = (obj1.dimensions.x + scene.formationArrayX)
                obj1.select = True
                obj1 = obj1.copy()
                if scene.positionMode == "vector":
                    location = Vector((scene.positionVector[0], scene.positionVector[1], ground.location.z))
                elif scene.positionMode == "object":
                    objStart = bpy.data.objects[scene.positionObject]
                    location = Vector((objStart.location.x, objStart.location.y, ground.location.z))
                obj1.location = location
                scene.objects.link(obj1)
                location.x -= offset_x

    return {'FINISHED'}


But this gives me


The offset value is formationArrayX

How can I add that offset in between each item?
Right now, it just adds all the cubes in the same place. All of my code in multiple files is at https://github.com/johnroper100/CrowdMaster.
Currently your duplicating an object and moving it by -(2a + 1.5) units on the x axis. This doesn't give you a offset between the objects as objects are still exactly 2 units away of each other (because a changes by 1).

You should multiply formationArrayX with a, instead of adding it. To get something similar to the screenshot you posted, increase formationArrayX to something like 3. You also don't need offset2 then.

Also note, that your current code duplicates all objects you added before in each step. Only the last one created is offset, all others stay at the initial position. This is because the duplicate operator duplicates all selected objects and you don't un-select objects after translating

EDIT: This answer was only valid for an earlier version of the opening question. Code has been changed since then.