No Recourse for while loop crash

No Recourse for while loop crash

wynand.lens
Explorer Explorer
431 Views
4 Replies
Message 1 of 5

No Recourse for while loop crash

wynand.lens
Explorer
Explorer

Yes, I understand that your code will crash if you have a non terminating while loop.

We all get it.

But in 2023 we

GIF.gif

REALLY should have a failsafe for when you do it without having to Kill maya in the task manager.

0 Likes
Accepted solutions (1)
432 Views
4 Replies
Replies (4)
Message 2 of 5

wynand.lens
Explorer
Explorer

Nowadays I don't lose any work, because I expect it. But it really throws a spanner in my flow when it happens.

0 Likes
Message 3 of 5

Kahylan
Advisor
Advisor
Accepted solution

Hi!

 

When testing while loops, its is quite common practice to build in a break statement after a set number of iterations:

breakingPoint = 5000
iterCount = 0
while <condition>:
    <logic>
    iterCount = iterCount +1
    if iterCount == breakingPoint:
        break

<statement>

 

If whatever comes after your while loop doesn't have a chance of success whitout the loop completing you can also use an error message:

import maya.cmds as cmds

breakingPoint = 5000
iterCount = 0
while <condition>:
    <logic>
    iterCount = iterCount +1
    if iterCount == breakingPoint:
        cmds.error("While loop ran for too long")

<statement>

 

This gives you all the benefits of the while loop, as it stops early if the condition turns true but it also allows you to set an end to it automatically.

 

Ofcourse there is the slight chance that your while loop would have completed after the breakoff point, so I would chose the breakoff point rather high. Also you'll have to wait for the loop to reach the breakoff point, but that is normally still faster than terminating and reopening maya.

 

I hope it helps!

Message 4 of 5

wynand.lens
Explorer
Explorer

@Kahylan 
Thank You very much! This is actually exactly what I needed. And it is a better solution than expecting an external break from maya itself. Better code is just better.
Cheers!

Message 5 of 5

Kahylan
Advisor
Advisor

Glad I could help 🙂

0 Likes