Name is not defined error on script that previously worked

I’ve been working on an IK/FK joint matching script, that I managed to finally get working correctly last month. Today start I up Maya, run the script and while the interface loads up, I receive the following errors…

This one upon running loading script:

# Error: NameError: file <maya console> line 196: global name 'Fks' is not defined # 

and this one upon clicking my ‘select root joint chain’ button:

# Error: NameError: file <maya console> line 1: name 'select_joints_afk' is not defined # 

I’m really new to this so I really don’t understand what exactly is going on

class Create_Selection_Chains(object):
       
    def __init__(self, name, *args):
        self.Fks = Fks
        self.Ikw = Ikw
        self.Pv = ikpv
        self.name = name
        self.combined_selection = self.Fks+self.Ikw+self.Pv
        print("List created"+self.name)
    
    def select_joints_afk(self):
        
        if cmds.ls(selection = True,type=("transform",'nurbsCurve')):
            sel = cmds.ls(sl=True)
            fks = sel
            fkCtrls = cmds.listRelatives(sel, allDescendents=True, type=("transform",'nurbsCurve'))
            Fks = [nurbsCurve for nurbsCurve in fkCtrls if nurbsCurve.startswith('FK') & nurbsCurve.endswith('Ctrl')]
            cmds.textFieldButtonGrp(gtF0, edit = True, tx ='' .join(sel),buttonLabel='IK OK',backgroundColor = (.5,.8,.2))
            del Fks[1]
            del Fks[2]
            Fks.extend(sel)
            print Fks
            
            return self.Fks 
        else:
            text = cmds.confirmDialog( title='Error', message='Must select joint', button=['OK'], 
                                        defaultButton='Ok', dismissString='No' )

    def select_joints_aikw(self):
        
        if cmds.ls(selection = True,type=("transform",'nurbsCurve')):
            
            sel=cmds.ls(selection = True)
            ikwrist = sel
            Ikw = [nurbsCurve for nurbsCurve in ikwrist if nurbsCurve.startswith('IK') & nurbsCurve.endswith('Ctrl')]
            cmds.textFieldButtonGrp(gtF1, edit = True, tx ='' .join(ikwrist),buttonLabel='IK OK',backgroundColor = (.5,.8,.2))
            cmds.select(ikwrist)
            print Ikw
            return self.ikw
        else:
            text = cmds.confirmDialog( title='Error', message='Must select joint', button=['OK'], defaultButton='Ok', 
                                        dismissString='No' )

    def select_joints_ikpv(self):
        
        if cmds.ls(selection = True,type=("transform",'nurbsCurve')):
            sel = cmds.ls(sl=True)
            ikPvsel = sel
            ikpv = [nurbsCurve for nurbsCurve in ikPvsel if nurbsCurve.startswith('IK') & nurbsCurve.endswith('Ctrl')]
            cmds.textFieldButtonGrp(gtF2, edit = True, tx ='' .join(ikPvsel),buttonLabel='IK OK',backgroundColor = (.5,.8,.2))        
            cmds.select(ikPvsel)
            print ikpv
            return self.ikpv
        else:
            text = cmds.confirmDialog( title='Error', message='Must select joint', button=['OK'], 
                                        defaultButton='Ok', dismissString='No' )

Looking at another thread here, it seems that the error might have occurred because I closed Maya?

If you need to see the code in its entirety, I can send a link if it helps…

That sounds likely. Whenever you create a variable in Maya’s Script Editor, that variable lives on until you close Maya.

def func():
  print(message)

func()
message = "Hello World!"

This for example won’t run, until you’ve run the last line. Once you’ve run it, you could remove it, and the function would keep working until you restart Maya.

A way you can avoid this happening, is by passing variables you need for a function or class into it when called.

def function(message):
  print(message)

function("Hello World")

Now you aren’t able to call the function, regardless of whether you restart, until you pass it the required argument. In this case, I would suggest you pass the assumed variables into the class when you instantiate it.

class Create_Selection_Chains(object):
       
    def __init__(self, name, Fks, Ikw, ikpv, name):
        self.Fks = Fks
        self.Ikw = Ikw
        self.Pv = ikpv
        self.name = name

Now if the class can be instantiated, it won’t depend on whether you’ve restarted Maya or not.

1 Like

I think I understand.

So that means that since I am getting this error upon running the script now, its because the function is not running since only two of the five arguments are being passed?

please correct me if I’m wrong

# Error: TypeError: file <maya console> line 413: __init__() takes at least 5 arguments (2 given) #

Line 413 is where I have my class instances. Using one limb as an example:


            # SELECTION LIST INSTANCES # 

right_arm_select = Create_Selection_Chains("right arm")

Hey @hanaharu,

That’s correct - in your first example you were supplying variables that were out of scope (Fks, Ikw, etc…) and would need to be defined at the module level or as a global aka:


myVar = 10  #Defined at the module level.

class MyClass(object):

   def __init__(self):

       self._myVar = myVar #Defined ^ above to can be passed to this scope.

You would need to supply you arguments to the constructor so they could be passed to the instance when binding to a variable.

Now that you’r doing that you need to supply all the arguments that are without default values, else your’ll get an error thrown. This is the case with all methods.


def myFunc(a, b, c):
    print a, b, c  # a, b, c need to be passed.

def MyFunc(a=10, b=10, c=10):
   print a, b, c, # a, b, c have default assignments and dont need to be overidden.

1 Like