Class Object

Class Object

Peter__B
Advocate Advocate
661 Views
3 Replies
Message 1 of 4

Class Object

Peter__B
Advocate
Advocate

Hello

 

Part of my python code contains the following:

 

class ParameterChange:

      def __init__(self,dia, length):

           self.DIA = dia

           self.LENGTH = length

 

If I execute it and also run e.g.

 

param = ParameterChange(1.1,2.2), I will get for param.DIA...1.1 but python says that param.LENGTH does not exist. Just the first works.

 

In standard python it works but not in the Spyder version in fusion 360.

 

Does anybody know why ?

 

Best Regards

Peter

 

0 Likes
Accepted solutions (2)
662 Views
3 Replies
Replies (3)
Message 2 of 4

JeromeBriot
Mentor
Mentor
Accepted solution

Hello,

 

the following script works as expected:

 

import adsk.core, traceback

class ParameterChange:

      def __init__(self,dia, length):

           self.DIA = dia
           self.LENGTH = length

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface

        param = ParameterChange(1.1,2.2)

        ui.messageBox('{} , {}'.format(param.DIA, param.LENGTH))

    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
Message 3 of 4

Peter__B
Advocate
Advocate
Accepted solution

Is it necessary to have all statements e.g. in the run function ? or can I have them outside to ? perhaps it special for "python Spyder fusion script" ?

 

/Peter

0 Likes
Message 4 of 4

Anonymous
Not applicable

In short, no. You can write your code outside the "run" method but you need to hook into "events" to do so - see this help topic.

 

I write my add-ins like proper python with objects, modules, packages, the whole bit. See this post for my boiler plate code which uses some black magic to abstract away the irritating "event object instance" pains.

0 Likes