NameError: global name is not defined [SOLVED]

On 23/10/2014 at 13:02, xxxxxxxx wrote:

Hi,

I have a problem when calling my function IterateHierarchy.
It raises the NameError: global name 'IterateHierarchy' is not defined

I've looked at many topics to fix this problem;
I came upon this: Your function should be defined before you call it.

But the weird thing for me is, that it is defined before I call it. This makes me kinda confused.

Here is my code:

import c4d
import os
import sys
from c4d import gui, plugins, bitmaps

PLUGIN_ID = 1000002

BPBUTTON = 1002
CROBUTTON = 1011

askedforbase = False
control = False

class ControlPoseHandler(plugins.TagData) :
  pass

def GetNextObject(op) :
      if op == None:
          return None

if op.GetDown() :
          return op.GetDown()

while not op.GetNext() and op.GetUp() :
          op = op.GetUp()

return op.GetNext()

def IterateHierarchy(op) :
      if op is None:
          return

count = 0

while op:
          count += 1
          print op.GetName()
          op = GetNextObject(op)

return count

def Init(self, node) :
      tag = node
      data = tag.GetDataInstance()

global askedforbase

if askedforbase == False:
          global askbase
          askbase = gui.QuestionDialog("Are you sure this is the base pose?")
          askedforbase = True

global doc
          doc = c4d.documents.GetActiveDocument()
          tagobject = tag.GetObject()
          global controlobject
          controlobject = doc.SearchObject("Control_Goal")

global baseposition
          baseposition = controlobject.GetAbsPos()

tagobject = tag.GetObject()
      count = IterateHierarchy(tagobject)

return True

def Execute(self, tag, doc, op, bt, priority, flags) :
      data = tag.GetDataInstance()

if askbase == False:
          tag.Remove()

print "The askedforbase is: ", askedforbase

return True

def Message(self, node, type, data) :
      if type == c4d.MSG_DESCRIPTION_COMMAND:
          if data['id'][0].id == BPBUTTON:
              doc.StartUndo()
              doc.AddUndo(c4d.UNDOTYPE_CHANGE, controlobject)
              controlobject.SetAbsPos(baseposition)
              doc.EndUndo()

return True

if __name__ == "__main__":
  bmp = bitmaps.BaseBitmap()
  dir, file = os.path.split(__file__)
  bitmapfile = os.path.join(dir, "res", "ControlPoseHandler.tif")
  result = bmp.InitWith(bitmapfile)
  if not result:
      print "Error loading bitmap!"

okyn = plugins.RegisterTagPlugin(id = PLUGIN_ID, str = "ControlPoseHandler", info = c4d.TAG_VISIBLE | c4d.TAG_EXPRESSION, g = ControlPoseHandler, description = "ControlPoseHandler", icon = bmp)
  print "ControlPoseHandler initialized", okyn

Thanks for your help!!

On 23/10/2014 at 14:50, xxxxxxxx wrote:

Hi,

self.IterateHierarchy(tagobject)

will do it.

it´s a function which is defined inside your class.

Best wishes
Martin

On 23/10/2014 at 15:11, xxxxxxxx wrote:

Thanks!! that does it.

I also had to change my functions to:

def GetNextObject(self, op) :
      if op == None:
          return None

if op.GetDown() :
          return op.GetDown()

while not op.GetNext() and op.GetUp() :
          op = op.GetUp()

return op.GetNext()

def IterateHierarchy(self, op) :
      if op is None:
          return

count = 0

while op:
          count += 1
          print op.GetName()
          op = self.GetNextObject(op)

return count

On 23/10/2014 at 15:20, xxxxxxxx wrote:

you´re welcome !