Message 1 of 8
Groups distribution script. Issue
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hello. 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