[Closed] problem with function
i dont now why but functions logik doesnt stay in my brain. so i have this script to learn sometihing on it. Its made to do a gradient with 2 color in a list of object. similar to the gradient possible with bone in the bone tool .
-so i what the user to be able to choose between 2 color with color picker
- select whatever he what. a list of similar object( light,mesh etc) or random object
- when he selected you just applied the gradient.
The function i made doest seem to work . I think the main reaseon is because i collect all my object and put them in an array. but at the time the function is call the array is undefined maybe.
So any hint would be helpful . I really need to catch the logic here.
The comment are in french but i dont think this kind of script is in a level you cant understand…
------------------------------------------------
--ColorByNumber
--par David Bordeleau( silicium_vitae@sympatico.ca)
--v1.2
--19 may 2006
--tester avec max7
------------------------------------------------
------------------------------------------------
--creer un gradient en function du nombre d'iteration
-----------------------------------------------------
--Global
global a
--include
-- variable
--Function
fn SETGrad startColor:[0,0,0] EndColor:[255,255,255] =
(
if a != undefined do
(
for a = 1 to objects do
(
numerator = a - 1.0
End = EndColor
ObjCol = (denominator / numerator) * a.count
a.wirecolor = ObjCol
)
)
)
--Script
utility ColorByName "Color by iteration"
(
group "Object"
(
button ListSel "Select Object"
button selall "select All Object"
button showlist "Print List to Listener"
)
group"Color"
(
colorpicker StartCol "Starting color" color:[0,0,0] align:#center -- choisir un couleur de depart Defaut:noir
colorpicker EndCol "Ending Color" color:[255,255,255] align:#center-- choisir un couleur de depart Defaut:white
Button SetGradient "Set Gradient" align:#center
)
---------------------
--function des bouton
---------------------
on Listsel pressed do -- deselect les object courant et choisit plusieur objet
(
clearselection()
hitByNameDlg()
a = selection as array
--if a != undefined then
-- (
--listbox Sobjet "objet" items:( for a in objects collect a.name)
-- )
)
on selall pressed do
(
clearselection()
max select all
)
on showlist pressed do -- verifie si un object a ete selectionner et printe la liste dans le listener
(
a = selection as array
print a
--listbox.items
)
on SetGradient pressed do SETgrad()
-- applique le gradient sur les object selectionner. quand sa marche
)
----------------------------------------------------------------------
also i forgot to say it base on a script i made before and it work. But without UI
you had to change everything in the script ( no so useful)
clearlistener()
select $camera*
bottomfraction = $camera*.count - 1.0
--redvalue = 0.0
for i = 1 to $camera*.count do
(
topfraction = i - 1.0
redvalue = (topfraction / bottomfraction) * 255
$[i].wirecolor = [redvalue,0,0]
)
deselect $camera*
There are so many problems with your code that I cannot tell you everything that is wrong with it. But here are a couple of comments:
*There is no reason to keep a global variable with the selection. I am sure you can deal with that in a local variable, or pass the selection as argument to the function, or grab the selection inside the function.
*What was that ‘denominator’ variable in your function? It doesn’t seem to be defined anywhere.
*What is ‘objects’ in your for loop? You define a global ‘a’ containing the selection array, then suddenly you use ‘a’ as local for loop variable counting from 1 to objects? Then you assume a is the current object? And at the same time you try to perform mathematical operations with a? (You should use longer descriptive names for such variables, so it is clear to anyone what they contain).
Here is a highly simplified version of your function that does what you want. Feel free to implement it in your utility.
fn SETGrad theObjects startColor:[0,0,0] EndColor:[255,255,255] =
(
for a = 1 to theObjects.count do
(
theFactor = 1.0 * a / theObjects.count
theObjects[a].wirecolor = EndColor * theFactor + startColor * (1.0-theFactor)
)
)
SETGrad (selection as array)
thank for your help bobo. as i said function or well structure code is still unexplored space to me. That why i need to work on it. anyways. i’ll try to explain why my code is so messy.
mainly im a 3d game artist so i code with a artist brain. Wich mean take somethin laft and right , mix it and make it Look OK ( read believable) but is script it seem OK is not acceptable . (sadly) so i try to pick snippet of code here and there and built somethin with it and it result… this. i used inspiration from neil blevin’s code, a script book i found and script found on the net, here and scriptspot. An they dont mix well especially when you try to learn and you dont know what your doing.
That explain the global instead of a local, the “numerator” etc.
i’ll look closely at you function and try to learn from it.
thank a lot
-D-