Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: passing variables as object attributes

Reply
Thread Tools

Re: passing variables as object attributes

 
 
Vikas Mahajan
Guest
Posts: n/a
 
      08-16-2010
On 16 August 2010 19:23, Nitin Pawar <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
nn
Guest
Posts: n/a
 
      08-16-2010
On Aug 16, 10:08*am, Vikas Mahajan <(E-Mail Removed)> wrote:
> On 16 August 2010 19:23, Nitin Pawar <(E-Mail Removed)> 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.


 
Reply With Quote
 
 
 
 
nn
Guest
Posts: n/a
 
      08-16-2010
On Aug 16, 10:08*am, Vikas Mahajan <(E-Mail Removed)> wrote:
> On 16 August 2010 19:23, Nitin Pawar <(E-Mail Removed)> 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

 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      08-17-2010
Vikas Mahajan a écrit :
> On 16 August 2010 19:23, Nitin Pawar <(E-Mail Removed)> 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
 
Reply With Quote
 
Vikas Mahajan
Guest
Posts: n/a
 
      08-17-2010
I got the concept to get and set object attributes and now can handle
similar problems effectively.

Thanks to all for your help.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
passing variables as object attributes Vikas Mahajan Python 1 08-16-2010 03:34 PM
Put variables into member variables or function variables? tjumail@gmail.com C++ 9 03-23-2008 04:03 PM
While executing the class definition which object is referenced bythe first argument of the class method, Y r Object attributes not allowed asdefault arguments Krishna Python 4 03-07-2008 09:44 PM
Parse reserved attributes as normal attributes Max XML 1 09-22-2006 12:04 PM
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM



Advertisments