Maya: 第五周 作业

lesson13_Maya 中的事件触发

image.png
属性影响练习.gif
import maya.cmds as cmds

def transfer_attr_val(source_obj, target_obj, attr_name):
    
    source_obj_attr_val = cmds.getAttr('{}.{}'.format(source_obj, attr_name))
    
    cmds.setAttr('{}.{}'.format(target_obj, attr_name), source_obj_attr_val)
  
# create ball and box mesh
ball_mesh = cmds.polySphere(n='ball', ch=False)[0]
cmds.setAttr('{}.tx'.format(ball_mesh), -3)

box_mesh = cmds.polyCube(n='box', ch=False)[0]
cmds.setAttr('{}.tx'.format(box_mesh), 3)

cmds.makeIdentity(ball_mesh, box_mesh, a=True, t=True)

attrs_list = ['{}{}'.format(attr, axial) for attr in 'trs' for axial in 'xyz']

jobs_id_list = []

for attr in attrs_list:
    
    job_id = cmds.scriptJob(attributeChange=('{}.{}'.format(ball_mesh, attr), 
                                             'transfer_attr_val("{}","{}","{}")'.format(ball_mesh,box_mesh,attr)))
    jobs_id_list.append(job_id)

# delete job id

cmds.scriptJob(kill= jobs_id_list)

扩展效果:

双向约束?.gif

不存在的!目前不支持K帧!就是走个逻辑玩玩,想玩的拿去玩~

import pymel.core as pm

# setup
source_objs = pm.selected()
mutual_constrain_grp_list = []
for obj in source_objs:
    obj_parent = obj.getParent()
    if not obj_parent or not obj_parent.name().endswith('_mutual_constrain_Grp'):
        mutual_constrain_grp = pm.group(em=True, n='{}_mutual_constrain_Grp'.format(obj.name()))
        pm.delete(pm.parentConstraint(obj, mutual_constrain_grp))
        pm.parent(obj, mutual_constrain_grp)
        if obj_parent:
            pm.parent(mutual_constrain_grp, obj_parent)
    mutual_constrain_grp_list.append(mutual_constrain_grp)
        

def p_constrain(source_objs):
    
    source_objs = [pm.PyNode(obj) for obj in source_objs]
    current_sel_obj = pm.selected()
    
    if not current_sel_obj or current_sel_obj[0] not in source_objs or len(current_sel_obj)>1:
        return False
    
    for item in source_objs:
        if not item.getParent().name().endswith('_mutual_constrain_Grp'):
            return False
    
    pm.delete([item.getParent().inputs(t='parentConstraint') for item in source_objs])

    copy_list = source_objs[::]
    copy_list.remove(current_sel_obj[0])

    for item in copy_list:
        pm.parentConstraint(current_sel_obj[0], item.getParent(), mo=True,weight=1)


job_id = pm.scriptJob(event= ("SelectionChanged", 'p_constrain({})'.format([obj.name() for obj in source_objs])))

# delete job

pm.scriptJob(kill= job_id)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  •   JavaScript 与 HTML 之间的交互是通过事件实现的。   事件,就是文档或浏览器窗口中发生的一些特...
    霜天晓阅读 3,543评论 1 11
  • 一、前言 Swift版本 4.0 Xcode版本 9.2 以前接触到的项目需求中,几乎都是全竖屏展现界面,所以我也...
    软件iOS开发阅读 1,952评论 0 0
  • 人们生活水平提高了,需求自然也就不断加码了,对于旅游的需求更是越来越重视自身的私密性。动不动就要包机了,如今,却又...
    燕娜说阅读 1,506评论 0 0
  • 最近遇到的问题:糖糖宝贝属于2周岁的叛逆期,会打人,会抢别人的东西,自己的东西不肯分享给其他的小朋友,同时动不动就...
    糖妈燕子阅读 173评论 0 0
  • 图文/桃子夭夭之 满山的绿,山上长满了果树,我从没有见过大片大片的果树,这个季节,结满了果实。 我才知道现在已是秋...
    桃子夭夭之阅读 368评论 2 1