FeaturePython : return obj vs fpo

Discussions about the wiki documentation of FreeCAD and its translation.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
Cyril
Posts: 133
Joined: Wed Aug 23, 2017 5:04 pm
Location: Geneva (Switzerland)
Contact:

FeaturePython : return obj vs fpo

Post by Cyril »

I followed FeaturePython_Objects tutorial. In my opinion FeaturePython creation should return obj and not fpo in following context :

Code: Select all

 def create(obj_name):
     """
     Object creation method
     """

     obj = App.ActiveDocument.addObject('App::FeaturePython', obj_name)

     fpo = box(obj)

     return fpo
It sounds more logic to me to return obj as it is what you want to use after creation in a FreeCAD context. Seeing eg. Arch/BIM workbench obj is returned, not fpo (eg. ArchWall). I don't see any common use case where it would be preferable to return fpo.

As I am pretty new to FeaturePython creation I wanted your opinion/approval on this point before altering this wiki page.
I blog about HVAC / BIM / Energy : pythoncvc.net. If you like you can follow the RSS feed.
vocx
Veteran
Posts: 5197
Joined: Thu Oct 18, 2018 9:18 pm

Re: FeaturePython : return obj vs fpo

Post by vocx »

Cyril wrote: Thu Sep 19, 2019 7:05 pm ...

As I am pretty new to FeaturePython creation I wanted your opinion/approval on this point before altering this wiki page.
On first instance I agree with you, it should return obj.

I had already thought this answer in my head "a lot of the programming documentation in the wiki is very old, so examples and tutorials should probably be thoroughly checked and rewritten".

However, I just noticed that this page was created earlier this year, March 2019, by Joel Graff, as "an update to Scripted objects". Joel likes to program in the best Pythonic style possible, keeping things modular and consistent, so maybe there is a reason why he wrote the examples that way. Let's ask him.
Joel_graff wrote: ping
Always add the important information to your posts if you need help. Also see Tutorials and Video tutorials.
To support the documentation effort, and code development, your donation is appreciated: liberapay.com/FreeCAD.
User avatar
Joel_graff
Veteran
Posts: 1949
Joined: Fri Apr 28, 2017 4:23 pm
Contact:

Re: FeaturePython : return obj vs fpo

Post by Joel_graff »

Honestly, I have no opinion.

Both results make sense to me, and I've never seen formal direction on that point. Given that each object references the other, I can't think of a reason to prefer one over the other, except that the general expectation for object creation is that a FreeCAD Document Object is returned, rather than the custom FPO. For me, in my use cases, I need the FPO reference more than I need the obj reference.

So.. whatever. I've seen both returned and have no opinion.

EDIT: Looking at this again, I'd agree returning the obj reference is probably the correct approach.
Last edited by Joel_graff on Fri Sep 20, 2019 11:39 am, edited 1 time in total.
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: FeaturePython : return obj vs fpo

Post by Joel_graff »

vocx wrote: Thu Sep 19, 2019 9:36 pm However, I just noticed that this page was created earlier this year, March 2019, by Joel Graff, as "an update to Scripted objects". Joel likes to program in the best Pythonic style possible, keeping things modular and consistent, so maybe there is a reason why he wrote the examples that way. Let's ask him.
BTW, thanks for the compliment. ;)
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
vocx
Veteran
Posts: 5197
Joined: Thu Oct 18, 2018 9:18 pm

Re: FeaturePython : return obj vs fpo

Post by vocx »

Joel_graff wrote: Fri Sep 20, 2019 3:27 am ... I need the FPO reference more than I need the obj reference.

So.. whatever. I've seen both returned and have no opinion.
...
Where have you seen this before? Do you do this in your own workbenches?

I've seen the code that Yorik writes, Draft, Arch, BIM, and it always returns the obj, so I assume that was standard practice. I haven't particularly looked into how FEM or Path, or other workbenches do it, so I can't comment on that.
Always add the important information to your posts if you need help. Also see Tutorials and Video tutorials.
To support the documentation effort, and code development, your donation is appreciated: liberapay.com/FreeCAD.
Cyril
Posts: 133
Joined: Wed Aug 23, 2017 5:04 pm
Location: Geneva (Switzerland)
Contact:

Re: FeaturePython : return obj vs fpo

Post by Cyril »

Joel_graff wrote: Fri Sep 20, 2019 3:27 am For me, in my use cases, I need the FPO reference more than I need the obj reference.
I would be interested to see some uses cases where you return fpo.

In my understanding :
  • If you return obj : you can still access fpo using

    Code: Select all

    obj.Proxy
  • If you return fpo : you can use fpo methods but it is not a FreeCAD Document Object and have no direct access to obj unless you add a reference to it during __init__
Edit : Found it. You add reference to obj during __init__ ;)
I blog about HVAC / BIM / Energy : pythoncvc.net. If you like you can follow the RSS feed.
User avatar
Joel_graff
Veteran
Posts: 1949
Joined: Fri Apr 28, 2017 4:23 pm
Contact:

Re: FeaturePython : return obj vs fpo

Post by Joel_graff »

Cyril wrote: Fri Sep 20, 2019 3:31 pm I would be interested to see some uses cases where you return fpo.
Right - like I say, the objects each reference each other, so it's a non-issue for me. The convention is to return the obj.
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
Cyril
Posts: 133
Joined: Wed Aug 23, 2017 5:04 pm
Location: Geneva (Switzerland)
Contact:

Re: FeaturePython : return obj vs fpo

Post by Cyril »

I looked into modifications history and found an intriguing comment by tomkcook :
tomkcook wrote: (cur | prev) 11:40, 1 July 2019‎ Tomkcook (talk | contribs)‎ . . (18,461 bytes) (-183)‎ . . (Remove references to `self.Object`- it causes serialisation problems) (undo)
Diff here
Would you mind explaining a little more ?
I blog about HVAC / BIM / Energy : pythoncvc.net. If you like you can follow the RSS feed.
Post Reply