Class not being assigned to variable

I am working on a set of scripts for a small project and I have encountered an error that I’m a little confused by (doesn’t take much lol).

I am trying to create an instance of a class using the name I give it as a variable.

The function I am using looks like this:


def CreateGearSystem (name, radius):
    import Gears.GearsClass as gears
    vars()[name] = gears.Gears(radius, name)

In theory what it should do is take the name I send it and create a instance of the gears class using the name as a variable to store the instance in.

Now the class gets created fine as when its created it creates a cylinder, however for some reason when ever I try to access the instance via the name I gave it it does not work.

Here is an example:

CreateGearSystem("Test",6)
# pymel.core : Updating pymel with pre-loaded plugins: DirectConnect, VectorRender, studioImport, mayaHIK, Mayatomr, ikSpringSolver
Test
# Error: NameError: file <maya console> line 1: name 'Test' is not defined # 

However if I run this in the python shell using a simple class it works fine.

class Test():
	def __init__(self,name):
		print name
>>> temp = "Cat"
>>> vars()[temp] = Test(temp)
Cat
>>> Cat
<__main__.Test instance at 0x00000000029DF4C8>
>>> 

From the tests I’ve done I cant see why its not working, it should take what ever the string is in name and use that as a variable to store the instance.

In the above example it takes what ever the string is in temp, which is “Cat” and uses that as the variable to store the instance in. So as you can see I can then access the instance using Cat.

Here is the class gears that seems to be causing the problem, note its a bit messy as it was written quickly as a test.

import ErrorsClass as CE
from pymel.core import *

class Gears():
    '''
    Gears Class. 
    '''

    def __init__(self, radius, name, parent = None):
        '''
        Radius of gears without teeth. If parent = None it will be created
        as a parent.
        '''
        
        self.radius = radius
        self.name = name
        
        # Check Value of Radius.
        try:
            if self.radius < 2:
                raise CE.RadiusError(self.radius)
            else:
                self.radius = int(round(self.radius, 0))
        except CE.RadiusError, (error):
            print error
            return # Stops the rest of the code executing.
        
        self.tempGearRadius = self.radius + 1.1 # Extra 1.1 unit for teeth.
        
        # Create Placement Geometry
        
        if parent == None:
            # Parent flag, allows easy test to see if it is parent later on.
            self.parent = True
            # Create Parent Gear. The driving gear.
            self.parentGear = polyCylinder(radius = self.tempGearRadius,
                                      height = 1,
                                      subdivisionsX = self.radius * 3,
                                      subdivisionsZ = 0,
                                      name = "%s_Parent" % self.name)[0]
            # Assign unique material for parent gear.  
            
    
    def addGear(self, parent):
        pass
                                    
           


I’m such a fool!

Its because the variable is being assigned in the functions name space so it cannot be access outside of the function.

Thats what I get for trying to use 3 different languages in one day lol.

I’ll find a different way to do it.

Sorry