A few question about Wiki Script Object examples

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
User avatar
johnwang
Veteran
Posts: 1382
Joined: Sun Jan 27, 2019 12:41 am

A few question about Wiki Script Object examples

Post by johnwang »

Hi,

I am trying out the wiki Scripted Object examples and have a few questions. See https://www.freecadweb.org/wiki/Scripted_objects

The Basic example is all good, but when after running the Complex one, I can't see anything on the viewport.

Regards,

John

OS: Windows 10
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.17.13528 (Git)
Build type: Release
Branch: releases/FreeCAD-0-17
Hash: 5c3f7bf8ec51e2c7187789f7edba71a7aa82a88b
Python version: 2.7.14
Qt version: 4.8.7
Coin version: 4.0.0a
OCC version: 7.2.0
hfc series CAE workbenches for FreeCAD (hfcNastran95, hfcMystran, hfcFrame3DD, hfcSU2 and more)
User avatar
Joel_graff
Veteran
Posts: 1949
Joined: Fri Apr 28, 2017 4:23 pm
Contact:

Re: A few question about Wiki Script Object examples

Post by Joel_graff »

johnwang wrote: Sat Mar 30, 2019 5:46 am The Basic example is all good, but when after running the Complex one, I can't see anything on the viewport.
I'm currently rewriting the scripted object wiki, so I'm curious as to what doesn't work.

Post your code (use the code tags button </> when you do!) and your system information.. You can get the system information from Help -> About and click 'Copy to Clipboard'. Then paste the results in your message.
FreeCAD Trails workbench for transportation engineering: https://www.github.com/joelgraff/freecad.trails

pivy_trackers 2D coin3D library: https://www.github.com/joelgraff/pivy_trackers
User avatar
johnwang
Veteran
Posts: 1382
Joined: Sun Jan 27, 2019 12:41 am

Re: A few question about Wiki Script Object examples

Post by johnwang »

The basic example, I got a red box on the right. But the complex example, I can't see anything on the right..
hfc series CAE workbenches for FreeCAD (hfcNastran95, hfcMystran, hfcFrame3DD, hfcSU2 and more)
User avatar
Joel_graff
Veteran
Posts: 1949
Joined: Fri Apr 28, 2017 4:23 pm
Contact:

Re: A few question about Wiki Script Object examples

Post by Joel_graff »

johnwang wrote: Sat Mar 30, 2019 11:41 am The basic example, I got a red box on the right. But the complex example, I can't see anything on the right..
Right, but it helps to show your code as well. For example, I don't know if you created the ViewProvider and passed it a reference to your ViewObject (which is the last line of that code segment). That can be a chief cause. Another cause would be an error in your code. Is your icon in color or is it gray?

At very least, did you recompute the document? If there's a blue checkmark on the icon, then it means the object needs to be recomputed, so you need to click the two circling arrows (look kind of like a 'recycle' symbol). The other thing you could try is clicking the 'fit view' button. It's the button with the magnifying glass on top of the page at the left of the lower toolbar.
FreeCAD Trails workbench for transportation engineering: https://www.github.com/joelgraff/freecad.trails

pivy_trackers 2D coin3D library: https://www.github.com/joelgraff/pivy_trackers
User avatar
johnwang
Veteran
Posts: 1382
Joined: Sun Jan 27, 2019 12:41 am

Re: A few question about Wiki Script Object examples

Post by johnwang »

The icon is colored with a blue tick.After hiting recompute, all good.

I just copied that 3 part codes in one. Need to add a line for recompute to see the result.

The basic example also needs a recompute, but shows the cube well without the recomputer.

I noticed there is a few lines of string in "". Not led by a comment letter #, but python seems has no issue.
Last edited by johnwang on Sun Mar 31, 2019 1:39 am, edited 1 time in total.
hfc series CAE workbenches for FreeCAD (hfcNastran95, hfcMystran, hfcFrame3DD, hfcSU2 and more)
User avatar
johnwang
Veteran
Posts: 1382
Joined: Sun Jan 27, 2019 12:41 am

Re: A few question about Wiki Script Object examples

Post by johnwang »

Another question: For the basic example, Where I put the line:

Code: Select all

obj.supportedPropeties()
Is it just after this?

Code: Select all

a=FreeCAD.ActiveDocument.addObject("Appp::FeaturePython","Box")


I think I should also change to:

Code: Select all

a.supportedPropeties()
But run like that, I don't know where I could see the list of available properties, in Python console?
Last edited by johnwang on Sun Mar 31, 2019 7:58 am, edited 4 times in total.
hfc series CAE workbenches for FreeCAD (hfcNastran95, hfcMystran, hfcFrame3DD, hfcSU2 and more)
User avatar
Joel_graff
Veteran
Posts: 1949
Joined: Fri Apr 28, 2017 4:23 pm
Contact:

Re: A few question about Wiki Script Object examples

Post by Joel_graff »

johnwang wrote: Sun Mar 31, 2019 1:19 am But run like that, I don't know where I could see the list of available properties, in Python console?
You can call supportedProperties() on the object itself and it will tell you which ones it supports. The other thing you can do is type:

Code: Select all

>>> dir(a)
That will list all properties and methods available to the object.

As for the examples, I'm rewriting them completely to be simpler and hopefully clearer, so I'm probably not going to worry too much about corrections to the existing ones at the moment.
FreeCAD Trails workbench for transportation engineering: https://www.github.com/joelgraff/freecad.trails

pivy_trackers 2D coin3D library: https://www.github.com/joelgraff/pivy_trackers
User avatar
Joel_graff
Veteran
Posts: 1949
Joined: Fri Apr 28, 2017 4:23 pm
Contact:

Re: A few question about Wiki Script Object examples

Post by Joel_graff »

johnwang wrote: Sun Mar 31, 2019 1:14 am I noticed there is a few lines of string in "". Not led by a comment letter #, but python seems has no issue.
I suspect you're talking about the doc strings that show up just after the start of a method or class?

Code: Select all

def my_methd(self):
    """
    This is a docstring
    """
If so, those are documentation strings that are used for intellisense.

While I'm thinking of it, if you plan on doing nay significant Python development, you might want to set up a development environment if you haven't already. VSCode is good and has python support (including pylint, which tells you when you're writing badly-formatted or wrong code).

Maybe I should add a section like that to the documentation.

Incidentally, here's the first part of what I've been writing - I don't know if you'll find it useful:

https://www.freecadweb.org/wiki/FeaturePython_Objects
FreeCAD Trails workbench for transportation engineering: https://www.github.com/joelgraff/freecad.trails

pivy_trackers 2D coin3D library: https://www.github.com/joelgraff/pivy_trackers
User avatar
johnwang
Veteran
Posts: 1382
Joined: Sun Jan 27, 2019 12:41 am

Re: A few question about Wiki Script Object examples

Post by johnwang »

Happy to know the folder structure and py file. I was using FCMacro file before.

I thought the inspect attributes part maybe better talk later. I can't see the benefit I know it now.

No argument to call FreeCAD.getUserMarcoDir{) on my computer.

I am interested in making the object selectable.
hfc series CAE workbenches for FreeCAD (hfcNastran95, hfcMystran, hfcFrame3DD, hfcSU2 and more)
User avatar
Joel_graff
Veteran
Posts: 1949
Joined: Fri Apr 28, 2017 4:23 pm
Contact:

Re: A few question about Wiki Script Object examples

Post by Joel_graff »

johnwang wrote: Sun Mar 31, 2019 9:43 am No argument to call FreeCAD.getUserMarcoDir{) on my computer.
You shouldn't need to pass an argument...?
johnwang wrote: Sun Mar 31, 2019 9:43 am I am interested in making the object selectable.
The current Scripted Objects tutorial has a section on that, but I don't think it's necessary any longer. That is, if you base your scripted object on a Part::FeaturePython (instead of App::FeaturePython), and assign the appropriate ViewProvider, it should behave as a normal object in FreeCAD and should be selectable.

If you have a particular thing you want to create, I recommend starting with that (or a simple example like it), rather than just working through the tutorials. It's often more helpful to create something you want, rather than just reading and following tutorials. :)
FreeCAD Trails workbench for transportation engineering: https://www.github.com/joelgraff/freecad.trails

pivy_trackers 2D coin3D library: https://www.github.com/joelgraff/pivy_trackers
Post Reply