Help: Strange super slow scripting of make 2D python bug

Hi @pascal and @Helvetosaur

I am working on some 2D tools (this is a small part that cleans up the data) and encountered a strange bug.

Making 2D of this tree takes in top view takes 4 seconds, and sorting the lines and deleting the short and joining the long lines takes an instant. BUT doing it together takes a loooong time… why?

Try this:

  • Make 2D of the tree and run this script.

  • Then remove the # and then select the block and run it again. See how slow it is? I have not been able to figure out why make2D in python causes this odd slow down.

import rhinoscriptsyntax as rs

#rs.Command("_-Make2D Layout=CPlane Properties=ByOutputLayers CreateHiddenLines=No ShowTangents=No CreateSceneSilhouette=No CreateClippingPlaneIntersections=No ShowViewRectangle=No GroupOutput=No Enter", False)

lines = rs.SelectedObjects()
shortLines = []
longLines = []
for line in lines:
    if rs.CurveLength(line)<0.1:
        shortLines.append(line)
    else:
        longLines.append(line)
rs.DeleteObjects(shortLines)
if len(longLines)>0:
    result = rs.JoinCurves(longLines, delete_input=False)
    rs.SelectObjects(result)

Tree.3dm (1.0 MB)

I see I get the same issue if I just use rs.Move() as well:

image

PS! Rhino 7

There is something strange going on, sometimes the make2d part of the script takes 70 or even 130 seconds too. So what is causing make2D to run slow within rs.Command() ?

Any news on this?
@wim maybe you can take a look too?

I spent hours yesterday making a 2D tool from “scratch” based on this rhino-developer-samples/rhinopython/SampleMake2dSnapshots.py at 6 · mcneel/rhino-developer-samples · GitHub, but the code is so much more complicated, so it would be great if it was possible to do it with fewer lines of code.

Thanks!

Hi @Holo,

Time to Make2D was 4.26 seconds

Is this what I should expect using Rhino 8?

– Dale

Can you try the script in v7? I get strange, slow behaviours when I use make2d there.

Somethimes make2d is slow, other times it hangs after.

I can elaborate more tomorrow when on the computer.

Hi Dale,

On both my machines it takes around 68 seconds to run this script on the tree.3dm provided.

Make2D takes 3.6 seconds.
if I make2D first and then delete the rs.Command line from the script then sorting the lines takes 0.28 seconds.

So the entire operation should take less than 4 seconds, so why is Rhino stalling for 64 seconds?
I have Lands Design installed, but I can’t really see why that makes any difference on why this is slow in one operation, but not if ran as two.

Please test the following on the tree.3ds file:

import rhinoscriptsyntax as rs
import time

objects = rs.GetObjects("Select objects", preselect=True, select = True)
if objects:
    startTime = time.time()
    
    rs.Command("_-Make2D Layout=CPlane Properties=ByOutputLayers CreateHiddenLines=No ShowTangents=No CreateSceneSilhouette=No CreateClippingPlaneIntersections=No ShowViewRectangle=No GroupOutput=No Enter", False)
    
    lines = rs.SelectedObjects()
    shortLines = []
    longLines = []
    for line in lines:
        if rs.CurveLength(line)<0.1:
            shortLines.append(line)
        else:
            longLines.append(line)
    rs.DeleteObjects(shortLines)
    if len(longLines)>0:
        result = rs.JoinCurves(longLines, delete_input=False)
        rs.SelectObjects(result)
    
    print round(time.time()-startTime,2)

Hi @Holo,

Here are my times with your file and latest script code:

V7: _RunPythonScript
Time to Make2D was 3.79 seconds
4.12

V8: _RunPythonScript
Time to Make2D was 5.48 seconds
5.78

V8: _-ScriptEditor _Run
Time to Make2D was 5.51 seconds
6.02

I don’t have any 3rd party plug-ins loaded. My system is a Dell XPS laptop, btw.

– Dale

Thanks for testing.
I really don’t understand this bug (in my systems)
I will do some tests.

Do you have any idea on what can cause this massive lag?

Try disabling all of your 3rd party plug-ins, restart Rhino, and retest?

– Dale

1 Like

Yup, if Lands Design is loaded it causes the bug.

Without it takes 3.58 seconds.
With it takes 68 seconds.

Now how can that cause that lag ONLY if I do the two tasks right after each other with in one script, but not if I do them in two scripts and run them just after each other?

Do you have any idea?

The reason I want to use rs.Command(“Make2d”) instead of scripting the entire thing is that “Make2D” is so much easier to use and code than to master Rhino.Geometry.HiddenLineDrawing.Compute on large files.

Hi @Holo,

Does replacing:

lines = rs.SelectedObjects()

with:

lines = rs.LastCreatedObjects()

help?

– Dale

1 Like

If memory serves me right then rs.LastCreatedObjects refers to everything that is created as the script runs, so when I make2D of more stuff within a script then that too ends up in the list.

I will check it out.

I see that

import rhinoscriptsyntax as rs

rs.Command("_line 0,0,0 10,0,0")
rs.Command("_line 10,0,0 10,5,0")
rs.Command("_line 10,5,0 0,0,0")


lines = rs.LastCreatedObjects()

rs.SelectObjects(lines)

Works as expected… But now I have to log off for a while, 20:33 here and dinner is cooling down and wife is not… :wink:

Thanks!

2 Likes

No, it is a list of objects created by the last rs.Command() operation only.

2 Likes

Thanks, that works great in this situation.
Edit: Ah, I wrote that to early. Once I incorperated that in more complex code I got the same lag again.
After make2d it hang and the process takes 108 seconds if Lands is loaded, but unloaded it takes just 3.3 seconds.

Hi @fsalla and @albert
can you take a look at this please?
Test the script in post 7 with the file in post 1 in this thread.

When Lands is not loaded the script takes less than 4 seconds, with lands loaded it takes 68 seconds.

Thank you!

Hi @Holo,

I tested it and I got a slow performance having Lands Design loaded as well (it was faster in my case though, around 25 seconds having Lands Design loaded). I am reporting this issue and we will keep you updated with news.

1 Like

Hi Holo,
I debugged it, and it looks like Lands Design is receiving a selection modified message for each short line you delete, causing the Lands Edit Panel selection tree to be updated for each short line deleted.
This happens because you have the lines selected when you delete them. You can avoid the problem in 2 ways:

  1. not having the Lands Edit panel active when you run the script
  2. unselecting the lines you are going to delete. I just tried to unselect all and it fixes the problem:
    rs.UnselectAllObjects();
    rs.DeleteObjects(shortLines)

We will try to be smarter when receiving selection modified messages during a script run.

2 Likes

Thank you, I will try that out.
Unselecting them will be best for me as I can not control if users have the panel active or not.

But it also sounds strange that Rhino is reporting one and one line deleted. @steve is this the only way for Rhino?

I will report back!

Cheers!

Hi again, yes, that seems to be a valid workaround.
Thanks for taking the time to look into this.

Dang, I wrote that too early. On my home machine with Lands 5.8.3.9064 I still get this issue if Lands is loaded. I think it must have been unloaded when I did those last tests… sorry about that.