maya中mel和python的递归

python中的递归

递归是什么意思呢,我的理解就是函数内部调用自己本身。比如阶乘f(n) = n! = 1 x 2 x 3 x 4 x (n-1) x n = (n-1)! x n = f(n-1) x n,像f(7)=7xf(6)=7x6xf(5)=7x6x5xf(4)=7x6x5x4xf(3)=7x6x5x4x3xf(2)=7x6x5x4x3x2xf(1),如下图
在这里插入图片描述

#打印7的阶乘
def fn(num):
    if num==1:
        return 1
    return (num*fn(num-1))
fn(7)
#从10开始一次递减打印数字,到1结束
def print_num(num):
    print num
    if num==1:
        return
    print_num(num-1)A
print_num(10)

maya中python的递归

我们可以用它找给定物体的子物体,思路就是判断给定的“父物体”层级下面有没有“子物体”,有的话把“子物体”当做“父物体”调用自身函数继续找,直到没有“子物体”为止。值得注意的是,创建骨骼的话如果不取消选择,骨骼会成为选定物体的子物体。
在这里插入图片描述

import pymel.core as pm
import maya.cmds as pc
#创建一条骨骼链
for i in range(10):
    pm.joint().setTranslation((5,0,0))
#创建十个球,并依次成为父子关系
a=[]
for i in range(10):
    a.append(pm.polySphere(ch=0)[0])
    a[i-1].translateX.set(5)
    if(len(a)>1):
        a[i].setParent(a[i-1])
#递归求所有的子物体     
def getChild(sel,child):
    for item in sel:
        child.append(item)
        Children=pm.listRelatives(item,c=1)
        if (len(Children)>0):
            getChild(Children,child)
sel=pm.ls(sl=1)
b=[]
getChild(sel,b)

mel中的递归

mel我一直以为没有递归,又是自己的无知,和python思路差不多。
在这里插入图片描述

global proc getChild(string $parent[],string $child[])
{
    for ($item in $parent)
    {
        $child[size($child)] = $item;
        string $Children[] = `listRelatives  -c $item`;
        if (size($Children)>0)
        {
            getChild($Children,$child);
        }
    }
}
string $child[];
clear $child;
string $parent[]=`ls -sl`;
getChild($parent,$child);
print $child;
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值