peng3d/model.py
def setAnimation(self,obj,animation,transition=None,force=False):
        """
        Sets the animation to be used by the object.
        
        See :py:meth:`Actor.setAnimation()` for more information.
        """
        self.ensureModelData(obj)
        data = obj._modeldata
        
        # Validity check
        if animation not in self.modeldata["animations"]:
            raise ValueError("There is no animation of name '%s' for model '%s'"%(animation,self.modelname))
        
        if data.get("_anidata",{}).get("anitype",None)==animation and not force:
            return # animation is already running
        
        # Cache the obj to improve readability
        anim = self.modeldata["animations"][animation]
        
        # Set to default if not set
        if transition is None:
            transition = anim.default_jt
        
        # Notify the animation to allow it to initialize itself
        anim.startAnimation(data,transition)
        
        # initialize animation data
        if "_anidata" not in data:
            data["_anidata"]={}
        adata = data["_anidata"]
        adata["anitype"]=animation
        
        if "_schedfunc" in adata:
            # unschedule the old animation, if any
            # prevents clashing and crashes
            pyglet.clock.unschedule(adata["_schedfunc"])
        
        # Schedule the animation function
        def schedfunc(*args):
            # This function is defined locally to create a closure
            # The closure stores the local variables, e.g. anim and data even after the parent function has finished
            # Note that this may also prevent the garbage collection of any objects defined in the parent scope
            anim.tickEntity(data)
        
        # register the function to pyglet
        pyglet.clock.schedule_interval(schedfunc,1./(anim.kps if anim.atype=="keyframes" else 60))
        # save it for later for de-initialization
        adata["_schedfunc"] = schedfunc
def setAnimation(self,obj,animation,transition=None,force=False):
        """
        Sets the animation to be used by the object.
        
        See :py:meth:`Actor.setAnimation()` for more information.
        """
        self.ensureModelData(obj)
        data = obj._modeldata
        
        # Validity check
        if animation not in self.modeldata["animations"]:
            raise ValueError("There is no animation of name '%s' for model '%s'"%(animation,self.modelname))
        
        if data.get("_anidata",{}).get("anitype",None)==animation and not force:
            return # animation is already running
        
        # Cache the obj to improve readability
        anim = self.modeldata["animations"][animation]
        
        # Set to default if not set
        if transition is None:
            transition = anim.default_jt
        
        # Notify the animation to allow it to initialize itself
        anim.startAnimation(data,transition)
        
        # initialize animation data
        if "_anidata" not in data:
            data["_anidata"]={}
        adata = data["_anidata"]
        adata["anitype"]=animation
        
        if "_schedfunc" in adata:
            # unschedule the old animation, if any
            # prevents clashing and crashes
            pyglet.clock.unschedule(adata["_schedfunc"])
        
        # Schedule the animation function
        def schedfunc(*args):
            # This function is defined locally to create a closure
            # The closure stores the local variables, e.g. anim and data even after the parent function has finished
            # Note that this may also prevent the garbage collection of any objects defined in the parent scope
            anim.tickEntity(data)
        
        # register the function to pyglet
        pyglet.clock.schedule_interval(schedfunc,1./(anim.kps if anim.atype=="keyframes" else 60))
        # save it for later for de-initialization
        adata["_schedfunc"] = schedfunc
Sets the animation to be used by the object.
        
        See :py:meth:`Actor.setAnimation()` for more information.