passing variables as object attributes

nn pruebauno at latinmail.com
Mon Aug 16 10:30:07 EDT 2010


On Aug 16, 10:08 am, Vikas Mahajan <vikas.mahaja... at gmail.com> wrote:
> On 16 August 2010 19:23, Nitin Pawar <nitinpawar... at gmail.com> 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




More information about the Python-list mailing list