add_text_animation
Add entrance, exit, or looping animations to text segments in video editing projects. Choose animation types like 'retro typewriter' or 'spring' and optionally set duration.
Instructions
为文本片段添加动画效果
Args: text_segment_id: 文本片段ID,通过add_text_segment获得 animation_type: 动画类型,支持以下类型: - "TextIntro": 入场动画 - "TextOutro": 出场动画 - "TextLoopAnim": 循环动画 animation_name: 动画名称,如 "复古打字机", "弹簧", "色差故障", "淡入", "淡出" 等,可以使用find_effects_by_type工具,资源类型选择TextIntro、TextOutro、TextLoopAnim,从而获取动画类型有哪些 duration: 动画持续时间(可选),格式如 "1s", "500ms"
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| text_segment_id | Yes | ||
| animation_type | Yes | ||
| animation_name | Yes | ||
| duration | No |
Implementation Reference
- jianyingdraft/tool/text_tool.py:138-231 (handler)MCP tool handler for add_text_animation. Validates parameters, checks animation availability using JianYingResourceManager, resolves draft and track info using index_manager, and delegates to add_text_animation_service.@mcp.tool() def add_text_animation( text_segment_id: str, animation_type: str, animation_name: str, duration: Optional[str] = None ) -> ToolResponse: """ 为文本片段添加动画效果 Args: text_segment_id: 文本片段ID,通过add_text_segment获得 animation_type: 动画类型,支持以下类型: - "TextIntro": 入场动画 - "TextOutro": 出场动画 - "TextLoopAnim": 循环动画 animation_name: 动画名称,如 "复古打字机", "弹簧", "色差故障", "淡入", "淡出" 等,可以使用find_effects_by_type工具,资源类型选择TextIntro、TextOutro、TextLoopAnim,从而获取动画类型有哪些 duration: 动画持续时间(可选),格式如 "1s", "500ms" """ # 参数验证 valid_types = ["TextIntro", "TextOutro", "TextLoopAnim"] if animation_type not in valid_types: return ToolResponse( success=False, message=f"无效的动画类型 '{animation_type}',支持的类型: {', '.join(valid_types)}" ) # 动画存在性验证(文本动画使用 title 字段) from jianyingdraft.utils.effect_manager import JianYingResourceManager manager = JianYingResourceManager() effects = manager.find_by_type( effect_type=animation_type, keyword=animation_name, limit=1 ) # 检查是否找到完全匹配的动画 exact_match = False if effects: for effect in effects: if effect.get('title') == animation_name: exact_match = True break if not effects or not exact_match: # 获取建议动画 animation_suggestions = manager.find_by_type(animation_type, keyword=animation_name) all_suggestions = [] for effect in animation_suggestions: if effect.get('title'): all_suggestions.append(effect.get('title')) return ToolResponse( success=False, message=f"在 {animation_type} 中未找到动画 '{animation_name}',请确认动画名称是否正确,或使用建议动画", data={ "error_type": "animation_not_found", "animation_type": animation_type, "animation_name": animation_name, "suggestions": all_suggestions } ) # 通过text_segment_id获取相关信息 draft_id = index_manager.get_draft_id_by_text_segment_id(text_segment_id) track_info = index_manager.get_track_info_by_text_segment_id(text_segment_id) if not draft_id: return ToolResponse( success=False, message=f"未找到文本片段ID对应的草稿: {text_segment_id}" ) if not track_info: return ToolResponse( success=False, message=f"未找到文本片段ID对应的轨道信息: {text_segment_id}" ) track_name = track_info.get("track_name") # 调用服务层处理业务逻辑 result = add_text_animation_service( draft_id=draft_id, text_segment_id=text_segment_id, animation_type=animation_type, animation_name=animation_name, duration=duration, track_name=track_name ) return result
- jianyingdraft/server.py:29-29 (registration)The text_tools function is called with the MCP instance to register the add_text_animation tool among others.text_tools(mcp)
- Service layer helper that instantiates TextSegment and calls its add_animation method to perform the operation, handling errors and formatting response.def add_text_animation_service( draft_id: str, text_segment_id: str, animation_type: str, animation_name: str, duration: Optional[str] = None, track_name: Optional[str] = None ) -> ToolResponse: """ 文本动画添加服务 - 为文本片段添加动画效果 Args: draft_id: 草稿ID text_segment_id: 文本片段ID animation_type: 动画类型,"TextIntro"、"TextOutro"、"TextLoopAnim" animation_name: 动画名称,如 "复古打字机"、"弹簧"、"色差故障" 等 duration: 动画持续时间(可选),格式如 "1s"、"500ms" track_name: 轨道名称(可选) Returns: ToolResponse: 包含操作结果的响应对象 """ try: # 创建TextSegment实例,传入text_segment_id text_segment = TextSegment(draft_id, text_segment_id=text_segment_id, track_name=track_name) # 调用文本动画添加方法 result_data = text_segment.add_animation( animation_type=animation_type, animation_name=animation_name, duration=duration ) # 构建返回数据 response_data = { "text_segment_id": text_segment_id, "draft_id": draft_id, "animation_type": animation_type, "animation_name": animation_name, "add_animation": result_data } # 添加可选参数到返回数据 if duration: response_data["duration"] = duration if track_name: response_data["track_name"] = track_name return ToolResponse( success=True, message=f"文本动画添加成功: {animation_type}.{animation_name}", data=response_data ) except ValueError as e: # 处理参数错误 return ToolResponse( success=False, message=f"参数错误: {str(e)}" ) except NameError as e: # 处理轨道不存在错误 return ToolResponse( success=False, message=f"轨道错误: {str(e)}" ) except Exception as e: # 处理其他未预期的错误 return ToolResponse( success=False, message=f"文本动画添加失败: {str(e)}" )
- Core TextSegment class method that constructs the animation operation JSON and appends it to the draft's text.json file.def add_animation(self, animation_type: str, animation_name: str, duration: Optional[str] = None,text_segment_id:Optional[str]=None) -> bool: """ 添加文本动画 Args: animation_type: 动画类型,"TextIntro"、"TextOutro"、"TextLoopAnim" animation_name: 动画名称,如 "复古打字机"、"弹簧"、"色差故障" 等 duration: 动画持续时间(可选),格式如 "1s"、"500ms" Returns: bool: 添加是否成功 """ if self.text_segment_id is None and text_segment_id is None: print("错误: text_segment_id不能为空") return False text_segment_id = text_segment_id or self.text_segment_id # 验证动画类型 valid_types = ["TextIntro", "TextOutro", "TextLoopAnim"] if animation_type not in valid_types: print(f"错误: 无效的动画类型 '{animation_type}',支持的类型: {valid_types}") return False # 构建add_animation参数 add_animation_params = { "animation_type": animation_type, "animation_name": animation_name } # 只添加用户明确传入的可选参数 if duration is not None: add_animation_params["duration"] = duration # 构建完整的动画数据 animation_data = { "text_segment_id": text_segment_id, "operation": "add_animation", "add_animation": add_animation_params } # 保存参数 self.add_json_to_file(animation_data) return True