Re: passing variables as object attributes

Discussion in 'Python' started by Vikas Mahajan, Aug 16, 2010.

  1. On 16 August 2010 19:23, Nitin Pawar <> wrote:
    > you would need to define a class first with its attiributes and then you may
    > want to initiate the variables by calling the class initilializer
    >

    Actually I have to dynamically add attributes to a object. I am
    writing python script for FreeCAD software. I am using loop to create
    multiple cylinders and I am writing following code-:
    cyname = "Cylinder"
    FreeCAD.ActiveDocument.addObject("Part::Cylinder",cyname)
    FreeCAD.ActiveDocument.cyname.Radius= .5
    FreeCAD.ActiveDocument.cyname.Height= 10

    And I am getting this error-:
    AttributeError: 'App.Document' object has no attribute 'cyname'

    But when I use Cylinder in place of cyname, I am not getting any error.

    Please help.

    Thanks.
     
    Vikas Mahajan, Aug 16, 2010
    #1
    1. Advertising

  2. Vikas Mahajan

    nn Guest

    On Aug 16, 10:08 am, Vikas Mahajan <> wrote:
    > On 16 August 2010 19:23, Nitin Pawar <> wrote:> you would need to define a class first with its attiributes and then you may
    > > want to initiate the variables by calling the class initilializer

    >
    > Actually I have to dynamically add attributes to a object. I am
    > writing python script for  FreeCAD software. I am using loop to create
    > multiple cylinders and I am writing following code-:
    > cyname = "Cylinder"
    > FreeCAD.ActiveDocument.addObject("Part::Cylinder",cyname)
    > FreeCAD.ActiveDocument.cyname.Radius= .5
    > FreeCAD.ActiveDocument.cyname.Height= 10
    >
    > And I am getting this error-:
    > AttributeError: 'App.Document' object has no attribute 'cyname'
    >
    > But when I use Cylinder in place of cyname, I am not getting any error.
    >
    > Please help.
    >
    > Thanks.
     
    nn, Aug 16, 2010
    #2
    1. Advertising

  3. Vikas Mahajan

    nn Guest

    On Aug 16, 10:08 am, Vikas Mahajan <> wrote:
    > On 16 August 2010 19:23, Nitin Pawar <> wrote:> you would need to define a class first with its attiributes and then you may
    > > want to initiate the variables by calling the class initilializer

    >
    > Actually I have to dynamically add attributes to a object. I am
    > writing python script for  FreeCAD software. I am using loop to create
    > multiple cylinders and I am writing following code-:
    > cyname = "Cylinder"
    > FreeCAD.ActiveDocument.addObject("Part::Cylinder",cyname)
    > FreeCAD.ActiveDocument.cyname.Radius= .5
    > FreeCAD.ActiveDocument.cyname.Height= 10
    >
    > And I am getting this error-:
    > AttributeError: 'App.Document' object has no attribute 'cyname'
    >
    > But when I use Cylinder in place of cyname, I am not getting any error.
    >
    > Please help.
    >
    > Thanks.


    This might work:
    cyname = "Cylinder"
    FreeCAD.ActiveDocument.addObject("Part::Cylinder",cyname)
    getattr(FreeCAD.ActiveDocument,cyname).Radius= .5
    getattr(FreeCAD.ActiveDocument,cyname).Height= 10
     
    nn, Aug 16, 2010
    #3
  4. Vikas Mahajan a écrit :
    > On 16 August 2010 19:23, Nitin Pawar <> wrote:
    >> you would need to define a class first with its attiributes and then you may
    >> want to initiate the variables by calling the class initilializer
    >>

    > Actually I have to dynamically add attributes to a object. I am
    > writing python script for FreeCAD software. I am using loop to create
    > multiple cylinders and I am writing following code-:
    > cyname = "Cylinder"
    > FreeCAD.ActiveDocument.addObject("Part::Cylinder",cyname)
    > FreeCAD.ActiveDocument.cyname.Radius= .5
    > FreeCAD.ActiveDocument.cyname.Height= 10
    >
    > And I am getting this error-:
    > AttributeError: 'App.Document' object has no attribute 'cyname'


    Indeed.

    > But when I use Cylinder in place of cyname, I am not getting any error.


    Of course.


    > Please help.


    I don't have FreeCAD installed, I won't install it, and I'm not going to
    read FreeCAD's doc neither, but you may want to check whether
    FreeCAD.ActiveDocument.addObject actually returns the newly created
    objet (which would be a sensible behaviour). If so, you'd just have to
    keep a ref to this object, ie:

    cylinder = FreeCAD.ActiveDocument.addObject("Part::Cylinder",cyname)
    cylinder.Radius = 0.5
    # etc

    Else, you can always get this ref using getattr, ie:

    FreeCAD.ActiveDocument.addObject("Part::Cylinder",cyname)
    cylinder = getattr(FreeCAD.ActiveDocument, cyname)
    cylinder.Radius = 0.5
    # etc

    And while you're at it, you could save on both typing and execution time
    by keepin a ref to the document object itself:

    doc = FreeCAD.ActiveDocument

    for cyname in ("cylinder1, "cylinder2", "cylinderN"):
    doc.addObject("Part::Cylinder",cyname)
    cylinder = getattr(doc, cyname)
    cylinder.Radius = 0.5
    # etc

    HTH
     
    Bruno Desthuilliers, Aug 17, 2010
    #4
  5. I got the concept to get and set object attributes and now can handle
    similar problems effectively.

    Thanks to all for your help.
     
    Vikas Mahajan, Aug 17, 2010
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Max
    Replies:
    1
    Views:
    486
    Joe Kesselman
    Sep 22, 2006
  2. P4trykx
    Replies:
    2
    Views:
    1,833
    bruce barker
    Jan 31, 2007
  3. james_027

    class attributes & data attributes

    james_027, Jun 20, 2007, in forum: Python
    Replies:
    2
    Views:
    380
    Bruno Desthuilliers
    Jun 20, 2007
  4. Replies:
    9
    Views:
    970
  5. Vikas Mahajan

    passing variables as object attributes

    Vikas Mahajan, Aug 16, 2010, in forum: Python
    Replies:
    1
    Views:
    292
    misterdi
    Aug 16, 2010
Loading...

Share This Page