Distributing group instancies

Hello. I'm making a script which should distribute a group of objects by the polygons of another object. But I'm getting an issue. All the objects in the group are placed at the same point, while I would like them to behave like a single object.

Is there a way to fix it somehow? 

   -- Event handler for the "Distribute" button click
    on distributeButton pressed do
    (
        local targetObject = getNodeByName(addPlaceOnObjectButton.text)
        if targetObject != undefined do
        (
            local selectedObjects = for objName in objectListBox.items collect getNodeByName(objName)
            local numPolygons = polyOp.getNumFaces targetObject
            local numObjects = selectedObjects.count
            if numPolygons >= numObjects do -- Ensure there are enough polygons
            (
                for i = 1 to numObjects do
                (
                    local obj = selectedObjects[i]
                    local polygonIndex = i
                    if polygonIndex > numPolygons do
                        polygonIndex = numPolygons -- Avoid index out of range
                    local polygonCenter = polyOp.getFaceCenter targetObject polygonIndex
                    local polygonNormal = polyOp.getFaceNormal targetObject polygonIndex
                    maxOps.cloneNodes obj cloneType:#instance newNodes:&instanceObj -- Create an instance of the object
                    instanceObj.position = polygonCenter -- Place the instance on the center of the polygon
                    instanceObj.dir = polygonNormal -- Align instance's local Y-axis with the polygon's normal
                )
 
                -- If the number of objects is less than the number of polygons, create additional instances
                if numPolygons > numObjects do
                (
                    for j = (numObjects + 1) to numPolygons do
                    (
                        local randomIndex = random 1 objectListBox.items.count -- Generate a random index
                        local randomObjName = objectListBox.items[randomIndex] -- Get a random object name from the list
                        local randomObj = getNodeByName(randomObjName) -- Get the corresponding object
                        local polygonCenter = polyOp.getFaceCenter targetObject j
                        local polygonNormal = polyOp.getFaceNormal targetObject j
                        maxOps.cloneNodes randomObj  cloneType:#instance newNodes:&instanceObj -- Create an instance of the random object
                        instanceObj.position = polygonCenter -- Place the instance on the center of the polygon
                        instanceObj.dir = polygonNormal -- Align instance's local Y-axis with the polygon's normal
                    )
                )
            )
        )
    )

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
SimonBourgeois's picture

Hi

it's because "instanceObj.position = polygonCenter and instanceObj.dir = polygonNormal" are applied to all objects within the groups, you need to filter out the actual objects and use only the groupheads (the pink dummys), you can use isgrouphead or isOpenGroupHead to add only the groupheads to the objectListBox.items when objects are part of a group or to filter out objects that aren't groupheads from instanceObj array .

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.