抛物线工具


import os
import sys
import ctypes.wintypes
 
def getDocPath(pathID=5):
    '''path=5: My Documents'''
    buf= ctypes.create_unicode_buffer(ctypes.wintypes.MAX_PATH)
    ctypes.windll.shell32.SHGetFolderPathW(None, pathID, None, 0, buf)
    return buf.value

Py_startup_path = os.path.expanduser('~/documents/MB/2018-x64/config/PythonStartup')
Py_startup_path = os.path.expanduser('~/documents/MB/2018-x64/config/PythonStartup/RHackParabolic')
if not Py_startup_path in sys.path:
    sys.path.append(Py_startup_path)




conf =  os.path.expanduser('~/documents/MB/2018-x64/config')
#conf =  os.path.expanduser('~/documents/MB/2018-x64/PythonStartup/MobuCurveEdit')
conf =  os.path.expanduser('~/documents/MB/2018-x64/PythonStartup/RHackParabolic')
file_path = os.path.join(conf,'selection.rick')
import json

from pyfbsdk import *
from pyfbsdk_additions import *
import math


Controls = {}
InitialSpeed = 500
TargetSpeed = 0
TimeToTargetSpeed = 30
AxisToMove = 1
OffsetAngle = 0
AddAnimationToCurrent = False
AutoConstantForAfterFrames = False

#Begining of function definitions

#######################################


def OnSimulatePressed(Control, Event):
    
    Cube = FBFindModelByLabelName('Animation Movement Hint')
    if(Cube == None):
        Cube = FBModelCube("Animation Movement Hint")
        Cube.Show = True
        Cube.Scaling = FBVector3d(10, 10, 10)
    
    Cube.Translation.SetAnimated(True)
    AnimNode = Cube.Translation.GetAnimationNode()
    
    ###
    global AxisToMove
    if(AxisToMove == 0):
        AxisToMove = 1
    
    AxisToMoveAbs = math.fabs(AxisToMove)
    
    MovementDir = 0
    
    if(AxisToMoveAbs == 1):
        MovementDir = FBVector3d(1, 0, 0)
    elif(AxisToMoveAbs == 2):
        MovementDir = FBVector3d(0, 1, 0)
    else:
        MovementDir = FBVector3d(0, 0, 1)

    global AddAnimationToCurrent

    if AddAnimationToCurrent == False:
        InitialTranslation = FBVector3d(0, 0, 0)
        for Index in range(0, 3):
            AnimNode.Nodes[Index].FCurve.EditClear()
    else:
        InitialTranslation = Cube.Translation.Data 

    #####
    Player = FBPlayerControl()
    #Player.GotoStart()
    Fps = Player.GetTransportFpsValue()
    
    #####
    TimeToTargetInSeconds = TimeToTargetSpeed / Fps    
        
    #####
    if(AxisToMove < 0):
        MovementDir *= -1
    
    MovementDir = RotateVectorByOffsetAngle(OffsetAngle,
    FBVector4d(MovementDir[0], MovementDir[1], MovementDir[2], 0))
    
    
    Acceleration = MovementDir * (TargetSpeed - InitialSpeed) / TimeToTargetInSeconds
    if AddAnimationToCurrent:
        StaringFrame = FBSystem().LocalTime.GetFrame()
    else:
        StaringFrame = FBSystem().CurrentTake.LocalTimeSpan.GetStart().GetFrame()
    
    Position = FBVector3d(0, 0, 0)
    for I in range(0, TimeToTargetSpeed + 1):
        TimeInSeconds = I / Fps
        Position = Acceleration *0.5 * TimeInSeconds * TimeInSeconds + MovementDir * InitialSpeed * TimeInSeconds
        Position += InitialTranslation
        AnimNode.Nodes[0].FCurve.KeyAdd(FBTime(0 ,0, 0, I + StaringFrame), Position[0])
        AnimNode.Nodes[1].FCurve.KeyAdd(FBTime(0 ,0, 0, I + StaringFrame), Position[1])
        AnimNode.Nodes[2].FCurve.KeyAdd(FBTime(0 ,0, 0, I + StaringFrame), Position[2])
        Player.StepForward()
    
    InitialTranslation = Position
    if AutoConstantForAfterFrames:
        EndFrame = FBSystem().CurrentTake.LocalTimeSpan.GetStop().GetFrame()
        if EndFrame > StaringFrame + TimeToTargetSpeed:
            Start = StaringFrame + TimeToTargetSpeed
            Duration = EndFrame - Start
            for I in range(0, Duration + 1):
                TimeInSeconds = I / Fps
                Position = InitialTranslation + MovementDir * TimeInSeconds * TargetSpeed
                AnimNode.Nodes[0].FCurve.KeyAdd(FBTime(0 ,0, 0, I + Start), Position[0])
                AnimNode.Nodes[1].FCurve.KeyAdd(FBTime(0 ,0, 0, I + Start), Position[1])
                AnimNode.Nodes[2].FCurve.KeyAdd(FBTime(0 ,0, 0, I + Start), Position[2])
                Player.StepForward()

    Player.Goto(FBTime(0 ,0, 0, StaringFrame))

#######################################
def OnAddToCurrentChange(Control, Event):
    global AddAnimationToCurrent
    AddAnimationToCurrent = bool(Control.State)

#######################################
def OnAddToAutoConstant(Control, Event):
    global AutoConstantForAfterFrames
    AutoConstantForAfterFrames = bool(Control.State)


#######################################
def OnTimeToTargetChange(Control, Event):
    Control.Value = int(Control.Value)
    global TimeToTargetSpeed
    TimeToTargetSpeed = int(Control.Value)

####################################### 
def OnInitialSpeedChange(Control, Event):
    global InitialSpeed
    InitialSpeed = Control.Value
    
####################################### 
def OnTargetSpeedChange(Control, Event):
    global TargetSpeed
    TargetSpeed = Control.Value

#######################################
def OnOffsetAngleChange(Control, Event):
    global OffsetAngle
    OffsetAngle = Control.Value
    
#######################################
def OnCalculateSpeed(Control, Event):
    Time = Controls["Edit_DistanceTime"].Value
    Dist = Controls["Edit_Distance"].Value
    Player = FBPlayerControl()
    Player.GotoStart()
    Fps = Player.GetTransportFpsValue()
    Controls["Edit_SpeedResult"].Value = Dist * Fps / Time
    
    
#######################################
def OnDistanceTimeChange(Control, Event):
    Control.Value = int(Control.Value)
    global TimeToTargetSpeed
    TimeToTargetSpeed = Control.Value
    
####################################### 
def OnAxisSelected(Control, Event):
    global AxisToMove
    if(Control.Caption == "R"):
        AxisToMove = 1
    elif(Control.Caption == "U"):
        AxisToMove = 2
    elif(Control.Caption == "F"):
        AxisToMove = 3
    elif(Control.Caption == "L"):
        AxisToMove = -1
    elif(Control.Caption == "D"):
        AxisToMove = -2
    else:
        AxisToMove = -3
        
#######################################
def CreateLabel(LabelId, LabelCaption, Anchor, AttachType, MainLyt):
    # Create label
    L = FBLabel()
    L.Caption = LabelCaption
    X = FBAddRegionParam(10,FBAttachType.kFBAttachLeft, "")
    Y = FBAddRegionParam(10,AttachType, Anchor)
    W = FBAddRegionParam(100,FBAttachType.kFBAttachNone, "")
    H = FBAddRegionParam(25,FBAttachType.kFBAttachNone, "")
    
    MainLyt.AddRegion(LabelId, LabelId, X, Y, W, H)
    MainLyt.SetControl(LabelId, L)
    Controls[LabelId] = L
    

#######################################
def CreateEditNumber(LabelId, LabelCaption, EditId, Anchor, AttachType, MainLyt, AttachLeftScale = 1, AttachBottmScale = 1, EditNumberAttachLeftAdjustment = 0):        
    # Create label
    L = FBLabel()
    L.Caption = LabelCaption
    X = FBAddRegionParam(10 * AttachLeftScale, FBAttachType.kFBAttachLeft, "")
    Y = FBAddRegionParam(10 * AttachBottmScale, AttachType, Anchor)
    W = FBAddRegionParam(150, FBAttachType.kFBAttachNone, "")
    H = FBAddRegionParam(25, FBAttachType.kFBAttachNone, "")
    
    MainLyt.AddRegion(LabelId, LabelId, X, Y, W, H)
    MainLyt.SetControl(LabelId, L)
        
    # Create edit
    E = FBEditNumber()
    Controls[EditId] = E
    
    X = FBAddRegionParam(100 - EditNumberAttachLeftAdjustment, FBAttachType.kFBAttachLeft, LabelId)
    Y = FBAddRegionParam(10 * AttachBottmScale, AttachType, Anchor) 
    W = FBAddRegionParam(80, FBAttachType.kFBAttachNone, "")
    H = FBAddRegionParam(25, FBAttachType.kFBAttachNone, "")

    MainLyt.AddRegion(EditId, EditId, X, Y, W, H)
    MainLyt.SetControl(EditId, E)

#######################################
def CreateButton(ButtonCaption, ButtonId, Anchor, AttachType, MainLyt, AttachLeftScale = 1):
    X = FBAddRegionParam(0 * AttachLeftScale, FBAttachType.kFBAttachLeft, "")
    Y = FBAddRegionParam(0, AttachType, Anchor)
    W = FBAddRegionParam(50, FBAttachType.kFBAttachRight, "")
    H = FBAddRegionParam(25, FBAttachType.kFBAttachNone, "")
    MainLyt.AddRegion(ButtonId, ButtonId, X, Y, W, H)
    Lyt = FBHBoxLayout()
    MainLyt.SetControl(ButtonId, Lyt)
    
    B = FBButton()
    B.Caption = ButtonCaption
    B.Justify = FBTextJustify.kFBTextJustifyLeft
    Lyt.Add(B, 85)
    B.OnClick.Add(OnSimulatePressed)

#######################################
def CreateRadioButton(ButtonCaption, ButtonId, RadioButtonGroup, Anchor, AttachType, MainLyt, AttachLeftScale = 1):
    
    if( AttachLeftScale < 1 ):
        AttachLeftScale  = 1    

    B = FBButton()
    RadioButtonGroup.Add(B)
    
    B.Caption = ButtonCaption
    B.Style = FBButtonStyle.kFBRadioButton
    
    X = FBAddRegionParam(10 * AttachLeftScale,FBAttachType.kFBAttachLeft,"")
    Y = FBAddRegionParam(10, AttachType, Anchor)
    W = FBAddRegionParam(100,FBAttachType.kFBAttachNone,"")
    H = FBAddRegionParam(25,FBAttachType.kFBAttachNone,"")
    
    Controls[ButtonId] = B

    MainLyt.AddRegion(ButtonId, ButtonId, X, Y, W, H)
    MainLyt.SetControl(ButtonId, B)
                
                
#######################################
def CreateCheckBox(ButtonCaption, ButtonId, Anchor, AttachType, MainLyt, AttachLeftScale = 1,onOff=1):
    
    if( AttachLeftScale < 1 ):
        AttachLeftScale  = 1    

    X = FBAddRegionParam(0 * AttachLeftScale, FBAttachType.kFBAttachLeft, "")
    Y = FBAddRegionParam(7, AttachType, Anchor)
    W = FBAddRegionParam(15, FBAttachType.kFBAttachRight, "")
    H = FBAddRegionParam(15, FBAttachType.kFBAttachNone, "")
    MainLyt.AddRegion(ButtonId, ButtonId, X, Y, W, H)
    Lyt = FBHBoxLayout()
    MainLyt.SetControl(ButtonId, Lyt)
    
    B = FBButton()
    B.Style = FBButtonStyle.kFBCheckbox
    B.Caption = ButtonCaption
    B.Justify = FBTextJustify.kFBTextJustifyLeft
    Lyt.Add(B, 150)
    if onOff==1:
        B.State=True
    else:
        B.State=False
         
    Controls[ButtonId] = B


               

#######################################
def PopulateLayout(MainLyt):
    Group = FBButtonGroup()
    Group.AddCallback(OnAxisSelected)
    CreateRadioButton("R", "RadioButton_Axis_X", Group, "", FBAttachType.kFBAttachTop, MainLyt, 0)
    CreateRadioButton("L", "RadioButton_Axis_X_Minus", Group, "", FBAttachType.kFBAttachTop, MainLyt, 4)
    CreateRadioButton("U", "RadioButton_Axis_Y", Group, "", FBAttachType.kFBAttachTop, MainLyt, 7)
    CreateRadioButton("D", "RadioButton_Axis_Y_Minus", Group, "", FBAttachType.kFBAttachTop, MainLyt, 10)
    CreateRadioButton("F", "RadioButton_Axis_Z", Group, "", FBAttachType.kFBAttachTop, MainLyt, 13)
    CreateRadioButton("B", "RadioButton_Axis_Z_Minus", Group, "", FBAttachType.kFBAttachTop, MainLyt, 16)
    
    # CreateLabel("Label_Orientation", "", "RadioButton_Axis_X", FBAttachType.kFBAttachTop, MainLyt)
    CreateEditNumber("Label1", "Init Velocity", "Edit_InitialSpeed", "RadioButton_Axis_X", FBAttachType.kFBAttachBottom, MainLyt)
    CreateEditNumber("Label2", "Target Velocity", "Edit_TargetSpeed", "Label1", FBAttachType.kFBAttachBottom, MainLyt)
    CreateEditNumber("Label3", "Frames to Target", "Edit_TimeToTarget", "Label2", FBAttachType.kFBAttachBottom, MainLyt)
    CreateEditNumber("Label4", "Offset Angle", "Edit_OffsetAngle", "Label3", FBAttachType.kFBAttachBottom, MainLyt)
    
    CreateButton("     G O", "Button_Simulate", "Edit_OffsetAngle", FBAttachType.kFBAttachBottom, MainLyt)
    
    # CreateLabel("Label_Checkbox", "Setting:", "Button_Simulate", FBAttachType.kFBAttachBottom, MainLyt)
    CreateCheckBox("This Frame", "Button_AddToCurrent", "Button_Simulate", FBAttachType.kFBAttachBottom, MainLyt, 0,0)
    CreateCheckBox("Constant", "Button_AutoConstant", "Button_AddToCurrent", FBAttachType.kFBAttachBottom, MainLyt, 0,1)
    


    #CreateCheckBox("Constant", 1, "Button_AddToCurrent", FBAttachType.kFBAttachBottom, MainLyt, 0)

    
    #CreateLabel("Label_SpeedCalculator", "Speed Calculator:", "Button_Simulate", FBAttachType.kFBAttachBottom, MainLyt)
    
    #CreateEditNumber("Label_Distance", "Distance", "Edit_Distance", "Label_SpeedCalculator", FBAttachType.kFBAttachBottom, MainLyt, 1, 1, 30)
    #CreateEditNumber("Label_TimeDistance", "Distance Time", "Edit_DistanceTime", "Label_SpeedCalculator", FBAttachType.kFBAttachBottom, MainLyt, 12, 1, 10)
    #CreateEditNumber("Label_SpeedResult", "Speed", "Edit_SpeedResult", "Label_TimeDistance", FBAttachType.kFBAttachBottom, MainLyt, 1, 1, 30)
            
    
    # Initial Speed
    E = Controls["Edit_InitialSpeed"]
    E.Max = 1000000
    E.Min = -1000000
    E.Value = InitialSpeed
    E.OnChange.Add(OnInitialSpeedChange)
    
    # Target Speed
    E = Controls["Edit_TargetSpeed"]
    E.Max = 1000000
    E.Min = -1000000
    E.Value = TargetSpeed
    E.OnChange.Add(OnTargetSpeedChange)
    
    # Time to Stop
    E = Controls["Edit_TimeToTarget"]
    E.Max = 1000000
    E.Min = 1
    E.Value = TimeToTargetSpeed
    E.OnChange.Add(OnTimeToTargetChange)
    
    # Time to Stop
    E = Controls["Edit_OffsetAngle"]
    E.Max = 360
    E.Min = -360
    E.Value = OffsetAngle
    E.OnChange.Add(OnOffsetAngleChange)
    
    
    # Distance
    #E = Controls["Edit_Distance"]
    #E.Max = 1000000
    #E.Min = 0
    #E.Value = 600
   # E.OnChange.Add(OnCalculateSpeed)
    
    # Distance Time
    #E = Controls["Edit_DistanceTime"]
   # E.Max = 1000000
    #E.Min = 1
    #E.Value = 30
    #E.OnChange.Add(OnCalculateSpeed)
    
    # Speed Result
    #Controls["Edit_SpeedResult"].OnChange.Add(OnCalculateSpeed)
    
    # Add to current
    Controls["Button_AddToCurrent"].OnClick.Add(OnAddToCurrentChange)
    
    # Auto Constant
    Controls["Button_AutoConstant"].OnClick.Add(OnAddToAutoConstant)
    
    # Axis to Move
    Controls["RadioButton_Axis_X"].State = 1
    
    x = FBAddRegionParam(110,FBAttachType.kFBAttachLeft,"")
    y = FBAddRegionParam(170,FBAttachType.kFBAttachTop,"")
    w = FBAddRegionParam(80,FBAttachType.kFBAttachNone,"")
    h = FBAddRegionParam(70,FBAttachType.kFBAttachNone,"")
    MainLyt.AddRegion("main","main", x, y, w, h)
    
    img = FBImageContainer()
    img.Filename = r"C:\Users\wb.zhangying18\Pictures\Snipaste_2023-02-15_15-38-20.png"

   # img.Filename = "Arrow.png"


    MainLyt.SetControl("main",img)
    

    
   

######################
def RotateVectorByOffsetAngle(Angle, Vec):
    Rad = math.radians(Angle)
    Quat = FBVector4d(0, math.sin(Rad * 0.5), 0, math.cos(Rad * 0.5))
    QuatInverse = FBVector4d(0, -Quat[1], 0, Quat[3])
    
    Res = FBVector4d(0, 0, 0, 0)
    FBQMult(Res, Quat, Vec)
    FBQMult(Res, Res, QuatInverse)
    
    return FBVector3d(Res[0], Res[1], Res[2])
    

                
    
    
    
#######################################
def CreateTool():
    # Tool creation will serve as the hub for all other controls
    Tool = FBCreateUniqueTool("RHack Parabolic")
    PopulateLayout(Tool)
    Tool.StartSizeX = 212
    #Tool.FontSize= 5
    Tool.StartSizeY = 278
    Tool.StartPosX = 1430
    Tool.StartPosY =441
    ShowTool(Tool)

#End of function definitions
    
CreateTool()


评论
  目录