Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
[MaxScript] Object Distributor
/***********************************************************************************
NAME/ em_Distributor
AUTHORS/ Mhamed Elmezoued
VERSION/ 1.0
DESCRIPTION/
Allow to copy object on other object
INSTALL/ Just punch it
LASTUPDATE/ 02.05.2013
CHANGELOG/
KNOW BUGS AND ISSUES/
Copyright (c) 2016 Mhamed Elmezoued (mhamed.e@gmail.com)
***********************************************************************************/
try(DestroyDialog rDistributor)catch()
/*VARIABLES*/
vObjSourceList = #()
vObjDestList = #()
/*CONSTANTES*/
CONST_LISTBOX_ERROR="ERROR"
CONST_LISTBOX_DUPNAMEOBJ="Duplicate name :"
CONST_LAYERNAME = "_DISTRIBUTOR"
rollout rDistributor "em_Distributor" width:215 height:435
(
/*UI*/
Group "Source List"
(
multilistbox mlbx_sourcelist "" width:190 height:5
button btn_addsobj "+" width:60 height:25 align:#left across:3 toolTip:""
button btn_rmvsobj "-" width:60 height:25 toolTip:""
button btn_clearslist "CLEAR" width:60 height:25
)
Group "Destination List"
(
multilistbox mlbx_destlist "" width:190 height:5
button btn_adddobj "+" width:60 height:25 align:#left across:3 toolTip:""
button btn_rmvdobj "-" width:60 height:25 toolTip:""
button btn_cleardlist "CLEAR" width:60 height:25
)
Group "Parameters"
(
radiobuttons rb_dupli "" labels:#("Instance", "Copy") default:1 columns:2 align:#left
checkbox chkbox_scale "Scale" checked:true align:#left across:3
checkbox chkbox_rotation "Rotation" checked:true
checkbox chkbox_position "Position" checked:true
checkbox chkbox_random "Randomize (?)" checked:true toolTip:"If not checked, the distributor take the first objet in the source list"
)
Group "Process"
(
button btn_proceed "DISTRIBUTION" width:180 height:50
)
/*FUNCTION*/
fn fUpdateSourceList =
(
if vObjSourceList != undefined do
mlbx_sourcelist.items = (for o in vObjSourceList collect o.name)
)
fn fUpdateDestList =
(
if vObjDestList != undefined do
mlbx_destlist.items = (for o in vObjDestList collect o.name)
)
/*EVENT*/
on btn_addsobj pressed do
(
vDuplicateName=#()
vErrorMessage=""
vObjListTemp = sort(for i in selection collect i.name)
for i=1 to vObjListTemp.count do
(
vCurrentObj = getnodebyname (vObjListTemp[i] as string) exact:true
if not appendIfUnique vObjSourceList vCurrentObj then
appendIfUnique vDuplicateName vCurrentObj.name
)
if vDuplicateName.count!=0 do
(
vErrorMessage+="\n"+CONST_LISTBOX_DUPNAMEOBJ+"\n"
for i in vDuplicateName do vErrorMessage+=(i as string + "\n")
)
if vErrorMessage!="" then MessageBox vErrorMessage title:CONST_LISTBOX_ERROR
free vDuplicateName
free vObjListTemp
fUpdateSourceList()
deselect $*
)
on btn_rmvsobj pressed do
(
local vCurrSel = mlbx_sourcelist.selection
for i = mlbx_sourcelist.items.count to 1 by -1 where vCurrSel[i] do (deleteItem vObjSourceList i)
fUpdateSourceList()
)
on btn_clearslist pressed do
(
vObjSourceList=#()
fUpdateSourceList()
)
on btn_adddobj pressed do
(
vDuplicateName=#()
vErrorMessage=""
vObjListTemp = sort(for i in selection collect i.name)
for i=1 to vObjListTemp.count do
(
vCurrentObj = getnodebyname (vObjListTemp[i] as string) exact:true
if not appendIfUnique vObjDestList vCurrentObj then
appendIfUnique vDuplicateName vCurrentObj.name
)
if vDuplicateName.count!=0 do
(
vErrorMessage+="\n"+CONST_LISTBOX_DUPNAMEOBJ+"\n"
for i in vDuplicateName do vErrorMessage+=(i as string + "\n")
)
if vErrorMessage!="" then MessageBox vErrorMessage title:CONST_LISTBOX_ERROR
free vDuplicateName
free vObjListTemp
fUpdateDestList()
deselect $*
)
on btn_rmvdobj pressed do
(
local vCurrSel = mlbx_destlist.selection
for i = mlbx_destlist.items.count to 1 by -1 where vCurrSel[i] do (deleteItem vObjDestList i)
fUpdateDestList()
)
on btn_cleardlist pressed do
(
vObjDestList=#()
fUpdateDestList()
)
on btn_proceed pressed do
(
if (vObjSourceList.count!=0 AND vObjDestList.count!=0) then
(
undo "em_Distributor" on
(
vNewLayer = LayerManager.getLayerFromName CONST_LAYERNAME
if vNewLayer == undefined then vNewLayer = LayerManager.newLayerFromName CONST_LAYERNAME
with redraw off
for objdest in vObjDestList do
(
if chkbox_random.checked then
(
nb_rand=random 1 vObjSourceList.count
objsource=vObjSourceList[nb_rand]
)
else
objsource=vObjSourceList[1]
if rb_dupli.state==1 then
tmp_obj=instance objsource
else
tmp_obj=copy objsource
if chkbox_scale.checked then
tmp_obj.scale=objdest.scale
if chkbox_rotation.checked then
tmp_obj.rotation=objdest.rotation
if chkbox_position.checked then
tmp_obj.pos=objdest.pos
tmp_obj.parent=objdest
vNewLayer.addnode tmp_obj
)
)
)
)
)
createDialog rDistributor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment