PDA

View Full Version : problems with my script (dynamic UI etc.)


GMS-575
10-09-2008, 11:10 AM
Hi Folks,

i wrote a small script with a dynamic UI. The script shows all the VrayMtls with reflections and let you change the reflection subdivs of the material. The script generates one main spinner to change all the subdivs at once and spinners to change the subdivs for each VrayMtl. But whenever i evalluate the script the first time i get a error message, the second time everything works fine.

I know that it has to do something with my function to update the main spinner, but i cant find the problem.

Maybe some of you maxscript masters can help me out and show me some tricks.

Cheers Georgios


---------------------------------------------------
-- V-Ray subdiv-O-mat
-- 01.10.2008 GEORGIOS MOUROUSSIDIS
-- v1.0
---------------------------------------------------
-- works only with VrayMtls and VrayMtls in
-- multimaterials
---------------------------------------------------
-- FUNCTIONS
---------------------------------------------------
-- GM_getVrayMaterials
-- GM_getVrayRefMaterials
-- GM_updateSpinnerValues
-- GM_getVrayMatsUI
---------------------------------------------------


---------------------------------------------------
fn GM_getVrayMaterials =
---------------------------------------------------
-- get all vraymaterials VrayMtl
-- return materials
---------------------------------------------------
(

local vrayMultiMats = #()
local vraySingleMats = #()
local vraySceneMats = #()
local sceneMats = ( for o in scenematerials where classof o == vraymtl or classof o == multimaterial collect o ) -- GET ALL VrayMtl MATERIALS
local z = 0
local y = 0

for i = 1 to sceneMats.count do
(
if classof sceneMats[i] == multimaterial then
for x = 1 to sceneMats[i].count do
if classof sceneMats[i][x] == vraymtl then
vrayMultiMats [z+=1] = sceneMats[i][x]

if classof sceneMats[i] == vraymtl then
vraySingleMats[y+=1] = sceneMats[i]
)

vraySceneMats = vraySingleMats + vrayMultiMats

vraySceneMats

)


---------------------------------------------------
fn GM_getVrayRefMaterials =
---------------------------------------------------
-- get all vraymaterials with reflections
-- return materials
---------------------------------------------------
(

local sceneVrayMaterials = GM_getVrayMaterials()
local vrayRefMaterials = #()
local z = 0


for i = 1 to sceneVrayMaterials.count do -- FILTER DOUBLE VALUES IN ARRAY
for j = sceneVrayMaterials.count to i + 1 by -1 do
if sceneVrayMaterials[i] == sceneVrayMaterials[j] do
deleteItem sceneVrayMaterials j

for i = 1 to sceneVrayMaterials.count do -- FILTER REFLECTIV MATERIALS
if sceneVrayMaterials[i].reflection != color 0 0 0 then
vrayRefMaterials [z+=1] = sceneVrayMaterials[i]

vrayRefMaterials

)


---------------------------------------------------
fn GM_updateSpinnerValues GM_rolloutName =
---------------------------------------------------
-- update spinners in the ui
---------------------------------------------------
(

local rolloutName = GM_rolloutName
local vrayMaterials = GM_getVrayRefMaterials()
local rolloutControls = rolloutName.controls
local rolloutSpinner = #()
local z = 0

for i = 1 to rolloutControls.count do -- FILTER ALL SPINNERS
if classOf rolloutControls[i] == SpinnerControl and rolloutControls[i].name != "GM_spinnerAll" then
rolloutSpinner[z+=1] = rolloutControls[i]

for i = 1 to rolloutSpinner.count do -- UPDATE ALL SPINNERS WITH THE spinnerAll VALUE
if rolloutSpinner[i].value != vrayMaterials[i].reflection_subdivs then
rolloutSpinner[i].value = vrayMaterials[i].reflection_subdivs

)


---------------------------------------------------
fn GM_getVrayMatsUI =
---------------------------------------------------
-- build up the main ui
---------------------------------------------------
(

global mainVrayOmatRolloutFloater -- NAME OF THE rolloutFloater
global spinnerArray = GM_getVrayRefMaterials()

local mainRollout = "rolloutMain"
local subRolloutOne = "rolloutOne"
local subRolloutTwo = "rolloutTwo"
local subRolloutAbout = "rolloutAbout"

local spinnerName = "GM_spinner"
local spinnerArrayFiltered = #()
local spinnerValue = 0
local z = 0
local rolloutFloaterHeight = 250

if spinnerArray.count >= 2 then
rolloutFloaterHeight = 250 + spinnerArray.count * 20

try ( closeRolloutFloater mainVrayOmatRolloutFloater ) catch()

mainVrayOmatRolloutFloater = newRolloutFloater "V-Ray subdiv-O-mat" 300 rolloutFloaterHeight

StringStreamRollout = stringStream ""

for i = 1 to spinnerArray.count do
spinnerArrayFiltered[z+=1] = filterString (spinnerArray[i] as string) ":"


format "rollout % \"set all reflection subdivs\" \n" subRolloutOne to:StringStreamRollout
format "(\n" to:StringStreamRollout
format "label GM_label1 \"subdivs\" across:2 align:#left offset:[10,0] \n" to:StringStreamRollout
format "spinner GM_spinnerAll \"\" range:[1,64,8] type:#integer align:#right fieldwidth: 30 \n" to:StringStreamRollout
format "on GM_spinnerAll changed val do for x = 1 to spinnerArray.count do ( spinnerArray[x].reflection_subdivs = val \n GM_updateSpinnerValues(%) ) \n" subRolloutTwo to:StringStreamRollout
format ")\n" to:StringStreamRollout

format "rollout % \"set reflection subdivs\"\n" subRolloutTwo to:StringStreamRollout
format "(\n" to:StringStreamRollout
format "group \"\"\n" to:StringStreamRollout
format "(\n" to:StringStreamRollout
for i = 1 to spinnerArray.count do
(
spinnerValue = spinnerArray[i].reflection_subdivs
format "label GM_spinnerLabel% \"%\" across:2 align:#left offset:[10,0] \n" i spinnerArrayFiltered[i][1] to:StringStreamRollout
format "spinner %% \"\" range:[1,64,%] type:#integer align:#right fieldwidth: 30 " spinnerName i spinnerValue to:StringStreamRollout
)
format ")\n" to:StringStreamRollout

format "label GM_label3 \"\" offset:[-10,-7] \n" to:StringStreamRollout
format "label GM_label4 \"\" offset:[-10,-7] \n" to:StringStreamRollout

for i = 1 to spinnerArray.count do
format "on %% changed value do spinnerArray[%].reflection_subdivs = value\n" spinnerName i i to:StringStreamRollout

format ")\n" to:StringStreamRollout

format "rollout % \"about\"\n" subRolloutAbout to:StringStreamRollout
format "(\n" to:StringStreamRollout
format "label GM_label5 \"\xa9 2008 Georgios Mouroussidis\"\n" to:StringStreamRollout
format "label GM_label6 \"v1.0\"\n" to:StringStreamRollout
format ")\n" to:StringStreamRollout

format "addRollout rolloutOne mainVrayOmatRolloutFloater\n" to:StringStreamRollout
format "addRollout rolloutTwo mainVrayOmatRolloutFloater\n" to:StringStreamRollout
format "addRollout rolloutAbout mainVrayOmatRolloutFloater\n" to:StringStreamRollout
format "rolloutAbout.open = false\n" to:StringStreamRollout

execute( StringStreamRollout as string )

StringStreamRollout = undefined

)

GM_getVrayMatsUI()

GMS-575
10-10-2008, 09:25 AM
no one with a solution for my problem????? where are you maxscript gods?? :bowdown:

JHN
10-12-2008, 09:13 PM
It's a global/local scope issue.

global GM_getVrayMaterials
global GM_getVrayRefMaterials
global GM_updateSpinnerValues
global GM_getVrayMatsUI

declare these on top of your script and you'll find it probably works.
The first time the script is run the UI can't find the functions outside it's own scope, and somehow they aren't globally available, because you didn't declare them globally. Why the're working the second time is because there are defined in a global scope (there are no parenthesis around the total script), so now it finds them when running the script again.

Declaring them global as the first thing you do should make sure, the're available always.

Hope this helps, because I have no Vray, so can't test it,
-Johan

GMS-575
10-13-2008, 09:12 AM
I tried it but it still doesn't work.. i also tried to declare some of my variables global..and guess what...it didn't work.. :scream:

JHN
10-14-2008, 09:33 PM
Hmm, that's weird than indeed... but I don't have no Vray, so I can't test it any further... sorry,

-Johan

MoonDoggie
10-14-2008, 09:37 PM
as already stated, you need to declare all of your functions and rollouts globally above all the code for it to evaulate correctly the first time.

GMS-575
10-15-2008, 10:29 AM
Hi Folks, first of all thanks for the advices. :beer:
I tried to declare the functions global but it seem not to work correct. I'm sure i made something wrong, declaring the functions global. But i found a workaround for my problem and i know that this solution is not the best one and i think its also not good coding.


---------------------------------------------------
-- V-Ray subdiv-O-mat
-- 01.10.2008 GEORGIOS MOUROUSSIDIS
-- v1.0
---------------------------------------------------
-- works only with VrayMtls and VrayMtls in
-- multimaterials
---------------------------------------------------
-- FUNCTIONS
---------------------------------------------------
-- GM_getVrayMaterials
-- GM_getVrayRefMaterials
-- GM_getVrayMatsUI
-- GM_updateSpinnerValues
-- GM_updateSpinnerValueUI
---------------------------------------------------

---------------------------------------------------
fn GM_getVrayMaterials =
---------------------------------------------------
-- get all vraymaterials VrayMtl
-- return materials
---------------------------------------------------
(
local vrayMultiMats = #()
local vraySingleMats = #()
local vraySceneMats = #()
local sceneMats = ( for o in scenematerials where classof o == vraymtl or classof o == multimaterial collect o ) -- GET ALL VrayMtl MATERIALS
local z = 0
local y = 0

for i = 1 to sceneMats.count do
(
if classof sceneMats[i] == multimaterial then
for x = 1 to sceneMats[i].count do
if classof sceneMats[i][x] == vraymtl then
vrayMultiMats [z+=1] = sceneMats[i][x]

if classof sceneMats[i] == vraymtl then
vraySingleMats[y+=1] = sceneMats[i]
)

vraySceneMats = vraySingleMats + vrayMultiMats

vraySceneMats

)


---------------------------------------------------
fn GM_getVrayRefMaterials =
---------------------------------------------------
-- get all vraymaterials with reflections
-- return materials
---------------------------------------------------
(

local sceneVrayMaterials = GM_getVrayMaterials()
local vrayRefMaterials = #()
local z = 0

for i = 1 to sceneVrayMaterials.count do -- FILTER DOUBLE VALUES IN ARRAY
for j = sceneVrayMaterials.count to i + 1 by -1 do
if sceneVrayMaterials[i] == sceneVrayMaterials[j] do
deleteItem sceneVrayMaterials j

for i = 1 to sceneVrayMaterials.count do -- FILTER REFLECTIV MATERIALS
if sceneVrayMaterials[i].reflection != color 0 0 0 then
vrayRefMaterials [z+=1] = sceneVrayMaterials[i]

vrayRefMaterials

)


---------------------------------------------------
fn GM_getVrayMatsUI =
---------------------------------------------------
-- build up the main ui
---------------------------------------------------
(

global mainVrayOmatRolloutFloater -- NAME OF THE rolloutFloater
global spinnerArray = GM_getVrayRefMaterials()

local mainRollout = "rolloutMain"
local subRolloutTwo = "rolloutTwo"
local subRolloutAbout = "rolloutAbout"
local spinnerName = "GM_spinner"
local colorpickerName = "GM_color"
local spinnerArrayFiltered = #()
local spinnerValue = 0
local materialRefValue = ( color 0 0 0 )
local z = 0
local rolloutFloaterHeight = 250

if spinnerArray.count >= 2 then
rolloutFloaterHeight = 250 + spinnerArray.count * 20

try ( closeRolloutFloater mainVrayOmatRolloutFloater ) catch()

mainVrayOmatRolloutFloater = newRolloutFloater "V-Ray subdiv-O-mat" 300 rolloutFloaterHeight

stringStreamRollout = stringStream ""

for i = 1 to spinnerArray.count do -- FILTER THE MATERIAL NAMES
spinnerArrayFiltered[z+=1] = filterString (spinnerArray[i] as string) ":"

format "rollout % \"set reflection subdivs\" category:2\n" subRolloutTwo to:stringStreamRollout
format "(\n" to:stringStreamRollout
format "group \"\"\n" to:stringStreamRollout
format "(\n" to:stringStreamRollout
format "label GM_matName \"material name\" across:3 align:#left offset:[10,-5] \n"to:stringStreamRollout
format "label GM_refColor \"reflect\" align:#left offset:[87,-5] \n"to:stringStreamRollout
format "label GM_matName2 \"subdivs\" align:#left offset:[48,-5] \n"to:stringStreamRollout
format "label GM_label7 \"\" align:#left \n"to:stringStreamRollout

for i = 1 to spinnerArray.count do
(
spinnerValue = spinnerArray[i].reflection_subdivs
materialRefValue = spinnerArray[i].reflection
format "label GM_spinnerLabel% \"%\" across:3 align:#left offset:[10,0] \n" i spinnerArrayFiltered[i][1] to:stringStreamRollout
format "colorpicker %% \"\" color:[%,%,%] fieldWidth:25 height:15 modal:false offset:[85,0]\n" colorpickerName i (materialRefValue.r as integer) (materialRefValue.g as integer) (materialRefValue.b as integer) to:stringStreamRollout
format "spinner %% \"\" range:[1,64,%] type:#integer align:#right fieldwidth: 30 " spinnerName i spinnerValue to:stringStreamRollout
)

format "label GM_label3 \"\" \n" to:stringStreamRollout
format ")\n" to:stringStreamRollout

for i = 1 to spinnerArray.count do
(
format "on %% entered do spinnerArray[%].reflection_subdivs = %%.value\n" spinnerName i i spinnerName i to:stringStreamRollout
format "on %% changed newColor do spinnerArray[%].reflection = newColor\n" colorpickerName i i to:stringStreamRollout
)

format ")\n" to:stringStreamRollout

format "rollout % \"about\" category:3\n" subRolloutAbout to:stringStreamRollout
format "(\n" to:stringStreamRollout
format "label GM_label5 \"\xa9 2008 Georgios Mouroussidis\"\n" to:stringStreamRollout
format "label GM_label6 \"v1.0\"\n" to:stringStreamRollout
format ")\n" to:stringStreamRollout

format "addRollout rolloutTwo mainVrayOmatRolloutFloater\n" to:stringStreamRollout
format "addRollout rolloutAbout mainVrayOmatRolloutFloater\n" to:stringStreamRollout
format "rolloutAbout.open = false\n" to:stringStreamRollout

execute( stringStreamRollout as string )

stringStreamRollout = undefined


)
GM_getVrayMatsUI()

---------------------------------------------------
fn GM_updateSpinnerValues =
---------------------------------------------------
-- update the values of the reflection subdiv
-- spinners
---------------------------------------------------
(

local vrayMaterials = GM_getVrayRefMaterials()
local rolloutControls = rolloutTwo.controls
local rolloutSpinner = #()
local z = 0

for i = 1 to rolloutControls.count do -- FILTER ALL SPINNERS EXCEPT GM_spinnerAll
if classOf rolloutControls[i] == SpinnerControl and rolloutControls[i].name != "GM_spinnerAll" then
rolloutSpinner[z+=1] = rolloutControls[i]

for i = 1 to rolloutSpinner.count do -- UPDATE ALL SPINNERS WITH THE GM_spinnerAll VALUE
if rolloutSpinner[i].value != vrayMaterials[i].reflection_subdivs then
rolloutSpinner[i].value = vrayMaterials[i].reflection_subdivs

)


---------------------------------------------------
fn GM_updateSpinnerValueUI =
---------------------------------------------------
-- update the values of the spinners in the rollout
---------------------------------------------------
(

spinnerArray = GM_getVrayRefMaterials()

rollout rolloutOne "set all reflection subdivs" category:1
(
label GM_label1 "subdivs" across:2 align:#left offset:[10,0]
spinner GM_spinnerAll "" range:[1,64,8] type:#integer align:#right fieldwidth: 30

on GM_spinnerAll entered do for x = 1 to spinnerArray.count do
(
spinnerArray[x].reflection_subdivs = GM_spinnerAll.value
GM_updateSpinnerValues()
)
)

addRollout rolloutOne mainVrayOmatRolloutFloater

)
GM_updateSpinnerValueUI()

hblan
10-15-2008, 01:46 PM
((
local a = 1
local b =2
........

rollout abc "abc"
(......)

rollout def "def "
(..... )

.........
newfloaterrollout .....
))

-- do not use global declaration . i never use it . --

MoonDoggie
10-15-2008, 03:24 PM
There is nothing wrong with using global variables in code. It is simply not feasible to pass variables to all functions based on a checkbox, listbox, etc, sometimes it's just easier and cleaner code to use a global. That being said, use them only when you need them, but don't shy away from them if they make life easier.

JHN
10-15-2008, 03:36 PM
What Colin said, you don't want them all over the place but sometimes you just can get without, especially with large scripts and script libraries. I tend to use structs and make the struct global. It works like a namspace that way. If you prefix a global variable with some unique word or letters, then there's no problem at all. Big issues arise when you declare a global var when that same name is already taken keyword in max.

Other than that you should be fine.
-Johan

GMS-575
10-15-2008, 04:31 PM
in my script i had to declare some globals otherwise some stuff would not work correct.

CGTalk Moderation
10-15-2008, 04:31 PM
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.