Last active
April 17, 2018 15:26
-
-
Save coxevan/ff9a814d146f663c4ced3e3f6452a6b0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import maya.cmds | |
PARTICLE_DEFAULT_NAME = 'spring_particle' | |
def setup_spring(obj, time_range = None, spring_weight = 0.5): | |
''' | |
Sets up spring physics system around a given object | |
:param obj: obj to apply physics system to | |
:param time_range: time range of animation to process, tuple | |
:param spring_weight: weight of spring anim to apply, default 0.5 | |
:return: True/False | |
''' | |
if not obj or maya.cmds.objExists(PARTICLE_DEFAULT_NAME): | |
return False | |
start_frame, end_frame = time_range or [ maya.cmds.playbackOptions(minTime=True, query=True), maya.cmds.playbackOptions(maxTime=True, query=True)] | |
maya.cmds.currentTime( start_frame, edit = True ) | |
locator = maya.cmds.spaceLocator(name='original_animation_locator') | |
maya.cmds.particle(p=[(0,0,0)], name=PARTICLE_DEFAULT_NAME) | |
# Bake the animation from the selected object to the locator | |
locator_constraint = maya.cmds.parentConstraint(obj, locator, mo=False) | |
maya.cmds.bakeResults(locator, t=(start_frame, end_frame)) | |
maya.cmds.delete(locator_constraint) | |
# Align the particle with the locator | |
#TODO Don't be lazy, do this without a constraint | |
align_constraint = maya.cmds.parentConstraint(locator, PARTICLE_DEFAULT_NAME,mo=False) | |
maya.cmds.delete(align_constraint) | |
# Create a goal for the particle | |
maya.cmds.goal(PARTICLE_DEFAULT_NAME, g=locator, w=spring_weight) | |
maya.cmds.spaceLocator(name='physics_locator') | |
maya.cmds.connectAttr('{0}.worldCentroid'.format(PARTICLE_DEFAULT_NAME), '{0}.translate'.format('physics_locator')) | |
point_constraint = maya.cmds.pointConstraint('physics_locator', obj, mo=True, n='physics_point_const') | |
return True | |
def bake_spring(obj, time_range = None): | |
''' | |
bake an object set up with physics sim. Assumes object passed is the one setup. | |
:param obj: obj to bake sim to | |
:param time_range: time range to bake on | |
:return: True/False | |
''' | |
if not obj or not maya.cmds.objExists(PARTICLE_DEFAULT_NAME): | |
return False | |
start_frame, end_frame = time_range or [ maya.cmds.playbackOptions(minTime=True, query=True), maya.cmds.playbackOptions(maxTime=True, query=True)] | |
maya.cmds.bakeResults('physics_locator', t=(start_frame, end_frame), simulation=True) | |
maya.cmds.delete(['original_animation_locator', PARTICLE_DEFAULT_NAME]) | |
maya.cmds.bakeResults(obj, t=(start_frame,end_frame)) | |
maya.cmds.delete(['physics_locator', 'physics_point_const']) | |
return True | |
if __name__ != '__main__': | |
obj_list = maya.cmds.ls(sl=True) | |
if obj_list: | |
obj = obj_list[0] | |
setup_spring(obj, spring_weight=0.5) # Set spring value here | |
bake_spring(obj) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Want to run straight in the script editor in maya? Change line 60:
if __name__ != '__main__':
to
if __name__ == '__main__':