"PyCXX: Error" when completing FeaturePyton Objects-tutorial

Discussions about the wiki documentation of FreeCAD and its translation.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
brambo90
Posts: 5
Joined: Sun Oct 27, 2019 6:58 am

"PyCXX: Error" when completing FeaturePyton Objects-tutorial

Post by brambo90 »

Dear

This isn't really a bug, but it isn't really clear where to report 'mistakes' coming from the Wiki.

So I followed this tutorial: https://www.freecadweb.org/wiki/Creatin ... x,_Part_II
and I get an error in FreeCAD: "<unknown exception traceback><class 'TypeError'>: PyCXX: Error creating object of type class Py::SeqBase<class Py::Object> from None"

When reading this thread: https://forum.freecadweb.org/viewtopic.php?t=12966
I changed this part of the code (as presented on the wiki tutorial):

Code: Select all

   def getDisplayModes(self,obj):
       """
       Return a list of display modes.
       """
       return None
to this :

Code: Select all

   def getDisplayModes(self,obj):
       """
       Return a list of display modes.
       """
       return [color=#FF0000][][/color]

and the error vanished. Probably this is something that slipped through with the newer releases, no biggie. Could someone correct the wiki?

Cheers!
chrisb
Veteran
Posts: 53920
Joined: Tue Mar 17, 2015 9:14 am

Re: "PyCXX: Error" when completing FeaturePyton Objects-tutorial

Post by chrisb »

Thanks for reporting, I moved it to the Wiki forum.

If someone can confirm, I can change the wiki.
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
User avatar
Aleks
Posts: 309
Joined: Sun Mar 08, 2020 5:27 pm
Location: Bonn, Germany
Contact:

Re: "PyCXX: Error" when completing FeaturePyton Objects-tutorial

Post by Aleks »

This issue is still present. I just followed this tutorial. The tutorial is good in general, but I dont understand the meaning of this error message.
FreeCAD als Maschinenbauer in die Konstruktion und Fertigung integrieren. Schulung buchen: www.alsado.de/freecad-schulungen
User avatar
Roy_043
Veteran
Posts: 8450
Joined: Thu Dec 27, 2018 12:28 pm

Re: "PyCXX: Error" when completing FeaturePyton Objects-tutorial

Post by Roy_043 »

You need to give more details. With the full code (at the bottom of the page) I do not experience issues with:

Code: Select all

from fpo.box import box
mybox = box.create('my_box')
User avatar
Aleks
Posts: 309
Joined: Sun Mar 08, 2020 5:27 pm
Location: Bonn, Germany
Contact:

Re: "PyCXX: Error" when completing FeaturePyton Objects-tutorial

Post by Aleks »

This is the error I get:

Code: Select all

15:58:38  Traceback (most recent call last):
  File "C:/Users/Aleksander/AppData/Roaming/FreeCAD/Macro\fpo\box\box.py", line 90, in onChanged
    App.Console.PrintMessage("Change property: " + str(prop) + "\n")
<class 'TypeError'>: PyCXX: Error creating object of type class Py::SeqBase<class Py::Object> from None
This is the code to execute the code:

Code: Select all

from fpo.box import box
mybox = box.create('mybox')
This is the class and the object creation code:

Code: Select all

import FreeCAD as App
import Part

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

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

    box(obj)

    ViewProviderBox(obj.ViewObject)

    App.ActiveDocument.recompute()

    return obj


class box():

    def __init__(self, obj):
        """
        Default constructor
        """

        self.Type = 'box'

        obj.Proxy = self

        obj.addProperty('App::PropertyString', 'Description', 'Base', 'Box description').Description = ""

        obj.addProperty('App::PropertyLength', 'Length', 'Dimensions', 'Box length').Length = 10.0
        obj.addProperty('App::PropertyLength', 'Width', 'Dimensions', 'Box width').Width = '10 mm'
        obj.addProperty('App::PropertyLength', 'Height', 'Dimensions', 'Box height').Height = '1 cm'

    def execute(self, obj):
        """
        Called on document recompute
        """

        obj.Shape = Part.makeBox(obj.Length, obj.Width, obj.Height)

class ViewProviderBox:

    def __init__(self, obj):
        """
        Set this object to the proxy object of the actual view provider
        """

        obj.Proxy = self

    def attach(self, obj):
        """
        Setup the scene sub-graph of the view provider, this method is mandatory
        """
        return

    def updateData(self, fp, prop):
        """
        If a property of the handled feature has changed we have the chance to handle this here
        """
        return

    def getDisplayModes(self,obj):
        """
        Return a list of display modes.
        """
        return []

    def getDefaultDisplayMode(self):
        """
        Return the name of the default display mode. It must be defined in getDisplayModes.
        """
        return "Flat Lines"

    def setDisplayMode(self,mode):
        """
        Map the display mode defined in attach with those defined in getDisplayModes.
        Since they have the same names nothing needs to be done.
        This method is optional.
        """
        return mode

    def onChanged(self, vp, prop):
        """
        Print the name of the property that has changed
        """

        App.Console.PrintMessage("Change property: " + str(prop) + "\n")

    def getIcon(self):
        """
        Return the icon in XMP format which will appear in the tree view. This method is optional and if not defined a default icon is shown.
        """

        return """
            /* XPM */
            static const char * ViewProviderBox_xpm[] = {
            "16 16 6 1",
            "    c None",
            ".   c #141010",
            "+   c #615BD2",
            "@   c #C39D55",
            "#   c #000000",
            "$   c #57C355",
            "        ........",
            "   ......++..+..",
            "   .@@@@.++..++.",
            "   .@@@@.++..++.",
            "   .@@  .++++++.",
            "  ..@@  .++..++.",
            "###@@@@ .++..++.",
            "##$.@@$#.++++++.",
            "#$#$.$$$........",
            "#$$#######      ",
            "#$$#$$$$$#      ",
            "#$$#$$$$$#      ",
            "#$$#$$$$$#      ",
            " #$#$$$$$#      ",
            "  ##$$$$$#      ",
            "   #######      "};
            """

    def __getstate__(self):
        """
        Called during document saving.
        """
        return None

    def __setstate__(self,state):
        """
        Called during document restore.
        """
        return None

    def claimChildren(self):
        """
        Return a list of objects which will appear as children in the tree view.
        """
        return None
FreeCAD als Maschinenbauer in die Konstruktion und Fertigung integrieren. Schulung buchen: www.alsado.de/freecad-schulungen
User avatar
Roy_043
Veteran
Posts: 8450
Joined: Thu Dec 27, 2018 12:28 pm

Re: "PyCXX: Error" when completing FeaturePyton Objects-tutorial

Post by Roy_043 »

The claimChildren method causes the issue. It was added later to one of the steps, but not to the "Complete code" paragraph. It should not return None. You can remove it, or change it to:

Code: Select all

    def claimChildren(self):
        """
        Return a list of objects which will appear as children in the tree view.
        """
        return []
User avatar
Aleks
Posts: 309
Joined: Sun Mar 08, 2020 5:27 pm
Location: Bonn, Germany
Contact:

Re: "PyCXX: Error" when completing FeaturePyton Objects-tutorial

Post by Aleks »

Thank you!

Changing the claimChildren method got rid of the error.

New method:

Code: Select all

def claimChildren(self):
    """
    Return a list of objects which will appear as children in the tree view.
    """
    return []
FreeCAD als Maschinenbauer in die Konstruktion und Fertigung integrieren. Schulung buchen: www.alsado.de/freecad-schulungen
Post Reply