FreeCAD does not execute the python script as expected

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
foadsf
Posts: 102
Joined: Fri Mar 06, 2015 10:02 pm
Contact:

FreeCAD does not execute the python script as expected

Post by foadsf »

I'm trying to learn FreeCAD python scripting. Basically I open the python console and do what I want to do in the GUI and then look into the python console to learn the commands. and then read the API for that specific task to learn the correct form of python commands.

Things were going fine till I got stuck in this weird issue where the program (I.E FreeCAD) does not execute parts of my code. For example inthis macro I create three boxes and then fuse two together, and it works just fine. But inthis one I create 3 boxes, fuse two of them together, and then try to cut the fusion out of the bigger box. and it doesn't work. I even tried including some flags using print("flag"), but it does not execute these commands. If I copy and past the exact commands into the python console and run it works fine!

so my speculations are:

1. FreeCAD does something in the GUI which does not report in the python console.
2. FreeCAD python interpreter does not execute some commands such as print("")
3. there is something in FreeCAD API which I'm not using correctly
I would appreciate if you could help me know:

1. if this is a bug in FreeCAD/python or it is intentional
2. how can I solve the issue so the FreeCAD python interpreter runs my macro/script as I expect?

I also asked the same question StackOverflow
foadsf
Posts: 102
Joined: Fri Mar 06, 2015 10:02 pm
Contact:

Re: FreeCAD does not execute the python script as expected

Post by foadsf »

OK, I figured the problem out. you may see the correct macro here in this Github Gist

explanation: Basically the solution is that when we want to run a Boolean operation on two existing objects we should not change their visibility to false (as the default GUI commands do). If we include those commands then none of the commands after them will be executed.
ian.rees
Posts: 696
Joined: Sun Jun 15, 2014 3:28 am
Contact:

Re: FreeCAD does not execute the python script as expected

Post by ian.rees »

Hi foadsf,

Thanks for your timely question - have a look at https://forum.freecadweb.org/viewtopic.php?f=10&t=21574 .

Short answer is that, IMHO, some of the text handling in this area is currently broken in FreeCAD (the design is not what it should be, vs the code not behaving according to the design). You should enable the redirection options to the Report View, and do your work with the Report View open at the moment.

I've been too busy with other stuff to actually implement the changes proposed in that thread, but hope to get back to it soon. -Ian-
triplus
Veteran
Posts: 9471
Joined: Mon Dec 12, 2011 4:45 pm

Re: FreeCAD does not execute the python script as expected

Post by triplus »

Hi @foadsf.

Note that Ian mentioned discussion in another thread and that discussion is more or less unrelated to your issue. Errors produced by your 2 script will be produced in Python console if you execute your script in Python console or in Report view if you run your code as a macro. For that you don't have to enable any option in preferences as it works like that by default (FreeCAD 0.16+).

The problem is you are trying to use an attribute that does not exist:

Code: Select all

mybox.Visibility=False
Instead you should use:

Code: Select all

mybox.ViewObject.Visibility=False
P.S. But note that you don't have to set visibility in the first place from the script. As the boolean feature you used will do that automatically.
ian.rees
Posts: 696
Joined: Sun Jun 15, 2014 3:28 am
Contact:

Re: FreeCAD does not execute the python script as expected

Post by ian.rees »

triplus wrote:that discussion is more or less unrelated to your issue.
??? The other thread is all about the debugging issues in the OP, how is it unrelated?

Just because the problem the OP was trying to find turned out to be unrelated to the text stuff, the issue that he posted about is precisely related to FreeCAD's weird behaviour in this regard.
User avatar
DeepSOIC
Veteran
Posts: 7896
Joined: Fri Aug 29, 2014 12:45 am
Location: used to be Saint-Petersburg, Russia

Re: FreeCAD does not execute the python script as expected

Post by DeepSOIC »

foadsf wrote: If I copy and past the exact commands into the python console and run it works fine!
I did it, and got this:

Code: Select all

>>> print("flag 1")
flag 1
>>> 
>>> 
>>> test1 = App.newDocument("Test1")
>>> 
>>> cylinder=test1.addObject("Part::Box","Cylinder")
>>> cylinder.Height = '20 mm'
>>> cylinder.Width = '5 mm'
>>> cylinder.Length = '40 mm'
>>> 
>>> myvec = App.Vector(5,0,5)
>>> myvec_2 = App.Vector(0,0,0)
>>> 
>>> mybox=test1.addObject("Part::Box","Mybox")
>>> mybox.Height = '5 mm'
>>> mybox.Width = '15 mm'
>>> mybox.Length = '10 mm'
>>> 
>>> 
>>> mybox.Placement.Base = myvec
>>> 
>>> mybox_2=test1.addObject("Part::Box","Mybox_2")
>>> mybox_2.Height = '5 mm'
>>> mybox_2.Width = '10 mm'
>>> mybox_2.Length = '20 mm'
>>> 
>>> mybox_2.Placement.Base = myvec_2
>>> 
>>> myfus = test1.addObject("Part::Fuse","myFus")
>>> myfus.Base = mybox
>>> myfus.Tool = mybox_2
>>> mybox.Visibility=False
Traceback (most recent call last):
  File "<input>", line 1, in <module>
AttributeError: 'PrimitivePy' object has no attribute 'Visibility'
>>> mybox_2.Visibility=False
Traceback (most recent call last):
  File "<input>", line 1, in <module>
AttributeError: 'PrimitivePy' object has no attribute 'Visibility'
>>> 
>>> print("flag 2")
flag 2
>>> 
>>> App.activeDocument().addObject("Part::Cut","Cut")
<Part::PartFeature>
>>> App.activeDocument().Cut.Base = App.activeDocument().Cylinder
>>> App.activeDocument().Cut.Tool = App.activeDocument().myFus
>>> App.ActiveDocument.recompute()
5
>>> 
>>> 
Notice the errors. If you execute a macro, the execution stops at the first error and goes no further. If you paste it to console, it is equivalent to typing the lines of code one by one into it. The console won't stop working if you type some nonsense, so it effectively ignores the faulty lines and continues.
User avatar
DeepSOIC
Veteran
Posts: 7896
Joined: Fri Aug 29, 2014 12:45 am
Location: used to be Saint-Petersburg, Russia

Re: FreeCAD does not execute the python script as expected

Post by DeepSOIC »

foadsf wrote:so my speculations are:

1. FreeCAD does something in the GUI which does not report in the python console.
2. FreeCAD python interpreter does not execute some commands such as print("")
3. there is something in FreeCAD API which I'm not using correctly
1. Correct.
2. Incorrect. The output goes to different places though: print in console is dumped to console; print in a macro or in other code not invoked from console ends up in Report View.
3. Just mistakes, already sorted out as I see.
triplus
Veteran
Posts: 9471
Joined: Mon Dec 12, 2011 4:45 pm

Re: FreeCAD does not execute the python script as expected

Post by triplus »

foadsf wrote:1. FreeCAD does something in the GUI which does not report in the python console.
DeepSOIC wrote:1. Correct.
Correct or correct behaviour? ;)
User avatar
DeepSOIC
Veteran
Posts: 7896
Joined: Fri Aug 29, 2014 12:45 am
Location: used to be Saint-Petersburg, Russia

Re: FreeCAD does not execute the python script as expected

Post by DeepSOIC »

triplus wrote:
foadsf wrote:1. FreeCAD does something in the GUI which does not report in the python console.
DeepSOIC wrote:1. Correct.
Correct or correct behaviour? ;)
Both. :P But I guess that raises a question of what "does" word means precisely. Taken to extreme, FreeCAD should report to console how something is reported to console, and infinite recursion begins :geek:
wmayer
Founder
Posts: 20319
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: FreeCAD does not execute the python script as expected

Post by wmayer »

foadsf wrote:OK, I figured the problem out. you may see the correct macro here in this Github Gist

explanation: Basically the solution is that when we want to run a Boolean operation on two existing objects we should not change their visibility to false (as the default GUI commands do). If we include those commands then none of the commands after them will be executed.
It doesn't work because you try to perform a wrong Python statement.

Code: Select all

mybox.Visibility=False
mybox_2.Visibility=False
mybox and mybox_2 don't have an attribute Visibility. Now when executing this as a macro then an exception is raised and the interpreter jumps out of the macro. That's why the rest of the macro won't be executed.

Now, the Python console works differently. It always gets the user input line by line and then for each added line the interpreter uses the compile function of the code module. Upon the return value it decides to either waits for the next line before executing, execute it now or raises and error because of syntax errors or indentation errors and so on.
If the executed code raises an exception it's handled immediately and then the interpreter waits for the next user input.

So, in your case this means it handles the errors with the Visibility attribute but then continues on executing the rest of the pasted code.

The standard Python interpreter (outside of FreeCAD) has exactly the same behaviour.
ian.rees wrote:Short answer is that, IMHO, some of the text handling in this area is currently broken in FreeCAD (the design is not what it should be, vs the code not behaving according to the design).
After all it is unrelated. The only issue was that the errors weren't reported when executing as macro and as triplus pointed out that it's now the default to redirect errors to the report view.
Post Reply