PDA

View Full Version : Array index and rollout slider?


Vapourise
04-17-2012, 11:20 AM
Hey, I am wondering what the best way to access an array's index via a slider would be?

Say I have an array of objects, I would like position 1 on the slider to relate to index number 1 of the array, 2 for 2, 3 for 3 etc. So that, as the slider changes value it accesses the relevant indexed object and that slider action triggers a piece of code for that indexed object.

Ultimately, as I change slider position, the object in the scene changes appropriately :)

Any help would be greatly appreciated, Cheers

K

stigatle
04-17-2012, 12:07 PM
make an array of objects.
set the slider's max value to the count of objects in the array.
use the the slider's value as index into your object array.
then trigger code when slider changes value.

Vapourise
04-17-2012, 12:14 PM
Thanks for the reply, I understand that this is the basic principle behind making it work.
Here is some Bobo code I found and trying to fit to my needs:



myArray = $selection as array

rollout slide ""
(
slider slide "Slider" range:[1,myarray.count,1] type:#integer ticks:5
label lbl "Tick I"
on slide changed val do
(
select myarray[val]
copy $
$.name = uniquename "copy"
$.pos = $locator.pos
)
)
createdialog slide



Ok here is an update, I have managed to get the slider to select the corresponding object and move it, the only thing I cannot figure out is how to then delete that copy before the next one is put in place?

Cheers, K

miauu
04-17-2012, 08:01 PM
Don't use global variable for task like this.
(
global rol_sliderTest
try(destroyDialog rol_sliderTest)catch()
rollout rol_sliderTest ""
(
local obj = undefined
local sel = undefined
slider sld_01 "" range:[0,10,0] type:#integer ticks:10

on rol_sliderTest open do
(
obj = objects as array
sld_01.range = [0,obj.count,0]
)

on sld_01 changed val do
(
if val != 0 do
(
print obj[val].name
obj[val].wirecolor = [(random 0 255),(random 0 255),(random 0 255)]
if isValidNode sel do delete sel

select obj[val]
sel = copy obj[val]
sel.name = uniquename "copy"
sel.pos = [0,0,0]
)
)
)
createdialog rol_sliderTest
)

Vapourise
04-17-2012, 08:45 PM
Miauu, thank you very much for taking the time to show me this solution it is greatly appreciated! I obviously have a lot to learn, I didnt realise it was quite so complicated. Although I do not fully understand all of your script I will try to learn from it.

Do you mind explaining the difference between Global and Local in this scenario and why Global was a bad choice?

Thanks again, K

miauu
04-17-2012, 09:00 PM
Search maxscript Help file for "Scope of Variables".
This is copy-paste:
Global variables are visible in all running MAXScript code and hold their values until you exit 3ds Max.

Local variables are directly visible to code only in the lexical scope currently in effect, and hold their values only as long as the current scope is active. Once a scope is no longer active, you can no longer access the contents of a variable local to that scope.

Global variables can cause conflict with global vars from other scripts - if the two vars have the same name, so try to avoid it when is possible.
My english is not so god, but I believe that some other people in CGtalk can explain this a bit better. Also, search the forum and you may find some better explenations.

My script is not complicated. Here how it works:
-- first, declare a var, which will hold the created copy
local sel = undefined
-- then, in the on slider changed val do , the script first check if the copy is already in the scene and if it is a valid object
if isValidNode sel do delete sel
-- if sel is a valid node then the script delete it
after that the script create a new copy and assign it to sel var
So, when you move the slider for first time, the sel is undefined, and the script will not delete any object. After that the first copy is created. On second move of the slide, the sel already holds the created copy, and when the script check this(with if isValidNode sel do delete sel ) the copy will be deleted(because this copy is a valid scene object) and the new copy is created. Nothing too complicated. :)
Hope, you will understand what I wrote. :)

Vapourise
04-21-2012, 02:47 PM
Hmmm, the thing is though, i have multiple sliders trying to do the same thing and it seems at the moment that when I use another slider to swap pieces in and out the code deletes the object i have just selected with the previous slider.

Is there anyway around this?

cheers, K

miauu
04-21-2012, 03:54 PM
Post the code, but if you using
if isValidNode sel do delete selin every slider's event handler and the SEL is only one variable, the object will be deleted.

Vapourise
04-22-2012, 03:34 PM
Heyand thanks for reply, I overcame the problem by declaring a new variable for every object at the beginning of the script. It seems to work, here is the code so far...i know it is probably very bad but it does work which is all I need as it is the concept that is more important (being an Artists) :P






undo off

if ((Swordsmith != undefined) and (Swordsmith.isdisplayed)) do
(destroyDialog Swordsmith)

BladePoint = point pos: [0,0,65] size: 5
CGPoint = point pos: [0,0,30] size: 5
RPPoint = point pos: [5,0,30] size: 5
LPPoint = point pos: [-5,0,30] size: 5
Hiltpoint = point pos: [0,0,-3] size: 5
HandlePoint = point pos: [0,0,27] size: 5

Global obj = undefined
Global B1 = undefined
Global B2 = undefined
Global CGD = undefined
Global RP = undefined
Global LP = undefined
Global HNDL = undefined
Global HLT = undefined

rollout SwordSmith "Swordsmith" width:260 height:800
(
button FormatBladeBtn "Format Blades" pos:[16,144] width:110 height:32
button FormatCGBtn "Format Cross-Guards" pos:[136,144] width:110 height:32
button FormatPommelBtn "Format Pommels" pos:[16,184] width:110 height:32
button FormatHandleBtn "Format Handles" pos:[136,184] width:110 height:32
button FormatHiltBtn "Format Hilts" pos:[16,224] width:110 height:32
button CreateCameraBtn "Create Camera" pos:[16,72] width:110 height:32
button RemoveCameraBtn "Remove Camera" pos:[136,72] width:110 height:32
GroupBox grp1 "Stage 1 - Setup Scene" pos:[8,16] width:248 height:96
GroupBox grp2 "Stage 2 - Setup Assets" pos:[8,128] width:248 height:136
slider sld1 "Select Blade 1" pos:[16,360] width:232 height:44 range:[0,10,0] type:#integer ticks:10
slider sld2 "Select Blade 2" pos:[16,408] width:232 height:44 range:[0,10,0] type:#integer ticks:10
slider sld3 "Select Cross-Guard" pos:[16,456] width:232 height:44 range:[0,10,0] type:#integer ticks:10
slider sld4 "Select Right Pommel" pos:[16,504] width:232 height:44 range:[0,10,0] type:#integer ticks:10
slider sld5 "Select Left Pommel" pos:[16,552] width:232 height:44 range:[0,10,0] type:#integer ticks:10
slider sld6 "Select Handle" pos:[16,600] width:232 height:44 range:[0,8,0] type:#integer ticks:8
slider sld7 "Select Hilt" pos:[16,648] width:232 height:44 range:[0,10,0] type:#integer ticks:10
GroupBox grp3 "Stage 3 - Create Sword" pos:[8,288] width:248 height:416
button GenSwordBtn "Generate Sword" pos:[16,312] width:110 height:32
button ResetSwordBtn "Reset" pos:[136,312] width:110 height:32
button ExportBtn "Export" pos:[16,736] width:110 height:32
button ImageBtn "Create Image" pos:[136,736] width:110 height:32
groupBox grp10 "Stage 4 - Save Result" pos:[8,720] width:248 height:56

on CreateCameraBtn pressed do
(

)
on RemoveCameraBtn pressed do
(

)
on FormatBladeBtn pressed do
(
BladeArray = selection as array
for i in BladeArray do
(
i.name = uniquename "Blade"

-- eddybrown, Maxscript - frustrating group issue
-- http://www.polycount.com/forum/showthread.php?t=93815, 01-26-2012
i.pivot = [i.min.x, (i.max.y+i.min.y)/2, (i.max.z+i.min.z)/2]
)
)
on FormatCGBtn pressed do
(
CGArray = selection as array
for i in CGArray do
(
i.name = uniquename "CG"
i.pivot = [(i.max.x+i.min.x)/2, (i.max.y+i.min.y)/2, (i.max.z+i.min.z)/2]
)
)
on FormatPommelBtn pressed do
(
PommelArray = selection as array
for i in PommelArray do
(
i.name = uniquename "Pommel"
i.pivot = [i.min.x, (i.max.y+i.min.y)/2, (i.max.z+i.min.z)/2]
)
)
on FormatHandleBtn pressed do
(
HandleArray = selection as array
for i in HandleArray do
(
i.name = uniquename "Handle"
i.pivot = [(i.min.x+i.max.x)/2, (i.max.y+i.min.y)/2, i.max.z]
)
)
on FormatHiltBtn pressed do
(
HiltArray = selection as array
for i in HiltArray do
(
i.name = uniquename "Hilt"
i.pivot = [(i.min.x+i.max.x)/2, (i.max.y+i.min.y)/2, i.max.z]
)
)
on GenSwordBtn pressed do
(


-- FROM Anubis: Random Objects in a List, Scriptspot, 2009-12-30 05:26

Select $Blade*
BladeArray = Selection as Array
rndIndex = random 1 BladeArray.count
RightBlade = BladeArray[rndIndex]
RightCopy = copy RightBlade
RightCopy.name = uniquename "Right_Blade"
RightCopy.pos = BladePoint.pos

Select $Blade*
BladeArray = Selection as Array
rndIndex = random 1 BladeArray.count
LeftBlade = BladeArray[rndIndex]
LeftCopy = copy LeftBlade
LeftCopy.name = uniquename "Left_blade"
LeftCopy.pos = BladePoint.pos

-- FROM Mr-Bullfrog: MaxScript - Rotate and Object, 3D Buzz, 10-24-2008

currentMatrix = LeftCopy.transform
preRotate currentMatrix (eulertoquat (eulerAngles 0 180 0))
Leftcopy.transform = currentMatrix

Select $CG*
CGArray = Selection as Array
rndIndex = random 1 CGArray.count
CG = CGArray[rndIndex]
CGCopy = copy CG
CGCopy.name = uniquename "Copy_CG"
CGCopy.pos = CGPoint.pos

Select $Pommel*
PommelArray = Selection as Array
rndIndex = random 1 PommelArray.count
RightPommel = PommelArray[rndIndex]
RightPommelCopy = copy RightPommel
RightPommelCopy.name = uniquename "Pommel_Right"
RightPommelCopy.pos = RPpoint.pos

Select $Pommel*
PommelArray = Selection as Array
rndIndex = random 1 PommelArray.count
LeftPommel = PommelArray[rndIndex]
LeftPommelCopy = copy LeftPommel
LeftPommelCopy.name = uniquename "Pommel_Left"
LeftPommelCopy.pos = LPpoint.pos

currentMatrix = LeftPommelCopy.transform
preRotate currentMatrix (eulertoquat (eulerAngles 0 180 0))
LeftPommelcopy.transform = currentMatrix

Select $Handle*
HandleArray = Selection as Array
rndIndex = random 1 HandleArray.count
Handle = HandleArray[rndIndex]
HandleCopy = copy Handle
HandleCopy.name = uniquename "Copy_Handle"
HandleCopy.pos = Handlepoint.pos

Select $Hilt*
HiltArray = Selection as Array
rndIndex = random 1 HiltArray.count
Hilt = HiltArray[rndIndex]
HiltCopy = copy Hilt
HiltCopy.name = uniquename "Copy_Hilt"
HiltCopy.pos = Hiltpoint.pos
)
on ResetSwordBtn pressed do
(

for obj in objects do
(
select $copy_Hilt*
max delete
select $Pommel_Right*
max delete
select $Pommel_Left*
max delete
select $left_blade*
max delete
select $right_blade*
max delete
select $copy*
max delete
)
)

-- SLIDER 1
-- Edited from Code contributed by Miauu, CG Society forum, in response to my post
-- "Array Index and Rollout Slider", 17/04/2012
on sld1 changed val do
(
(
select $Blade*
BladeArray = Selection as Array
)
if val != 0 do
(
if isValidNode B1 do delete B1

select BladeArray[val]
B1 = copy BladeArray[val]
B1.name = uniquename "copy"
B1.pos = Bladepoint.pos
)
)

on sld1 buttondown do
(
select $Right*
max delete
)

on sld1 buttonup do
(
for obj in objects do
(
deselect objects
)
)

-- SLIDER 2
on sld2 changed val do
(
(
select $Blade*
BladeArrayLeft = Selection as Array

)
if val != 0 do
(



if isValidNode B2 do delete B2

select BladeArrayLeft[val]
B2 = copy BladeArrayLeft[val]
B2.name = uniquename "copy"
B2.pos = Bladepoint.pos

currentMatrix = B2.transform
preRotate currentMatrix (eulertoquat (eulerAngles 0 180 0))
B2.transform = currentMatrix
)
)
on sld2 buttondown do
(
select $Left*
max delete
)
on sld2 buttonup do
(
for obj in objects do
(
deselect objects
)
)

-- SLIDER 3
on sld3 changed val do
(
(
select $CG*
CGArray = Selection as Array
)
if val != 0 do
(
if isValidNode CGD do delete CGD

select CGArray[val]
CGD = copy CGArray[val]
CGD.name = uniquename "copy"
CGD.pos = CGpoint.pos
)
)
on sld3 buttondown do
(
select $copy_CG*
max delete
)
on sld3 buttonup do
(
for obj in objects do
(
deselect objects
)
)

-- SLIDER 4
on sld4 changed val do
(
(
select $Pommel*
RightPommelArray = Selection as Array
)
if val != 0 do
(
if isValidNode RP do delete RP

select RightPommelArray[val]
RP = copy RightPommelArray[val]
RP.name = uniquename "copy"
RP.pos = RPpoint.pos
)
)
on sld4 buttondown do
(
select $Pommel_Right*
max delete
)
on sld4 buttonup do
(
for obj in objects do
(
deselect objects
)
)

-- SLIDER 5
on sld5 changed val do
(
(
select $Pommel*
LeftPommelArray = Selection as Array
)
if val != 0 do
(
if isValidNode LP do delete LP

select LeftPommelArray[val]
LP = copy LeftPommelArray[val]
LP.pos = LPpoint.pos
LP.name = uniquename "copy"

currentMatrix = LP.transform
preRotate currentMatrix (eulertoquat (eulerAngles 0 180 0))
LP.transform = currentMatrix
)
)
on sld5 buttondown do
(
select $Pommel_Left*
max delete
)
on sld5 buttonup do
(
for obj in objects do
(
deselect objects
)
)

-- SLIDER 6
on sld6 changed val do
(
(
select $Handle*
HandleArray = Selection as Array
)
if val != 0 do
(
if isValidNode HNDL do delete HNDL

select HandleArray[val]
HNDL = copy HandleArray[val]
HNDL.name = uniquename "copy"
HNDL.pos = Handlepoint.pos
)
)
on sld6 buttondown do
(
select $copy_Handle*
max delete
)
on sld6 buttonup do
(
for obj in objects do
(
deselect objects
)
)

-- SLIDER 7
on sld7 changed val do
(
(
select $Hilt*
HiltArray = Selection as Array
)
if val != 0 do
(
if isValidNode HLT do delete HLT

select HiltArray[val]
HLT = copy HiltArray[val]
HLT.name = uniquename "copy"
HLT.pos = Hiltpoint.pos
)
)
on sld7 buttondown do
(
select $copy_Hilt*
max delete
)
on sld7 buttonup do
(
for obj in objects do
(
deselect objects
)
)
on ExportBtn pressed do
(

)
on ImageBtn pressed do
(

)
)

createdialog SwordSmith

Cheers, K

miauu
04-22-2012, 03:53 PM
Why you have so many global variables? Global with name obj will definitely cause problems with other scripts.
If only the script(rollout) use them - put all globals in the:
rollout rol_name ""
(
local obj = undefined
...
...
)
createDialog rol_name
Also, if you want, you can create all point helpers in
on rol_name open do
(
BladePoint = point pos: [0,0,65] size: 5
...
...
)

Vapourise
04-22-2012, 04:27 PM
I think I tried making them local at beginning of the script like you say and the sliders would not work.

Although this was probably just a mistake I made as I am still learning. I thought that if I put them at the beginning like you say then the other parts of the code (such as the 'on sld changed val do') would not be able to access them?

This was the problem I was having, when I moved ANY of the sliders they affected each other because they were sharing one variable.

I will put local variables like you say though and test, thanks for the point helper tip I will also implement that. I really appreciate your input, you will definately be in my dissertation acknowledgements :)

K

miauu
04-22-2012, 04:33 PM
First place the variables, then the rollout controls. There is no reason the sliders to not work.

rollout SwordSmith "Swordsmith" width:260 height:800
(
local obj = undefined
local B1 = undefined
local B2 = undefined
local CGD = undefined
local RP = undefined
local LP = undefined
local HNDL = undefined
local HLT = undefined

button FormatBladeBtn "Format Blades" pos:[16,144] width:110 height:32
button FormatCGBtn "Format Cross-Guards" pos:[136,144] width:110 height:32
)

Vapourise
04-23-2012, 11:39 AM
One last thing, slightly off topic but I cant seem to find a reason anywhere. Why does the viewport become progressively slower/more lag the more times I execute the script or use a button on the script rollout?

I really do not understand this and it is very annoying :/

K

CGTalk Moderation
04-23-2012, 11:39 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.