function 'register' is not defined I am trying to run a custom script when Blender starts.

My script location is


  C:\Program Files (x86)\Blender Foundation\Blender\2.70\scripts\startup


My idea is to start a TCP server on Blender start up, but Blender is complaining:


  NameError: name 'register' is not defined


This is my script:

import bpy
import socketserver

class MyTCPHandler(socketserver.BaseRequestHandler):

def handle(self):
    # self.request is the TCP socket connected to the client
    self.data = self.request.recv(1024).strip()
    print("{} wrote:".format(self.client_address[0]))
    print(self.data)
    # just send back the same data, but upper-cased
    self.request.sendall("[MOVE]MessageReceived[END]")

def register():
    bpy.utils.register_class(MyTCPHandler)

def unregister():
    bpy.utils.unregister_class(MyTCPHandler)

if __name__ == "__main__":

    register()  

    # Create the server, binding to localhost on port 9999
    server = socketserver.TCPServer(("localhost", 9999), MyTCPHandler)

    # Activate the server; this will keep running until you
    # interrupt the program with Ctrl-C
    server.serve_forever()
The script has a few issues, first indentation seems not correct and seems to cause the error (could be caused by copy and paste). 

Python relies on the indentation of code because it doesn't use braces to separate code segments syntactically.

Before a class can be registered as an operator the class must be inherit from bpy.types.Operator or bpy.types.Menu (see the docs whether there're more options).

The socketserver constructor is not invoked.

Related:


https://stackoverflow.com/questions/3277367/how-does-pythons-super-work-with-multiple-inheritance
Blenders build-in text editor provides some examples of operators as Templates acessible from the Menu.