Groups distribution script. Issue

Groups distribution script. Issue

suhiros
Explorer Explorer
890 Views
7 Replies
Message 1 of 8

Groups distribution script. Issue

suhiros
Explorer
Explorer

sript.pngHello. I'm trying to write a script using AI as I'm not good in this. Almost all goes good except 1 thing. 

So the script works like this. You select the objects, add them to the list. Then instancies of slelected objects are distributed by polygons of another object. Everything works great with single objects. But when I need to distribute a group of objects it ditributes a group as a dummy, so no objects inside. Can someone help me to fix it please? 

 

 

 

rollout Placer "Placer" width:200 height:500 -- Define a rollout for the window
(
	local myArray = #() -- Array to hold the names of the objects
	
	
	-- UI Elements
	dotNetControl spacertop1 "Label" text:"" height:6 width:0
    button addObjectButton "Add selected" across:2 -- Button to add selected objects to the list
    button btn_remove "Remove selected" -- Button to remove selected item from the list box
	button btn_clear"Clear list" width:150 -- Button to clear the objectListBox
    listbox objectListBox "Objects" -- List of objects
    button addPlaceOnObjectButton "Add place on object" width:150 -- Button to add place on object
    button distributeButton "Distribute" width:150 -- Button to distribute objects

	
	-- Add Button Event
    on addObjectButton pressed do
    (
        for obj in selection do
        (
            append myArray obj.name -- Add the name of the object to the array
        )
        objectListBox.items = myArray -- Update the listbox
    )

    -- Remove Button Event
    on btn_remove pressed do
    (
        if objectListBox.selection != undefined do -- Check if an item is selected
        (
            deleteItem myArray objectListBox.selection -- Remove the selected item from the array
            objectListBox.items = myArray -- Update the listbox
        )
    )
	
	-- Clear Button Event
    on btn_clear pressed do
    (
        myArray = #() -- Clear the array
        objectListBox.items = myArray -- Update the listbox
    )
	
    -- Event handler for the "Add place on object" button click
    on addPlaceOnObjectButton pressed do
    (
        addPlaceOnObjectButton.text = "Select Object..."
        local selectedObj = pickObject message:"Select an object to add place on" -- Allow user to select an object
        if selectedObj != undefined do
        (
            addPlaceOnObjectButton.text = selectedObj.name -- Replace button label with selected object name
        )
    )
	
    -- Function to update the list box with selected objects
    fn updateObjectList =
    (
        objectListBox.items = for obj in selection collect obj.name
    )

    -- 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
                    local instanceObj = instance obj -- 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
                        local instanceObj = instance randomObj -- 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
                    )
                )
            )
        )
    )
)

createDialog Placer -- Create the dialog with specified dimensions

 

 

 

0 Likes
891 Views
7 Replies
Replies (7)
Message 2 of 8

denisT.MaxDoctor
Advisor
Advisor

Are you asking to fix code written by AI?

 
0 Likes
Message 3 of 8

suhiros
Explorer
Explorer

yes 😊

 

At least may be some hint where is a problem

0 Likes
Message 4 of 8

denisT.MaxDoctor
Advisor
Advisor

@suhiros wrote:

yes 😊

 

At least may be some hint where is a problem


denisTMaxDoctor_0-1711308019799.png

 

0 Likes
Message 5 of 8

suhiros
Explorer
Explorer

Thank you so much. Sorry for bothering, but I need the group to copied as instance. Is it a problem for script functionality?

0 Likes
Message 6 of 8

denisT.MaxDoctor
Advisor
Advisor

@suhiros wrote:

Thank you so much. Sorry for bothering, but I need the group to copied as instance. Is it a problem for script functionality?


Just tell the AI that a NI (natural intelligence) told it to think of an alternative way to "instance".

Message 7 of 8

suhiros
Explorer
Explorer

Hi again. 

 

AI didn't help me, but I found another way to make an instance with "maxOps.cloneNodes"  I checked it on a simple action and it works with groups. I have replaced "local instanceObj = instance obj" With this "local instanceObj = maxOps.cloneNodes obj cloneType:#instance". But I'm getting error. Unknown property "Position" in true. 

Could you give an advise? Am I on the right way or gpoing wrong? 

 

    -- 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
                    local instanceObj = maxOps.cloneNodes obj cloneType:#instance -- 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
                        local instanceObj = maxOps.cloneNodes randomObj cloneType:#instance -- 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
                    )
                )
            )
        )
    )

I  

0 Likes
Message 8 of 8

suhiros
Explorer
Explorer

Ok, I finally found correct solution to instance groups. I just should use this. 

maxOps.cloneNodes  cloneType:#instance newNodes:&instanceObj

 But now I have a problem that all children of the group are placed in one point, and when I try to ditribute them on different polygons they fly away on a random locations outside the polygons, while single objects distribution works well

 

0 Likes