semi-dynamic parameter default in scripted plugin

semi-dynamic parameter default in scripted plugin

Anonymous
Not applicable
547 Views
5 Replies
Message 1 of 6

semi-dynamic parameter default in scripted plugin

Anonymous
Not applicable
Does anyone know if there is a way to change the default value of a parameter before a scripted plugin object is created?

Essentially I want to count the number of a certain type of object in the scene and make the parameter default to that number plus one in the rollout.
ie if we had one box in the scene, it's 'testParam' would be set to one. With 20 boxes in the scene, if we created a new box it's 'testParam' would be 21.
I can set this in the 'tool create' section, but I want it to be visible in the rollout and changable before the object is created.
As an aside, does anyone know why the parameter defaults are used in the initial rollout rather than the rollout defaults?

Testing codes as follows:
    
plugin helper test
name:"test"
category:"test"
classID:#(0x3d3f8c7a, 0x470fa329)
extends: exposetm

initialRollupState: 0x00000007
version: 1

(

fn GetNodeCount =
(
nodeCount =0
for i in helpers do
(
if (hasProperty i "pTestParam") then
(
nodeCount += 1
)
)
nodeCount
)

local lNodeCount = GetNodeCount()


parameters qmotionParam rollout: testRollout
(
pTestParam type:#integer animatable:false ui:uTestParam default:25
)


tool create
(
on mousePoint clickno do
(
if (clickno == 1) then
(
nodeTM.position = worldPoint
delegate.useParent = false
delegate.box = true
delegate.cross = false
)
)

on mouseMove clickno do
(
if (clickno == 2) then
(
nodeTM.position = worldPoint
)
else
(#stop)
)
)

rollout testRollout "Testing"
(

Spinner uTestParam "Test:" type:#integer range:

)
)


The line:

pTestParam type:#integer animatable:false ui:uTestParam default:25

is where I'm having the problem. Ideally it would be as simple as

pTestParam type:#integer animatable:false ui:uTestParam default:lNodeCount

however that doesn't work. Neither does (my implementation of) an execute line, as mentioned here
I'm trying the execute as follows;

parameters qmotionParam rollout: testRollout
(
execute "pTestParam type:#integer animatable:false ui:uTestParam default:" + lNodeCount as string
)

or;

execute "parameters qmotionParam rollout: testRollout\n (pTestParam type:#integer animatable:false ui:uTestParam default:" + lNodeCount as string+")"


Any help much appreciated
0 Likes
548 Views
5 Replies
Replies (5)
Message 2 of 6

Anonymous
Not applicable
I really don't know what I'm doing, but I stumbled on a bit of a hack...

Change:
pTestParam type:#integer animatable:false ui:uTestParam default:999999


And:
Spinner uTestParam "Test:" type:#integer range:


Nowhere near the right way to do it, but if you're in a jam...


MP
0 Likes
Message 3 of 6

Anonymous
Not applicable
Aha! Thanks for your reply Melting Point, unfortunately I need to keep the maximum value of the parameter defined. I was thinking perhaps I could redefine the maximum on creation which directed me to solution.

The secret is that any 'on <parameter> set' clause is called when the rollout is created. I'm not really sure if it's actually when the rollout is created, but it's definitely called after you press the 'create object' button and before you create the object.

on pTestParam set val do if (lCreated == false) then
(
pTestParam = GetNodeCount()
)


Of course the problem is that it would then count nodes whenever you tried to set it manually afterward, hence the lCreated test. If we set lCreated to true when the object is created, then that clause isn't called after that point. I'm sure it will bite me down the track, but works for now.

Thanks for your input again MeltingPoint 🙂

Full working code as follows;

plugin helper test
name:"test"
category:"test"
classID:#(0x3d3f8c7a, 0x470fa329)
extends: exposetm

initialRollupState: 0x00000007
version: 1

(

fn GetNodeCount =
/* Count the number of nodes in the scene and add one */
(
nodeCount =1
/* loop through all helpers in the scene and count then number with the "pTestParam" parameter */
for i in helpers do
(
if (hasProperty i "pTestParam") then
(
nodeCount += 1
)
)
nodeCount
)

/* variable to tell if object has been created */
local lCreated = false

parameters qmotionParam rollout: testRollout
(
/* standard parameter declaration, connected to spinner below, default 66 would usually
be default value before creation */
pTestParam type:#integer animatable:false ui:uTestParam default:66
/* test if parameter has been set and the object has not been created */
on pTestParam set val do if (lCreated == false) then
(
/* set the parameter to the number of nodes in the scene including the current */
pTestParam = GetNodeCount()
)
)

tool create
(
on mousePoint clickno do
(
if (clickno == 1) then
(
/* create object in the mouse position */
nodeTM.position = worldPoint

/* the object has now been created */
lCreated = true

/* cosmetic changes to the node left in from previous code */
delegate.useParent = false
delegate.box = true
delegate.cross = false
)
)

on mouseMove clickno do
(
if (clickno == 2) then
(
/* move object as long as mouse is down */
nodeTM.position = worldPoint
)
else
(#stop)
)
)

rollout testRollout "Testing"
(
/* standard spinner, integer from 0 to 100 default 50. Default here is not effective */
Spinner uTestParam "Test:" type:#integer range:
)
)


Edit: Added comments to code to help other newbies (used /* comments as they show up better in the browser)
0 Likes
Message 4 of 6

Anonymous
Not applicable
Actually that doesn't work. Moved the lCreated flag = true to inside the 'on set' clause so it only runs once.

plugin helper test
name:"test"
category:"test"
classID:#(0x3d3f8c7a, 0x470fa329)
extends: exposetm

initialRollupState: 0x00000007
version: 1

(

fn GetNodeCount =
/* Count the number of nodes in the scene and add one */
(
nodeCount =1
/* loop through all helpers in the scene and count then number with the "pTestParam" parameter */
for i in helpers do
(
if (hasProperty i "pTestParam") then
(
nodeCount += 1
)
)
nodeCount
)

/* variable to tell if object has been created */
local lCreated = false

parameters qmotionParam rollout: testRollout
(
/* standard parameter declaration, connected to spinner below, default 66 would usually
be default value before creation */
pTestParam type:#integer animatable:false ui:uTestParam default:66
/* test if parameter has been set and the object has not been created */
on pTestParam set val do if (lCreated == false) then
(
/* set the parameter to the number of nodes in the scene including the current */
pTestParam = GetNodeCount()
/* only run this sesction once */
lCreated = true
)
)

tool create
(
on mousePoint clickno do
(
if (clickno == 1) then
(
/* create object in the mouse position */
nodeTM.position = worldPoint

/* cosmetic changes to the node left in from previous code */
delegate.useParent = false
delegate.box = true
delegate.cross = false
)
)

on mouseMove clickno do
(
if (clickno == 2) then
(
/* move object as long as mouse is down */
nodeTM.position = worldPoint
)
else
(#stop)
)
)

rollout testRollout "Testing"
(
/* standard spinner, integer from 0 to 100 default 50. Default here is not effective */
Spinner uTestParam "Test:" type:#integer range:
)

)
0 Likes
Message 5 of 6

Anonymous
Not applicable
Nice work around. I just can't shake the feeling that there's a 'proper' way to do it. But when something works... don't fix it 🙂
0 Likes
Message 6 of 6

Anonymous
Not applicable
Just a note in case anyone else uses this code.
The local variable lCreated needs to be changed to a parameter instead. It needs to be saved and loaded with the plugin nodes, otherwise all the nodeCounts will change to the number of nodes in the scene whenever a file containing the plugin is loaded.

MeltingPoint, I'm starting to believe that with maxScript there is never a 'proper' way of doing it 😉
0 Likes