# Copyright 2009 Autodesk, Inc.  All rights reserved.
# Use of this software is subject to the terms of the Autodesk license agreement 
# provided at the time of installation or download, or which otherwise accompanies
# this software in either electronic or hard copy form.
#
# Script description:
# Assuming a RigidBody properties already exists in a scene, assign it to all selected models.
#
#
# Topic: FBMessageBox, FBGetSelectedModel, FBModel.ConnectSrc
#

from pyfbsdk import *

def IsConnectedTo(to, toConnect):
    for i in range(to.GetSrcCount()):
        if to.GetSrc(i) == toConnect:
            return True
    return False


def AssignRigidBodyToSelectedModels(rigidBody):
    # Get the selected models.    
    lModelList = FBModelList()
    FBGetSelectedModels( lModelList )

    if len( lModelList ) == 0:
        FBMessageBox( "Message", "Nothing selected", "OK", None, None )
    else:
        modifiedmodels = []
        for model in lModelList:
            # Ensure that we can assign the same rigidbody only ONCE on a model
            if not IsConnectedTo(model, rigidBody):
                model.ConnectSrc(rigidBody)
                modifiedmodels.append(model)

        lMessage = "Models modified:"
        lMessage += ''.join( map( lambda pModel: "\n" + pModel.Name, modifiedmodels ))
        FBMessageBox( "Message", lMessage, "OK", None, None )


# check the scene to see if we have a RigidBody
for p in FBSystem().Scene.PhysicalProperties:
    if p.ClassName() == "KxL_RigidBodyProperty":
        # if so: assign this rigid body to all selected models
        AssignRigidBodyToSelectedModels(p)
        break