View Full Version : Path Constraint Randomly Placed Objects
10-13-2009, 10:20 PM
I am looking to make a script that will randomly place all my selected objects onto a path without placing any on top of each other.
I am able to grab a bit of code from the maxscript listener but I am unsure as to where to go from here.
select $'rpc_Ford Focus07'
sliderTime = 2070f
$.pos.controller.Path_Constraint.controller.percent = 49.9
I am not much of a maxscripter so I am not sure how to write code for this.
I would need to create an array of random numbers (1-100% on the path) for the amount of objects selected, then make sure no duplicate numbers are generated. At this point I would select the path I would want to constrain the objects to and the script would place the objects on the path at the percentages in the array.
Can anyone give me a hand with this? Is this possible? Did I explain it clear enough?
Thanks for your help!
10-14-2009, 10:04 AM
here is my quick try on it:
if selection.count > 1 and isKindOf $ shape then (
local pathObj = $
local pathLength = curveLength pathObj
local distObjs = for i = 2 to selection.count collect selection[i]
fn getObjDiameter obj =
local bb = nodeLocalBoundingBox obj
(distance bb bb) / (sqrt 3)
local distObjsDiameter = for o in distObjs collect getObjDiameter o
for i = 1 to distObjs.count do (
local pc = path_constraint()
pc.constraints.appendTarget pathObj 100
pc.follow = true
pc.constantVel = true
local dist = distObjsDiameter[i] / 2
if i > 1 then
dist += (distObjsDiameter[i - 1] / 2) + pathLength * (distObjs[i - 1].pos.controller.percent / 100)
local maxRand = pathLength - dist
if i < distObjs.count then (
for j = i + 1 to distObjs.count do
maxRand -= distObjsDiameter[j]
maxRand /= (distObjs.count - i + 1)
dist += random (maxRand / 2) maxRand
pc.percent = 100 * dist / pathLength
distObjs[i].pos.controller = pc
the code is not so elegant but it kind of does the job.
simply select the path first and then the objects you want to distribute on it and evaluate the script.
10-14-2009, 01:54 PM
No worries on the code not being elegant. Not so sure I would know what elegant code looks like if it bite me on my nose! :surprised
Thanks much for taking a stab at this. I shall give this a try when I get a chance.
10-14-2009, 03:03 PM
Guess I got to checking it sooner than I thought.
So the script lays the objects out randomly along the beginning of the path well.
Unfortunately it clusters them together and always places them at the beginning of the path. An interesting effect occurs when I move the timeline down the path and run the script. It will still place the objects at the beginning of the path. So if I have a timeline from 0-100 and have the slider at 50, run the script, then scrub back to frame 0 the objects will then be 50% down the path.
Wondering if the script could be tweaked to space out the objects much more than what is going on now. Something more like the screen shot below.
Thanks again for your help! :bowdown:
10-14-2009, 03:23 PM
I updated the script above,
let me know if this works better for you..
10-14-2009, 04:20 PM
Amazing! That worked wonders.
Thanks much for the help. :bowdown:
10-14-2009, 04:20 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.