[Closed] CgTalk Maxscript Challenge 012: "Corner Placer"
[b]CgTalk Maxscript Challenge 012: “Corner Placer”
[/b]
DESCRIPTION: Create a tool that allows a user to clone and align objects to the 8 bounding corners of a group or mesh.
INTERMEDIATE SCRIPTERS: Add options for aligning the objects to point in different directions: x,y,z and corners.
ADVANCED SCRIPTERS: Add an offset option and the ability to align the objects to the center of the "faces" of the bounding box.
DEADLINE: 10/11/2006
RULES:
[ul]
[li]Code from scratch. Try not to use pre-exisitng functions or plugins.[/li][li]Show your script references, if any (eg. Looking at another script to assist you).[/li][li]You are encouraged to ask for help where needed, but try to do it on your own. The maxscript reference is an invaluable resource.[/li][li]Post your final script inside [/li]“`
tags (located on your posting toolbar).
[li]Post all code into THIS thread.[/li][li]Post the max version you coded in, plus any maxscript extensions you used. (Thanks galagast!)[/li][li]Try to finish the challenge in a week. There is no definite time limit.[/li][/ul]NOTES: Sorry for the long hiatus! A fairly simple one to get started again.
Here is my entry, I think it fulfil’s all the requirements for the advanced category, tell me what you guys think.
(
rollout roBoundingCorners "12: Bounding Corners"
(
local theObj
group "Affect"
(
radiobuttons rdoScene "" labels:#("Selection", "Scene") columns:2 default:1
pickbutton btnPickObject "Pick Object" width:134 height:16
)
group "Place"
(
checkbox chkCorners "Corners" across:2 checked:true
checkbox chkFaces "Faces" checked:true
)
group "Align"
(
radiobuttons rdoAlign "" labels:#("N", "X", "Y", "Z") columns:4 default:1
checkbox chkFlipAxis "Flip Axis" width:134 height:16
)
group "Offset"
(
spinner spnX "X:" height:16 range:[-10e10, 10e10, 0]
spinner spnY "Y:" height:16 range:[-10e10, 10e10, 0]
spinner spnZ "Z:" height:16 range:[-10e10, 10e10, 0]
checkbox chkUniform "Uniform" height:16
)
group "Copy Type"
(
radiobuttons rdoCopy "" labels:#("Copy", "Instance", "Reference") columns:2 default:1
)
button btnBoundingCorners "Bounding Corners" width:153 height:16
label lblWahooney2006 "Wahooney 2006" align:#right height:16
fn BoundAlignObjects objs obj corners faces copyType flip:false alignTo:1 offset:[0,0,0] =
(
local max_p = objs.max + offset
local min_p = objs.min - offset
local cnt_p = (max_p + min_p) / 2.0
local arrPts = #()
local arrObjs = #()
local c
if corners then
(
for i = 1 to 8 do
(
p = copy max_p
if (bit.get (i-1) 1) then p.x = min_p.x
if (bit.get (i-1) 2) then p.y = min_p.y
if (bit.get (i-1) 3) then p.z = min_p.z
append arrPts p
)
)
if faces then
(
for i = 1 to 6 do
(
p = copy cnt_p
case (mod i 3) of
(
1:(p.x = if i <= 3 then min_p.x else max_p.x)
2:(p.y = if i <= 3 then min_p.y else max_p.y)
0:(p.z = if i <= 3 then min_p.z else max_p.z)
)
append arrPts p
)
)
for p in arrPts do
(
if copyType == 1 then
c = copy obj
else if copyType == 2 then
c = instance obj
else if copyType == 3 then
c = reference obj
c.pos = p
c.dir = case alignTo of
(
1: (normalize (c.pos - cnt_p))
2: ([1,0,0])
3: ([0,1,0])
4: ([0,0,1])
)
if flip then c.dir *= -1
append arrObjs c
)
arrObjs
)
on btnPickObject picked obj do
theObj = obj
on spnX changed val do
(
if chkUniform.checked then spnY.value = spnZ.value = val
)
on chkUniform changed val do
(
if val then
spnY.value = spnZ.value = spnX.value
spnY.enabled = spnZ.enabled = not val
)
on btnBoundingCorners pressed do
(
if theObj != undefined and not isDeleted theObj then
select (BoundAlignObjects (if rdoScene.state == 1 then selection else objects) (theObj) chkCorners.checked chkFaces.checked rdoCopy.state flip:chkFlipAxis.checked alignTo:rdoAlign.state offset:[spnX.value, spnY.value, spnZ.value])
else
messageBox "Please pick an object first"
)
)
createDialog roBoundingCorners style:#(#style_titlebar, #style_border, #style_sysmenu, #style_toolwindow)
)
Version: Max 8
Working time: +/-2 Hours
Tools Used: Rollout Army Knife 2.0 BETA
Reference: Maxscript Reference 8.0
Brilliant work! Very simple to understand and use. Nice simple calculations too!
I’m going to have to challenge you harder I think Keith!
Keep them coming everyone! Try to figure it out for yourself before scanning over the work submitted here. The main point of the challenges is to stretch your brain into new ideas and efficient methods.
Thanks erilaz.
This wasn’t as hard as I thought it would be and I sure as hell wasn’t going to figure out 8 combinations of coords when I could just use some good old binary math
wahooney,
I like it.
Maybe you can explain about the “good old binary math” ?
Georg
Useful things to consider first:
There are 8 corners to a cube.
8 combinations in a binary number with 3 bits.
There are 3 floats in a point 3.
So instead of writing the whole lot of iterations out:
loop 1 to 8
if bit 1 is set use the minimum x else use the maximum
if bit 2 is set use the minimum y else use the maximum
if bit 3 is set use the minimum z else use the maximum
There you have your 8 corners.