running script at file open and access global variables in UI

running script at file open and access global variables in UI

mark_aldred
Advocate Advocate
1,587 Views
11 Replies
Message 1 of 12

running script at file open and access global variables in UI

mark_aldred
Advocate
Advocate

I have a script controlling objects and to get it working I'm evaluating it after I open up the file containing the objects. I have two questions ..

 

1. what's the best way to set it up so that the script is evaluated automatically when the file is opened?

 

2. what would be the best way to make for example the variable "speedMultiplier" available in the UI?

 

Here is a sample of the script ..

 

global speedMultiplier = 1000
global plMultiplier = 4.029
global initialPlPosition = 254.52
global positionOffset = 0

$pl.position.controller=position_script()
$pl.position.controller.script = "$pl.transform.controller.Position = [$plOrbit.radius*cos(positionOffset+initialPlPosition+360*speedMultiplier*plMultiplier*F/2700), $plOrbit.radius*sin(positionOffset+initialPlPosition+360*speedMultiplier*plMultiplier*F/2700), 0]+$plOrbit.transform.pos"





 

0 Likes
1,588 Views
11 Replies
Replies (11)
Message 2 of 12

miauuuu
Collaborator
Collaborator

1. use callbacks

 

callbacks.addScript #filePostOpen ...

 

2. the "speedMultiplier" is defined as global variable, so you can access it directly in your UI

https://miauu-maxscript.com/
0 Likes
Message 3 of 12

mark_aldred
Advocate
Advocate

Thanks.

So if I evaluate a script as part of a callback, that script is registered with the file and executed for example on #filePostOpen? Where does the script actually reside? What if I want to edit the script? Is there a way to access it from inside max, or do I have to edit an external copy and evaluate that?

 

 

0 Likes
Message 4 of 12

miauuuu
Collaborator
Collaborator

The script resides in 3ds max memory. To edit it while 3ds max is running open the script(from the maxRoot/scripts/startup folder if you placed it there), edit it in the MaxScript Editor. Then save the script and evaluate it. 3dsMax will use the updated version of the script(because you evaluated it). And, since you edit(and save) the script in the startup folder, next time you run 3ds max the updated script will be executed.

https://miauu-maxscript.com/
Message 5 of 12

mark_aldred
Advocate
Advocate

Thanks.

So a callback script has to be saved in the startup folder? I thought maybe it would be saved as part of the *.max file. If it's a separate file, the only advantage is that it can be executed automatically? Not that it travels as part of the max file?

 

 

 

0 Likes
Message 6 of 12

miauuuu
Collaborator
Collaborator

To bind your code to the max scene try this:

[code]

myCA = attributes myAttributes
(
fn My_Fn =
(
-- "code to execute goes here"
)
-- "should be executed when the file is open"
on load do My_Fn()
)

custAttributes.add rootnode myCA

[/code]

Save the file, reset 3ds max, load the file.

https://miauu-maxscript.com/
Message 7 of 12

Serejah
Advocate
Advocate

@mark_aldred  написал (-а):
... I thought maybe it would be saved as part of the *.max file. If it's a separate file, the only advantage is that it can be executed automatically? Not that it travels as part of the max file?

You can place the script inside custom attribute, scripted controller so it will be stored in max scene file.

Another story how would it execute when you load the scene. Especially after AD introduced super-security-anti-virus thing it could be a problem.

Message 8 of 12

mark_aldred
Advocate
Advocate

Thank you both!

The custom attribute is working. Is this the normal way to do things?

Regarding callbacks, what is the difference between callback and bind? Are callbacks never saved with the file?

 

Apologies for all the questions. I'll go through help myself too.

 

Edit: Incidentally, where do the custom attributes reside and how can I access them?

0 Likes
Message 9 of 12

mark_aldred
Advocate
Advocate

Bump: anybody know how I can access those custom attributes?

Thanks

 

0 Likes
Message 10 of 12

istan
Advisor
Advisor
0 Likes
Message 11 of 12

mark_aldred
Advocate
Advocate

Thanks. I've been looking at that page.

 

I have a couple of questions ..

 

"Custom attributes is currently supported only in the Material Editor and the Command Panel, so you can only see any rollouts associated with the custom attributes on base objects, modifiers, and materials."

 

This means I need to associate a custom attribute to some kind of object to be able to create a rollout?

 

And .. how can I actually access the custom attribute script itself? Do I have to save it separately and execute it every time I edit?

 

All I need is an editable script that travels with the file.

 

0 Likes
Message 12 of 12

istan
Advisor
Advisor

From a security point of view, I would not allow the execution of a script, which comes with a scene. The same applies for word, excel, ppt,.. files.

0 Likes