FreeCAD does not execute the python script as expected
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Be nice to others! Respect the FreeCAD code of conduct!
FreeCAD does not execute the python script as expected
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
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
Re: FreeCAD does not execute the python script as expected
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.
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.
Re: FreeCAD does not execute the python script as expected
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-
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-
Re: FreeCAD does not execute the python script as expected
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:
Instead you should use:
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.
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
Code: Select all
mybox.ViewObject.Visibility=False
Re: FreeCAD does not execute the python script as expected
??? The other thread is all about the debugging issues in the OP, how is it unrelated?triplus wrote:that discussion is more or less unrelated to your issue.
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.
- 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
I did it, and got this:foadsf wrote: If I copy and past the exact commands into the python console and run it works fine!
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
>>>
>>>
- 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
1. Correct.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
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.
Re: FreeCAD does not execute the python script as expected
foadsf wrote:1. FreeCAD does something in the GUI which does not report in the python console.
Correct or correct behaviour?DeepSOIC wrote:1. Correct.
- 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
Both. 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 beginstriplus wrote:foadsf wrote:1. FreeCAD does something in the GUI which does not report in the python console.Correct or correct behaviour?DeepSOIC wrote:1. Correct.
Re: FreeCAD does not execute the python script as expected
It doesn't work because you try to perform a wrong Python statement.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.
Code: Select all
mybox.Visibility=False
mybox_2.Visibility=False
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.
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.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).