[Closed] My first try at maxscript and need help!
Yeah…i too was thinking around the way u had defined the variable…but i was more digging around “where” to define the variable rather than it being global or local and u came with the solution.
This leaves me with a question though!!Other than global being accesible from everywhere and local within the scope of the handler or whereever its defined, are there any other goods or bads with these??
brgds
sandy
for that answer, Sandy i will point you to the MXShelp file- Local and Global Variables
Ok…i did read that and i understand locals and globals …but when i saw ur last script u posted here…i thought why not define each variable as global in the begining and access it the way u want in the script…why do we need locals !!!
i may sound stupid here…but thats the reason i asked…
brgds
sandy
First Read in Maxscript Reference: “Scope of Variables”
global variables can cause problems. When you start programming alot, you start to develop your own style, I for instance always name my rollouts/buttons/ui elements the same way
3 letter abbr + “_” + name so
button btn_Changed
checkbox chk_InstanceModeOn
rollout rlt_Main
you will also start naming variables the same way
alwaysOn
isDrivingFunction
canBeFrozen
etc.
Global variables persist through scene changes, etc. and Persisitant Global variables persist through exiting and restarting max
Almost all programming languages have local (sometimes called “private”) scoping. (In fact I can’t think of one that doesn’t, but there might be one!) You want to keep your code as clean and neat as possible. and that being the case, keeping your globals to an absolutely minimum is more than desirable.
To answer your question why we need them is because most of the time it just isn’t necessary! Why would you want to access a variable anywhere in the code (for which memory is allocated) that you are just using once for some silly temporary string or temp_array, you don’t. And to keep confusion down in your code you can define the temp_string or temp_array for instance
The rule is that every variable should be local unless it’s needed to be global.
I wish I could give you the longer answer, but I don’t know it!
Hope this helps,
Colin
I got ur point buddy…working on it…this memory thing was new to me though!
I’ve just covered up the basics and now started with this script …at the end i want to make a useful bunch of code with some good learning…
How did u guys got started and how much time u took to not to go basics again and again!
Cheers
sandy
All good code is self-contained and re-useable It’s all about the basics.
-Colin
Got that and Hi Once again
So this is my understanding of intersectRay function
I’ve to define two variables which hold the respective Z values of Master and child objects and intersectRay function is then used to make this happen.
I got a lot of help from the max help file on this issue and this is what i could figure out.
Local variable ChildRay = A ray which starts from whatever object is there in selectmult variable along the -Z axis
Local ChildRay = ray SelectMult.object.pos [0,0,-1]
Local variable MasterZ = maximum Z coordinates of the master object’s surface
Local MasterZ = btnPMastO.object.max.z
Directing the childray to the maximum of MasterZ Z axis with an offset of .0001
ChildRay.pos.z = MasterZ + 0.0001 * abs MasterZ
this will make the intersection happen
intersectRay btnPMastO ChildRay
for the text marked in red
Is SelectMult.object.pos the right way to call postion of objects in a variable from an array?
I understand from this link that picked button has a .object property but how it works in arrays?
( http://discussion.autodesk.com/adskcsp/thread.jspa?messageID=5497075 )
Hope i made things understood.
Brgds
Sandy
Hi Freinds
Seems like i’m at an inch from what i was looking for…or rather looking for a more refined way of doing it…
I extended the script what pudgerboy wrote last…
Here’s the code:
global rot_child = eulerangles 50 30 90 -------- better to define globals at the start
global selectmult = #()
try(destroydialog AnimatedAlign)catch() -- remove the rollout if its currently open
rollout AnimatedAlign "Animated Align" width:123 height:134
(
local endtime = animationrange.end -------------end of the timeline to whatever frames it is
pickbutton btnPMastO "Pick Master Object" pos:[7,4] width:110 height:27 autoDisplay:true
button btnPMultipleO "Pick Child Objects" pos:[6,37] width:111 height:27 autoDisplay:true
button doit "Do It!!!" pos:[6,68] width:111 height:60 enabled:false
on btnPMultipleO pressed do
(
SelectMult = SelectByName() fn geo_filter obj = isKindOf obj Geometry --showHidden:false
if Selectmult != undefined do
(
doit.enabled = true
if SelectMult.count == 1 then btnPMultipleO.text = (selectmult[1].name as string) else btnPMultipleO.text = "Picked Multiple"
)
)
on doit pressed do
(
if Selectmult != undefined and (btnPMastO.object) != undefined do
(
for t in SelectMult do
(
animate on
(
at time 0 (t.pos = t.pos)
ChildRay = ray t.pos (t.dir*[-1,0,-1])
MasterZ = btnPMastO.object
TheInt = IntersectRay MasterZ ChildRay
at time endtime (t.pos = TheInt.pos)
t.parent = btnPMastO.object
--if TheInt != undefined then else t.pos = btnPMastO.object.position
)
)
)
)
)--end rollout
createdialog AnimatedAlign style:#(#style_toolwindow, #style_sysmenu)
PLs advice
Brgds
Sandy
if the master has animated modifier and is changing shape…the children are not going to respect that…any ideas/hints on this issue?
brgdds
sandy