Problem working with externally defined class

Hello people of the forum! I’ve just started working with rhino script and am relatively new to Python (though quite experienced in other languages) I’m on a mac and developing using the Atom text editor.

The issue I’m having is as follows:

I have written a fairly straightforward class called CombinationEngine located in a file called CombinationEngineClass.py (2.5 KB) the code is attached as discourse is having trouble formatting it properly.

If I instantiate this class in the same file like this I have no issues and the code runs fine in rhino (there are supposed to be underscores on name and main)

if( name == “main” ):
combiner = CombinationEngine()
# combiner.thing(“asdf”)
combiner.add_objects()
combiner.duplicate_objects(20)
combiner.make_salad([-600, 600], [-1000, 1000], [0, 500])

If I do the same from a seperate file salad.py (located in the same directory) the class initializes no problem, but when I try and call any of the methods I get this error message

Message: CombinationEngine instance has no attribute 'add_objects'

the only other code: salad.py (288 Bytes)

What could be going on here? Does this have anything to do with running the code through Atom? I am super confused!

Thanks, I’m sure it’s something silly, but would love to get some fresh eyes on this.

@smm,

i have saved both your scripts in a new empty folder named “Folder” and created a Rhino file containing only a box, then saved this file in the same folder. After this i´ve opened a new empty document and started the second script salad.py.

The script starts by asking me for a folder. If i select “Folder”, the box is imported and duplicated. Then i get this error:

Message: global name 'jj' is not defined

The error is pointing to the function duplicate_objects(). In this function, you´re trying to iterate over a variable jj in line 42 which obviously is not defined anywhere in the function. If i replace the variable name jj with container, it runs through and the box is spread randomly as you´ve probably intended.

This is what happens on Windows. So I get past the the error of add_objects() you´ve reported. I´ve also made sure that the function add_objects() is available. It is, once the class instance of CombinationEngine has been created.

From my point of view, it looks like the import statement of the CombinationEngine in salad.py is the source of the problem. To veryfy, run this:

combiner = CombinationEngine()
print dir(combiner)

What is listed ?

c.

Hi Clement,

Thanks for taking a look! When I run the code you suggest I get the following in the console:

['AddObjectsToFile', 'MakeSalad', '__doc__', '__init__', '__module__', 'objects']

Which is very odd if those are supposed to be the available method names as I renamed them a day ago…also if it is supposed to be a complete list of methods I have since added several more.

@smm,

That is odd indeed. You can create the class instance without error, but it looks like all the class methods are not imported as they should. What happens if you change the import statement and then access with full name eg:

import CombinationEngineClass
combiner = CombinationEngineClass.CombinationEngine()
print dir(combiner) 

Apart from this, i do not know what to try as it works fine under Windows (it prints all class methods over here)

c.

dang, I get the same message… hmmmmmmmm I wonder what it is!

For now I can keep working with just the one file, but this certainly is limiting if there isn’t a way around it.

Hi,

Shot in the dark, did you try and restart your system?
Or:
Is there a means in Atom to reset?
In the Win pytho-neditor there is an option to “reset script engine” I need to use that all the time to update changed libraries.

-Willem

1 Like

oh hm, well that seems to do it…if I restart rhino my changes are made visible. Unfortunately there doesn’t seem to be any “reset script engine” command on the mac which sucks as restarting rhino every time I make a change to a script is pretty impractical.

Anyhow, I can get by for now just working with one file, but I’m wondering if there is anything I can do to get something like a reset script engine option for mac?

@smm,

i´m wondering if you could check the python scope for availability of the class (within try:except), if it is found, delete it using del and then re-import it ?

c.

Hi Clement and @smm,

How about reloading the module like so:


reload(module)

I believe best practice is to only do this when developing and leave it out for the final script.

HTH
-Willem

1 Like

Thank you @Willem,

good find ;). I use external classes and modules so rarely…

c.

Great! Thanks @Willem the reload did the trick.

Hey there,

it seems the reload() question arises more frequently. Here I outlined a solution that generalizes to whole namespaces / packages and doesn’t require you to manually reload every file that changes.

Note that this should also be done only during development!, as it heavily influences performance.

Hope it helps for someone!

Cheers,
Max