Correct use of PropertyLinkSub - 'NoneType' object is not subscriptable

Need help, or want to share a macro? Post here!
Post Reply
User avatar
Aleks
Posts: 260
Joined: Sun Mar 08, 2020 5:27 pm
Location: Bonn, Germany
Contact:

Correct use of PropertyLinkSub - 'NoneType' object is not subscriptable

Post by Aleks »

I have created a FeaturePython object and attached a vertex subobj to it with PropertyLinkSub

I get the following error at object creation, but not on object changes.

Code: Select all

15:13:42  Traceback (most recent call last):
  File "C:/Users/Aleksander/AppData/Roaming/FreeCAD/Macro\uselessbox.py", line 27, in execute
    link_sub = obj.LinkSub[0]
<class 'TypeError'>: 'NoneType' object is not subscriptable
This is the object code:

Code: Select all

class UselessBox():

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

        self.Type = 'box'

        obj.Proxy = self

        obj.addProperty('App::PropertyString', 'Description', 'Base', 'Box description').Description = "Just a simple box"
        obj.addProperty('App::PropertyLength', 'Length', 'Dimensions', 'Box length').Length = 10.0
        obj.addProperty('App::PropertyLength', 'Width', 'Dimensions', 'Box width').Width = 5.0
        obj.addProperty('App::PropertyLength', 'Height', 'Dimensions', 'Box height').Height = 10.0
        obj.addProperty("App::PropertyLinkSub", "LinkSub", "Subsection", "Description for tooltip").LinkSub = None

    def execute(self, obj):
        """
        Called on document recompute
        """
        # get the parent document object containing the subobject
        link_sub = obj.LinkSub[0]
        # get the list of sub object names from the LinkSub Property
        sub_obj_name_list = obj.LinkSub[1]
        # get the first name in the sub object name list
        sub_obj_name = sub_obj_name_list[0]
        # get the actual sub object by its name
        vertex = link_sub.getSubObject(sub_obj_name)

        vtx_vector = App.Vector(vertex.X - (obj.Length.Value * 0.5), vertex.Y  - (obj.Width.Value * 0.5), vertex.Z  - (obj.Height.Value * 0.5))

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


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

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

    UselessBox(obj)

    obj.ViewObject.Proxy = 0 # just set it to something different from None (this assignment is needed to run an internal notification)

    App.ActiveDocument.recompute()

    return obj
Everything I say is from the perspective of a mechanical engineer that is interested in tools to help create machines and other physical products. If you know what 2006/42/EG is, I would like to talk :)
User avatar
Aleks
Posts: 260
Joined: Sun Mar 08, 2020 5:27 pm
Location: Bonn, Germany
Contact:

Re: Correct use of PropertyLinkSub - 'NoneType' object is not subscriptable

Post by Aleks »

I suspect its because of this line, when the PropertyLinkSub gets assigned None. The LinkSub property gets assigned a real subobj shape later on.:

Code: Select all

obj.addProperty("App::PropertyLinkSub", "LinkSub", "Subsection", "Description for tooltip").LinkSub = None
But I dont know what to do, so this error does not come up.
Everything I say is from the perspective of a mechanical engineer that is interested in tools to help create machines and other physical products. If you know what 2006/42/EG is, I would like to talk :)
User avatar
Aleks
Posts: 260
Joined: Sun Mar 08, 2020 5:27 pm
Location: Bonn, Germany
Contact:

Re: Correct use of PropertyLinkSub - 'NoneType' object is not subscriptable

Post by Aleks »

Nevermind, I just passed the values to the custom obj class on object creation.

Code: Select all

class UselessBox():

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

        self.Type = 'box'

        obj.Proxy = self

        obj.addProperty('App::PropertyString', 'Description', 'Base', 'Box description').Description = "Just a simple box"
        obj.addProperty('App::PropertyLength', 'Length', 'Dimensions', 'Box length').Length = 10.0
        obj.addProperty('App::PropertyLength', 'Width', 'Dimensions', 'Box width').Width = 5.0
        obj.addProperty('App::PropertyLength', 'Height', 'Dimensions', 'Box height').Height = 10.0
        obj.addProperty("App::PropertyLinkSub", "LinkSub", "Subsection", "Description for tooltip").LinkSub = [sub_parent_obj, sub_elem_name]
Everything I say is from the perspective of a mechanical engineer that is interested in tools to help create machines and other physical products. If you know what 2006/42/EG is, I would like to talk :)
Post Reply