Announcements

Between mid-October and November, the content on AREA will be relocated to the Autodesk Community M&E Hub and the Autodesk Community Gallery. Learn more HERE.

delay script execution - not with sleep command

delay script execution - not with sleep command

Anonymous
Not applicable
4,352 Views
10 Replies
Message 1 of 11

delay script execution - not with sleep command

Anonymous
Not applicable
Hello. I'm a complete noob in programming and want to get the following: When max is starting up, I'd like to auto open some panels I use a lot. So I used the macro recorder to record some commands and I placed them in a script in the script startup folder. The script is executed during Max startup but the panels that auto open are closed again. I suppose that this has something to do with the execution of other scripts during Max startup. So I started my own script with a sleep command of 20 seconds to give the other scripts time to do what they have to do. But I see that the sleep command holds the execution of every script during Max startup. And when executing my script is resumed, something closes the panels again. So I removed the sleep command. Can someone help me what to do? Ho can I make my script execute after, say 30 seconds, after Max viewports and scripts has finally settled down? This is the script I made: macros.run "Track View" "LaunchFCurveEditor" -- Curve Editor Toggle macros.run "Scene Explorer" "SELayerExplorer" -- Scene Explorer Toggle actionMan.executeAction 135018554 "32771" -- Particle View Toggle actionMan.executeAction 0 "50048" -- Tools: Material Editor Toggle actionMan.executeAction 0 "60010" -- Render: Render SetUp Toggle actionMan.executeAction 0 "40029" -- Render: Environment Dialog Toggle Greetings, Ludo
0 Likes
Accepted solutions (2)
4,353 Views
10 Replies
Replies (10)
Message 2 of 11

spacefrog_
Advisor
Advisor
Accepted solution

best option is to use a dotnet Windows.Forms.Timer object and open the panels in the timer's Tick event

 

The following code opens the corresponding panels after a 5 seconds delay ( 5000 milliseconds)

You can save the code  to a ms file in your scripts/startup folder ( eg. DelayedLaunch.ms ) and the panels should be opened automatically at 3ds Max launch

 


function PerformDelayedTasks sender evt= ( -- important: don't repeat this timer event sender.enabled = false -- for sanity and cleanup dotnet.RemoveEventHandler sender "Elapsed" PerformDelayedTasks -- put all your tasks below macros.run "Track View" "LaunchFCurveEditor" -- Curve Editor Toggle macros.run "Scene Explorer" "SELayerExplorer" -- Scene Explorer Toggle actionMan.executeAction 135018554 "32771" -- Particle View Toggle actionMan.executeAction 0 "50048" -- Tools: Material Editor Toggle actionMan.executeAction 0 "60010" -- Render: Render SetUp Toggle actionMan.executeAction 0 "40029" -- Render: Environment Dialog Toggle ) delayTimer= dotnetobject "Windows.Forms.Timer" delayTimer.Interval=5000 dotnet.AddEventHandler delayTimer "Tick" PerformDelayedTasks delayTimer.enabled = true

Josef Wienerroither
Software Developer & 3d Artist Hybrid
Message 3 of 11

spacefrog_
Advisor
Advisor
Accepted solution

For some reasons the script does'nt want to work when put into the scripts/startup folder ( despite being loaded ) . I'll investigate and report back as soon as i know the reasons ...

 

Update:

Okay - fixed the code. Both used variables, the timer and the function have to be global for this to work. Sounds plausible as otherwise both would be destroyed as soon as the maxscript interpreter exits the script ...


Josef Wienerroither
Software Developer & 3d Artist Hybrid
Message 4 of 11

Anonymous
Not applicable
Hi Spacefrog. Thanks for the energy you put in to this. I couldn't even hope for such a reply. I'm gonna implement it here and try it out. I'll let you know what the outcome is. PS: people like you who can code are magicians in my eyes 😉 Greetings, Ludo Timp, The Netherlands
0 Likes
Message 5 of 11

Anonymous
Not applicable
Hi Spacefrog. It works. Thank you. You're a wizard! Greetings, Ludo Timp, The Netherlands
0 Likes
Message 6 of 11

didiridou
Participant
Participant

Thank you so much for providing this solution, Spacefrog!

May I ask how could this delay be used in the form of a loop?

For example make it move a box object by 10cm on the x axis, every 5 seconds.

Thank you in advance.

0 Likes
Message 7 of 11

istan
Advisor
Advisor
for i=1 to 5 do (
 move $yourbox [0,10,0]
 sleep 5
)

 

0 Likes
Message 8 of 11

didiridou
Participant
Participant

istan, I was wondering if it's possible to do it with the use of the timer like the example above, as opposed to the use of the sleep command, but in any case thank you very much for your reply! 🙂

0 Likes
Message 9 of 11

denisT.MaxDoctor
Advisor
Advisor

@didiridou wrote:

May I ask how could this delay be used in the form of a loop?

For example make it move a box object by 10cm on the x axis, every 5 seconds.


open a new thread... this one is not quite on your topic.

what you are asking for can be done in a number of different ways, but the correct solution depends on why you need it and how you want to use the time during the delay.

0 Likes
Message 10 of 11

didiridou
Participant
Participant

Thank you denisT.MaxDoctor! I understand that it's not part of the original question, I just thought I'd ask in addition to it. What I'd like to do is to keep access to a button that would exit the loop, at the same time as it runs (since the loop process I'm using might end up taking minutes sometimes). Using the sleep command would not allow access to that, but using a new thread would, thank you. I was wondering if Windows.Forms.Timer would as well.

0 Likes
Message 11 of 11

denisT.MaxDoctor
Advisor
Advisor

@didiridou wrote:

Thank you denisT.MaxDoctor! I understand that it's not part of the original question, I just thought I'd ask in addition to it. What I'd like to do is to keep access to a button that would exit the loop, at the same time as it runs (since the loop process I'm using might end up taking minutes sometimes). Using the sleep command would not allow access to that, but using a new thread would, thank you. I was wondering if Windows.Forms.Timer would as well.


This is a very old topic. And it is "closed" and marked as "solved". I don't want to discuss another problem here, perhaps a similar one.

0 Likes