Navigation

    • Register
    • Login
        No matches found
    • Search
    1. Home
    2. happygrass_cn
    H

    happygrass_cn

    @happygrass_cn

    0
    Reputation
    1
    Posts
    23
    Profile views
    0
    Followers
    0
    Following
    Joined Last Online

    • Profile
    • More
      • Following
      • Followers
      • Topics
      • Posts
      • Best
      • Groups
    happygrass_cn Follow

    Best posts made by happygrass_cn

    This user hasn't posted anything yet.

    Latest posts made by happygrass_cn

    Can I reset the keyframe of an object through Python script?

    Hi, I am making a DEMO for my research work of crowd simulation. The people may walk and stop during the animation. The position and drection(Velocity) of the crowd have been recorded in a text file. I have written a Python script successfully read the data file at each keyframe. Now, I use TP partilce to animate the crowd. And, I subsitute the particle with a Man model through XPresso Pshape tags. Shown as followed.
    b3afd9da-2ab6-40ad-be83-c18da2d7dbc6-image.png

    When the position is not changed the model's motion should stop(stay at present keyframe) untill he moved again. The motion of the model is pre-rendered like a re-cycle movie.

    The keyframe of the Model as follow:
    26a31c95-5f23-46fa-81e0-454749ede650-image.png

    What should I do to hold on the present keyframe? Sorry ablout my poor question description!

    My Python scripts as followed:

    # Boids for Py4D by smart-page.net
    
    import c4d
    import math
    
    # particles' params
    boids_number = 1000
    currentframe = None
    
    # used for read user data frame by frame
    frame_step = boids_number+1 
    frame_total = 0
    
    
    def main():
        global tp
        global doc
        global currentframe    
        
        currentframe = doc.GetTime().GetFrame(doc.GetFps())
        
        # particles born at 0 frame
        if currentframe == 0:   
            tp.FreeAllParticles()
            tp.AllocParticles(boids_number)
                 
                    
        # life time for particles
        lt = c4d.BaseTime(1000)
    
        # user data for paritlces
        filename = op[c4d.ID_USERDATA, 1]
       
        # open the user file. First, read a frame of data from the user file. Then, read lines one bye one to feed the particles.
        with open(filename, 'r') as fn:
            
            # read all lines of the user data
            lines = fn.readlines()
            
            # compute how many frames of data in the file
            frame_total = int(len(lines) / frame_step)
            
            # 
            frame = 1
            i=0
    
            #read a frame of data according to the scene keyframe
            for frame in range(frame_total): 
                if frame == currentframe:
                    t_lines = lines[frame * frame_step:frame * frame_step + frame_step - 1]  
                    
                    #pase lines of the readed data 
                    for line in t_lines: 
                        if line == t_lines[0]:  # filter the first line of each frame in the text file, because is just flag words
                            print(line)                       
    
                        else:
                            #split position(x,y,z) and direction (dx,dy,dz)
                            x, y, z, dx, dy, dz = line.split()                      
                            pos = c4d.Vector(float(x), float(y), float(z) )
                            vol = c4d.Vector(float(dx), float(dy), float(dz))
                                                   
                            temp=(pos-tp.Position(i)).GetLength()  
                            
                            # some codes wanted here
                            if temp==0.0:
                                # the motion of the man should stop.
                                # should I  edit the keyframe of the walking Man model?
                                # when temp==0, then the walking Man stay at present keyframe but not go ahead along with the scene keyframe. 
                                                   
                                                 
                            
                           
                            # align to velocity direction
                            vel = vol.GetNormalized()
                            side = c4d.Vector(c4d.Vector(0, 1, 0).Cross(vel)).GetNormalized()
                            up = vel.Cross(side)
                            m = c4d.Matrix(c4d.Vector(0), side, up, vel)
                            tp.SetAlignment(i, m)
                           
                            # set position
                            tp.SetPosition(i,pos)
                            
                            # set life time for particle i
                            tp.SetLife(i, lt)  
                             
                            i=i+1
    
        c4d.EventAdd()
    
    if __name__=='__main__':
        main()
    code_text
    

    My user data format as followed.
    c6d1c4a1-6356-4120-a174-5dc56e19e6b2-image.png

    posted in General Talk •