def parse_node(self, node, parent=None):
        
        
        current_node = None
        node_name = None
        
        if "name" in node.attrib:
            node_name = node.attrib["name"]
                
        if node.tag == "scene":
            if node_name is None:
                node_name = "default_scene" 
            
            current_node = Node(name=node_name)
        
        elif node.tag == "camera":
            current_node = Camera()
            # SIGH - There is no scene at this point because the loading has just begun
            # If the current scene doesn't yet have an active camera
#            if not parent is None and \
#               isinstance(parent, Node) and \
#               parent.scene.active_camera is None:
#                # Set this camera as the active camera
#                parent.scene.active_camera = current_node
                    
                        
        elif node.tag == "light":
            current_node = Light()
        
        elif node.tag == "node":
            current_node = Node()
            
            if not node_name is None and not current_node is None:
                if hasattr(current_node, 'name'):
                    current_node.name = node_name
        
        # Child Nodes
        
        # Transform
        elif node.tag == "transform":
            if not parent is None:
                
                transform = None
                if isinstance(parent, Transform):
                    transform = parent
                elif isinstance(parent, Node):
                    transform = parent.transform
                
                if "position" in node.attrib:
                    coord = node.attrib["position"].split(" ")
                    if len(coord) == 3:
                        coord = Vector3(float(coord[0]), float(coord[1]), float(coord[2]) )
                    else:
                        self._log.Log("Invalid position: [%s]" % (" ".join(coord)))
                    
                    transform.position = coord
                
                if "rotation" in node.attrib:
                    rot = node.attrib["rotation"].split(" ")
                    if len(rot) == 4:
                        rot = Quaternion().new_rotate_axis(float(rot[0]), Vector3(float(rot[1]),float(rot[2]),float(rot[3])))
                    else:
                        self._log.Log( "Invalid rotation: [%s]" % (" ".join(rot)) )
                    
                    transform.rotation = rot
                    
                if "scale" in node.attrib:
                    scale = node.attrib["scale"].split(" ")
                    if len(scale) == 3:
                        scale = Vector3(float(scale[0]), float(scale[1]), float(scale[2]) )
                    else:
                        self._log.Log( "Invalid scale: [%s]" % (" ".join(scale)) )
                    
                    transform.local_scale = scale
        # Colour
        elif node.tag == "colour":
            if not parent is None:
                colour_object = None
                
                if isinstance(parent, Light) and not parent.light is None:
                    colour_object = parent.light
                elif isinstance(parent, BaseMaterial):
                    colour_object = parent
                    
#                if not colour_object is None:
                if "ambient" in node.attrib:
                    colour_object.ambient = Colour.from_string(node.attrib["ambient"])
                
                if "diffuse" in node.attrib:
                    colour_object.diffuse = Colour.from_string(node.attrib["diffuse"])
                
                if "specular" in node.attrib:
                    colour_object.specular = Colour.from_string(node.attrib["specular"])
                        
        elif node.tag == "attenuation":
            if not current_node is None and isinstance(current_node, Node):
                if not current_node.light is None:
                    if "value" in node.attrib:
                        atten = node.attrib["value"].split(" ")
                        if len(atten) == 1:
                            atten = float(atten)
                        else:
                            self._log.Log( "Invalid attenuation: [%s]" % (str(atten)) )
                        
                        current_node.light.attenuation = float(atten)
#                        if isinstance(parent, GLLight):
#                            parent.attenuation = float(atten)
        
        # Mesh
        elif node.tag == "mesh":
            if not isinstance(parent, Light) or not isinstance(parent, Camera):
                if "preset" in node.attrib:
                    preset = node.attrib["preset"]
                    if preset in MeshTypesString.MESHES:
                        parent.renderer.mesh = MeshFactory.get_mesh(MeshTypesString.MESHES[preset])
                    else:
                        self._log.Log( "Invalid Mesh Preset: [%s]" % ( preset ) )
                        
                if "filename" in node.attrib:
                    
                    # Get the name
                    name = None
                    if 'name' in node.attrib:
                        name = node.attrib['name']

                    # Load the mesh
                    filename = node.attrib["filename"]
                    mesh = ResourceManager.get_instance().process_resource(filename, name)

                    # Set it to the node
                    parent.renderer.mesh = mesh
        
        # Children        
        elif node.tag == "children":
            if not parent is None:
                # Get the child nodes of the children Element
                
                children = node.getchildren()
                
                # Process each of the child elements found.
                for child in children:
                    self.parse_node(child, parent)
            
        # Scripts
        elif node.tag == "scripts":
            if not parent is None:
                # Get the scripts applied to this node
                scripts = node.getchildren()
                
                # Process and attach each of the scripts
                for script in scripts:
                    self.parse_node(script, parent)
                    
        elif node.tag == "script":
            # If this tag is used as a child tag of another node.
            if not parent is None:
                script_name = node.attrib["name"]
                parameters = {}
                for key in node.attrib:
                    if not key == "name":
                        parameters[key] = node.attrib[key]
                        
                # The current limitation of this is that the only scripts that can be used
                # are the ones listed in TestScripts.  TBD: Change to dynamic import, allow
                # files to be included in the scene description.
                
                # The python files listed in the scene definition need to be parsed and loaded
                # into the engine before this parsing occurs if this is to be made more
                # flexible
                
                # Create the Script
                script = None
                try:
                    script = getattr(testscripts, script_name)
                    new_script = script()
                except Exception, e:
                    self._log.Log("Could not create Script with name: " + script_name)
                    self._log.Log("Message: " + e.message)
                    script = None
                
                if not script is None:
                    try:
                        # Initialise it using the parameters in the XML
                        
                        new_script.init_parameters(parameters)
                        parent.add_script(new_script)
                    except Exception, e:
                        self._log.Log("Invalid parameters for script: " + script_name)
                        self._log.Log("parameters: " + str(parameters))
Example #2
0
    def __init__(self):
        # Load Configuration settings
        ConfigurationManager.load_configuration()

        #Start Logger
        method = Settings.get('LoggerSettings','method')
        filename = Settings.get('LoggerSettings','filename')
        to_console = Settings.get('LoggerSettings','log_to_console')
        Logger.Configure(method, filename, to_console)
        Logger.Log('Initialising Core Systems')
        
        # The order of initialisation in this function is extremely important
        # as it determines the order in which classes will receive events.
        # For instance it is important that input is processed
        
        # Start Event System so it can receive new listeners when the
        # classes below are initialised 
        self._event_manager = EventManager.get_instance()
        
        #Register Core Event Listener for detecting quit
        self._core_listener = CoreListener(False)
        
        # Initialise Time
        self._time = Time.get_instance()
        
        # Create a Scene
        self._scene = SceneManager.get_instance()
        self._scene.init()
        
        #Start Render System
        self._render_manager = RenderManager.get_instance()

        self._render_manager.init( Settings.get('DisplaySettings','resolution')[0],
                                   Settings.get('DisplaySettings','resolution')[1],
                                   Settings.get('DisplaySettings','window_title')
                                 )
        
        # Setup the framework callbacks
        self._framework = FrameworkManager.framework()
        self._framework.setup = self.setup
        self._framework.run_loop = self.update
        self._framework.on_draw = self._render_manager.draw
        self._framework.on_shutdown = self.shutdown
                
        # Setup Frame Start/End Events
        self._frame_start = FrameStarted()
        self._frame_end = FrameEnded()
        
        #Get the Texture Manager
        self._texture_manager = TextureManager.get_instance()
        
        # Start the ShaderManager
        self._shader_manager = ShaderManager.get_instance()
        
        # Start Scripting System
        self._script_manager = ScriptManager.get_instance()
        
        # Start the UI Manager and setup the MainUI
        self._ui_manager = UIManager.get_instance()
        self._main_ui = MainUI()
        
        # Start the GizmoManager system
        self._gizmo_manager = GizmoManager.get_instance()
        
        # Configure the ResourceManager
        self._resource_manager = ResourceManager.get_instance()
        # enable Image Loading
        self._resource_manager.add_handler(ImageResourceHandler())
        # enable Scene Loading
        self._resource_manager.add_handler(SceneResourceHandler())
        # enable Wavefront Obj Loading
        self._resource_manager.add_handler(WavefrontResourceHandler())
        
        # Resources
        elif node.tag == "texture":
            texture = None
            
            # Get the name
            name = None
            if "name" in node.attrib:
                name = node.attrib["name"]

            # If the texture is referenced by filename
            if "filename" in node.attrib:
                
                # Load the texture
                filename = node.attrib["filename"]
                texture = ResourceManager.get_instance().process_resource(filename, name)                    
            
            # If the texture is just a reference, retrieve it from the TextureManager for assignment
            if texture is None and name is not None:
                texture = TextureManager.get_instance().get_texture_by_name(name)
            
            # If the current parent is a Material node, set the material node's texture 
            if not parent is None:
                if isinstance(parent, BaseMaterial) and not texture is None:
                    parent.texture = texture
        
#        elif node.tag == "shader":
#            pass
        
        # Unknown XML tag    
        else: