maya 选择当前节点的所有子节点,设置半径,获取动画时长,并且设置时长
python 脚本
- import maya.cmds as cmds
-
- # 获取当前选择的节点
- selected_nodes = cmds.ls(selection=True)
-
- # 创建一个列表来存储所需的节点:当前选中的节点及其所有后代
- nodes_to_select = list(selected_nodes) # 确保当前选择也被包含
-
- # 遍历每个已选择的节点并获取其所有后代节点
- for node in selected_nodes:
- # listRelatives命令用来获取节点的所有后代,allDescendents=True开启递归查询
- node_descendants = cmds.listRelatives(node, allDescendents=True) or []
- nodes_to_select.extend(node_descendants)
-
- # 过滤出有半径属性的节点
- nodes_with_radius = [node for node in nodes_to_select if cmds.attributeQuery('radius', node=node, exists=True)]
-
- # 设置所有有半径属性的节点的半径为0.01
- for node in nodes_with_radius:
- cmds.setAttr(node + ".radius", 0.01)
-
- # 可选:重新选择这些节点以便在界面上看到变化
- cmds.select(nodes_with_radius)
-
- def get_animation_length():
- # 获取场景中所有的动画曲线节点
- anim_curves = cmds.ls(type='animCurve')
-
- # 初始化最小和最大帧变量
- min_frame = float('inf')
- max_frame = float('-inf')
-
- # 遍历所有动画曲线
- for curve in anim_curves:
- # 获取每条曲线的关键帧时间
- keyframes = cmds.keyframe(curve, query=True)
-
- # 更新最小和最大帧数
- if keyframes:
- min_frame = min(min_frame, min(keyframes))
- max_frame = max(max_frame, max(keyframes))
-
- # 检查是否找到有效的帧数
- if min_frame == float('inf') or max_frame == float('-inf'):
- return 0
- else:
- return max_frame
-
- # 调用函数并打印结果
- end_frame=get_animation_length()
-
- print("end_frame",end_frame)
-
- start_frame = 1
-
-
- # 设置动画的实际播放范围
- cmds.playbackOptions(min=start_frame, max=end_frame)
-
- # 设置动画编辑器的时间范围(如果需要的话)
- cmds.playbackOptions(animationStartTime=start_frame, animationEndTime=end_frame)
-
- # 设置时间轴的显示范围
- cmds.playbackOptions(minTime=start_frame, maxTime=end_frame)