Random object array

Random object array

Anonymous
Not applicable
2,529 Views
27 Replies
Message 1 of 28

Random object array

Anonymous
Not applicable
Hi all,

I am a long time max user, but very new to scripting. I am trying to make a script that will take a simple object, and array it to form a complex structure. Is this possible / easy to do? I would like the end result to be a very random, complex structure. Any tips would help.

Thanks
0 Likes
2,530 Views
27 Replies
Replies (27)
Message 2 of 28

Steve_Curley
Mentor
Mentor
Copying an object to a random location is the easy bit - can you explain a bit more about the "complex structure"? The word "structure" implies some ordering - some logic to how the objects relate to each other. That is slightly at odds with being "random".

To create between 1 and 100 new objects whose Pivot Points lie inside a 1000*1000*1000 cube (roughly). Assume $Box01 is the original object to be copied.

for cnt = 0 to random 1 100 do
(
newObj = copy $Box01
newObj.pos =
)

If the final "shape" (volume, if you prefer) of the "complex object" is known, you could introduce limits so that any copies outside the volume are rejected. The more complex the final volume the more difficult it becomes to work out if a point is inside or outside that volume.

Max 2016 (SP1/EXT1)
Win7Pro x64 (SP1). i5-3570K @ 4.4GHz, 8Gb Ram, DX11.
nVidia GTX760 (2GB) (Driver 430.86).

0 Likes
Message 3 of 28

Anonymous
Not applicable
This is close to what I was getting at. I think I may have been a bit vague in my question.
Consider a structural system such as these ones:

http://www.aecbytes.com/newsletter/2008/issue_37-images/fig2.jpg

http://www.thecityreview.com/s08sasia29.jpg

Both systems have one type of common geometry that has been arrayed. I would like all the objects to still be connected once they have been arrayed.

Any ideas?
Thanks for your time.
0 Likes
Message 4 of 28

Steve_Curley
Mentor
Mentor
😮
I see what you mean. For the awning I think I would poly model it in such a way that there's a poly in each position where you want the object, then use Scatter. There's a tutorial for making a stepped pyramid like this somewhere - might be in the Tutorials section of this site.
As for attaching them - do mean literally attaching so they are all elements of a single Editable Poly (or mesh)? Or possibly kept independent, but all linked to a dummy so moving the dummy moves them all?

Pyramid Tut

Max 2016 (SP1/EXT1)
Win7Pro x64 (SP1). i5-3570K @ 4.4GHz, 8Gb Ram, DX11.
nVidia GTX760 (2GB) (Driver 430.86).

0 Likes
Message 5 of 28

Anonymous
Not applicable
No I didn't mean they they all have to be literally attached, I just meant that I would like them to be touching.
This is where I originally got this idea:

http://images.beijing2008.cn/20080112/Img214231013.jpg

I would like to make a similar architectural structure, but with a certain degree of randomness. Again I'm very new to these concepts so please bare with me.
:)

That tutorial is pretty close, however I would like the final form to be random. This tutorial is almost backwards to what I'm trying to do. I understand all of those concepts but have no clue on how I could reproduce that tutorial with a script.
0 Likes
Message 6 of 28

Steve_Curley
Mentor
Mentor
That's not going to be easy - I can't think of an easy way to script making such an object. Just making a flat plane of randomly sized pentagons would be tricky enough, but once you had that you could could bend it to the required shape, clone it and add a Lattice to the clone. That would give you something pretty close to the image.
I'm not convinced that using Scatter, even if scripted, is the right approach.

Need some input from those with more scripting experience, I think.

Max 2016 (SP1/EXT1)
Win7Pro x64 (SP1). i5-3570K @ 4.4GHz, 8Gb Ram, DX11.
nVidia GTX760 (2GB) (Driver 430.86).

0 Likes
Message 7 of 28

Anonymous
Not applicable
Ok I understood that this would be a bit more complex. Consider this then, would it be possible to make one pentagon and array them in such a way that they are still all touching and on a flat plane? They don't necessarily have to be varied in size.
0 Likes
Message 8 of 28

Anonymous
Not applicable
I'm not sure script is the best place to search to do that kind of stuff... looks like coding geometry fractals and all (and I've got no idea how to do that) !

I'd say take a bunch of hexagons (easier than pentagons), put them together by hand and clone them at will.
Attach them together, put a Noise on 2 axes.

After that convert to poly, weld all that mess and you got yourself something that kinda looks like your pattern that you can play with...

But that's not scripting so I guess it has nothing to do in this section !

Cheers!
0 Likes
Message 9 of 28

Anonymous
Not applicable
I've chosen scripting as a topic for a digital design course that I'm taking, so that option won't work for me. I probably could model this without any issues if I wasn't using a script, but that would defeat the purpose.
0 Likes
Message 10 of 28

Steve_Curley
Mentor
Mentor
I have a feeling (not in front of Max at the moment) that there's a section in the Maxscript help regarding creating a new "shape". Maybe that could be turned into making a new, parametric, object (like the N-Gon) then adding the ability to increase the number of pentagons - easier if they're regular ones (same length sides). Just thinking out loud here.

I would be very tempted to model it (roughly) - using the basic tools as much as possible, and make notes of exactly what I did. So it would involve creating an object (probably a Mesh at this stage), adding vertices at specific locations to create a single pentagon, creating the faces to give you a surface, then creating more pentagons which have edges in common with existing ones. You'll need a bit of maths to work out where the vertices have to go, you'll need to be able to work out which edges to use in order to extend it (by adding a touching pentagon) and so on. In other words try and script exactly what you would do manually.

Definitely not an easy task if you're just starting scripting.

Max 2016 (SP1/EXT1)
Win7Pro x64 (SP1). i5-3570K @ 4.4GHz, 8Gb Ram, DX11.
nVidia GTX760 (2GB) (Driver 430.86).

0 Likes
Message 11 of 28

Anonymous
Not applicable
Thanks for the reply Steve. If I take a step back here...how could I go about making a hexagon through a script, and then copy multiples of that hexagon so that they are all still touching at the edges. I think if I can just get some insight on these easier steps, then maybe eventually I'll be able to work a series of connected hexagons into a random, complex form. Any thoughts are appreciated.
0 Likes
Message 12 of 28

Anonymous
Not applicable
i suggest u to have a look at mathsurf script which generates procedural surfaces.
0 Likes
Message 13 of 28

Steve_Curley
Mentor
Mentor

(
fn IsOdd val =
(
if ((mod val 2) != 0.0) then
true
else
false
)

xCount = 7
yCount = 6
rad = 25

for col = 1 to xCount do
(
if (IsOdd col) then
yOffset = 0
else
yOffset = rad

for row = 1 to yCount do
(
yPos = ((row - 1) * rad * 2) + yOffset
xPos = (col - 1) * 1.7321 * rad
nGon name:(uniquename "Hex")\
nSides:6 radius:rad scribe:0\
position:
)
)
)

Max 2016 (SP1/EXT1)
Win7Pro x64 (SP1). i5-3570K @ 4.4GHz, 8Gb Ram, DX11.
nVidia GTX760 (2GB) (Driver 430.86).

0 Likes
Message 14 of 28

keithm
Enthusiast
Enthusiast
Ohhh.. that roof is cool...

My first thought for building that "random honeycomb roof" would be to have take a defined area and array random points which are in that area which have a min and max distance from other points in the array. That max and min distance represent your max and min hexagon size. Consider the space filled after X amount of failed attempts to place a point based on your max and min.
I would then sort the array by distance from the center of the field. See "distance" command in maxscript. Bubble sort it or better yet figure out using the "qsort" function.
Then working in 2D grow your octagons from your min to max value until they intersect another octagon. Then build some 3D on that...

I could be fun to just build that field of hexagons in script. Add them to a solid object group and "drop" them using reactor into the container that would make then stack on edge... leave the hard math and intersections to reactor.

The other option would be to do an edge walk, in which you align an octagon to each side of an existing Octagon and once again starting with a small one grow until you reach a failed state of intersection or reach max size. Then recurse into the new octagons and do the same thing...

Definitely a fun problem.
0 Likes
Message 15 of 28

Steve_Curley
Mentor
Mentor
I was thinking:-
Modify the script above to create 1 Hexagon, convert to EP and use that as a temporary node from which to make copies which can be all be attached together, then weld the coincident vertices, then randomise them (within limits).

Remember that Cogliati is (in his own words) "a long time max user, but very new to scripting" - I suspect that some of the maths (algorithms and their underlying concepts) needed may be a bit too advanced at this stage, though I can see where you're going with that and it may well produce a "better" final result - one closer to that in the image. That's why I tried to keep it fairly simple as a "first pass" attempt.

I'll have a go at "pass 2" when I get a free moment or 3.

Sure is an interesting problem though :)

Max 2016 (SP1/EXT1)
Win7Pro x64 (SP1). i5-3570K @ 4.4GHz, 8Gb Ram, DX11.
nVidia GTX760 (2GB) (Driver 430.86).

0 Likes
Message 16 of 28

Anonymous
Not applicable
That is a great starting point for me Steve, thanks so much for the help. I'll be interested to see what your second pass looks like, I think I am starting to understand some of the language of the scripts that you have provided.

Kram, any chance you could make a small sample of the idea that your talking about? I think I understand what you are saying, but would have no clue how to go about putting that into script.
0 Likes
Message 17 of 28

Steve_Curley
Mentor
Mentor
Pass 2.

( -- start of the script
-- declare a function to determine if the pass-in value "val" is odd or even
fn IsOdd val =
(
if ((mod val 2) != 0.0) then
true
else
false
)

-- declare some local constants
-- these can be changed to suit your needs
local xCount = 7, yCount = 6 -- how many columns and rows do we want
local rad = 25 -- the Radius of the intial hexagon we create
local maxRndOffset = 10 -- used for randomising the vertices. vertices moved between +/- this value
local baseObject -- variable to hold that initial hexagon
local col, row, yOffset, tmp, ourObject, vertnum, firstTime = true -- remaining variables we need

for col = 1 to xCount do -- outer loop, from left to right, columns
(
if IsOdd col then -- the hexagons in odd columns are positioned (rad * 2) * the row number -1
yOffset = 0
else -- those in the even columns are offset from those in the odd columns by 1 * rad
yOffset = rad

for row = 1 to yCount do -- inner loop, from front to back, rows
(
if firstTime then -- first time we have got to this point
(
-- create the baseObject we will be copying later
-- circumscribed hexagon. radius is to the flat sides, inscribed would be to the points
baseObject = nGon name:"Hex" \ -- constructors like this can only span lines
nSides:6 radius:rad scribe:0 \ -- if you add a backslash at the end of each line
position:
convertToPoly baseObject -- converts the shape to an editable poly object
) -- end of the if-then statement
tmp = copy baseObject -- make a duplicate
yPos = ((row - 1) * rad * 2) + yOffset -- calculate the position where the new object needs to be located
xPos = (col - 1) * 1.7321 * rad -- 1.7321 is the square root of 3, don't ask - I had to google it :o '
tmp.pos = -- set the position
if firstTime then
(
ourObject = tmp -- this is the beginning of our final object
firstTime = false -- must not do this again
)
else
polyop.attach ourObject tmp -- attach the new one to our final object
) -- end of the inner (row) loop
) -- end of the outer (col) loop

delete baseObject -- we don't need it any more '

-- now to weld all those duplicated vertices
ourObject.weldThreshold = 0.1 -- make sure the weld threshold is low so we don't weld too much
polyop.weldVertsByThreshold ourObject #All -- #all is equivalent to creating a list (array) of all the vertices in the object

-- now to randomise the positions of all the vertices
for vertNum = 1 to (polyop.getNumVerts ourObject) do -- the "to" must evaluate to a single valuem hence the ( )
(
polyop.moveVert ourObject #(vertNum) \ -- vertex number as a list

) -- end of the for loop

) -- end of the script


Max 2016 (SP1/EXT1)
Win7Pro x64 (SP1). i5-3570K @ 4.4GHz, 8Gb Ram, DX11.
nVidia GTX760 (2GB) (Driver 430.86).

0 Likes
Message 18 of 28

Anonymous
Not applicable
Wow that is excellent, I think I actually understand most of the script. Now my next question, how could that flat plane of hexagons morph into a more architectural form? Something along these lines...

http://www.building.co.uk/Pictures/web/f/n/m/Zaha_Cirrus_mn.jpg

Don't focus too much on the exact form shown, but rather the abstract quality of the form.

Or maybe a better question....would it be possible make a random "blob" or form within the same script, and then conform the hexagon plane to that "blob"?
0 Likes
Message 19 of 28

Steve_Curley
Mentor
Mentor
A "random blob" eh? Bit like an Asteroid?
There's a tutorial on this in Max 9, but not in later versions. Basically create a sphere and apply a Noise Modifier, tweak the settings until it looks good. That shouldn't be too difficult to script. I think the blob will need to be slightly larger than the Hexagons.
Then you'll need the Hexagons - a lot more than in the example script above.
Then you'll need to Conform the Hexagons to the blob. This will mean working out how to use the Conform from a script (which I don't know, but I'll have a look), and the blob will need to be positioned below the Hexagons - the pivot of the hexagons is at so you'll need to find the centre of them (equivalent to affect pivot only | centre to object).

I would have a play with it all in Max manually to determine the kind of values you will need to put into the script in terms of overall size, number of Hexagons, Noise Modifier values and so on.

Max 2016 (SP1/EXT1)
Win7Pro x64 (SP1). i5-3570K @ 4.4GHz, 8Gb Ram, DX11.
nVidia GTX760 (2GB) (Driver 430.86).

0 Likes
Message 20 of 28

Anonymous
Not applicable
Maybe blob was the wrong terminology, essentially I'm just trying to get a random form out of this. I will try and play with the noise modifier and a sphere to see what I can achieve. In order to figure out how to script the noise modified sphere, should I be loooking at the macro recorder?


One other question, is there a way to understand the way that Max randomises numbers? In the first script that you wrote for me in this thread (the one with the box array) you added the word "random" to the scriptl. It would be interesting if I could understand the math behind this built in function, in order to gain full control over my script.
0 Likes