如何存储然后检索父子依赖项数据(Maya MEL/Python脚本) [英] How to store and then retreive parent-child dependency data (Maya MEL/Python script)

查看:132
本文介绍了如何存储然后检索父子依赖项数据(Maya MEL/Python脚本)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个层次结构:

  1. 分开
  2. doSomething()
  3. 将其重新放回原处.

我知道如何确定要打破的事情,并计划好在层次结构统一时我需要做的事情.问题是我该如何将其作为父母?

I know how to break things for sure, and have plan about what I need to do when they are flat in hierarchy. The problem is how do I parent them back?

详细信息

这与我之前的问题有关:

This is related to my previous question:

对带有动画子对象(MAYA MEL/Python脚本)的父对象进行冻结比例转换

我需要冻结层次结构中所有节点上的比例转换.问题在于节点上具有转换的动画,如果我尝试冻结父节点上的缩放比例,则子动画会变得很奇怪. 但是,似乎有一个解决方案,但是我需要在实施方面提供帮助. 我要:

I need to freeze scale transforms on all nodes in a hierarchy. Problem is that nodes have translate animations on them, and if I try to freeze scale on a parent node, it's child animation gets weird. However it looks like there is a solution, but I need help with implementation of it. I want to:

  1. 将每个对象分组
  2. 将每个小组都与世界亲密
  3. 将动画烘焙到世界空间(摆脱组节点)
  4. 冻结缩放变换
  5. 重新把他们当父母

当我手动执行此操作时,它可以正常工作,因此一切正常.但是,如果您认为有更好的方法可以实现我的主要目标,请告诉我.

It works when I do it manually, so everything should be ok. But if you think there is better way to achieve my main goal, please do tell me.

因此,我一直在考虑将所有节点写入字符串并从那里调用它们,但是我不知道如何处理分支. 例如: 如果这是一个无分支的层次结构(一直有一个父母和一个孩子),那么我可以两个人和一个父母来轻松地称它们. 但是分支使字符串中的名称变得像父母,孩子,孩子,孩子,父母..."

So, I've been thinking to write all nodes to a string and call them from there, but I don't know how to handle branches. For example: If it was a branchless hierarchy (one parent and one child all along) I could call them by two, and parent easily. But branches make names in string go like "parent, child, child, child, parent..."

可能我想得太多,方向错误.有什么想法吗?

Probably I'm thinking too much and in wrong direction. Any ideas?

推荐答案

如果您还没有的话,我建议您使用python-您可以相互使用几乎所有数据类型,以在单个数据结构中获得所需的数据.这将比创建长字符串并稍后再将其重新备份容易得多.

I would suggest using python if you aren't already - you can use pretty much any data types with each other to get what you need in a single data structure. It will make it much easier than creating a long string and breaking it back up again later.

收集您的层次结构数据:

to collect your hierarchy data:

import maya.cmds as mc

def hierarchyTree(parent, tree):
    children = mc.listRelatives(parent, c=True, type='transform')
    if children:
        tree[parent] = (children, {})
        for child in children:
            hierarchyTree(child, tree[parent][1])

top_node = 'name_of_node'  # could also use mc.ls(sl=True)[0] if you want...
hierarchy_tree = {}
hierarchyTree(top_node, hierarchy_tree)

这基本上应该从您的顶部节点开始,然后递归地向下层次结构创建一个类似于嵌套字典的数据结构...每个键都是父节点,其键值是一个存储子项列表的元组,儿童数据字典.每个子dict遵循相同的格式-子是键,其子数据在元组等中,等等,等等,直到层次结构的末尾.我正在使用具有列表和字典的元组,因为字典是无序的...列表基本上可以确保您以它们来自的顺序重新父它们,但是如果您不真正使用字典,则可以存储该字典关心保留订单...

this should basically start from your top node and recursively go down the hierarchy to create a data structure that's almost like nested dicts... each key is a parent node with it's key value being a tuple that stores a list of children and a dict of children data. Each child dict follows the same format - the child is a key with it's child data in a tuple, etc, etc, etc to the end of the hierarchy. I'm using the tuple with a list and a dict since dicts are unordered... the list will basically ensure you re-parent them in the same order they came from, but you could just store the dict if you don't really care about retaining the order...

要将其全部归为父类,您将执行以下操作:

To parent it all back, you'll do something like this:

def reparent(tree):
    for parent, data in tree.iteritems():
        children, child_tree = data
        mc.parent(children, parent)
        reparent(child_tree)

reparent(hierarchy_tree)

现在...我还没有测试过此代码-无需将其带入Maya即可即时编写.我更担心re-parent函数中会弹出错误,因此您可能需要在其中尝试/例外,但希望它会跳过任何空的dict项,并使您接近需要输入的内容.育儿/重新育儿.这还假设您的所有节点都具有唯一的短名称...

now... I haven't tested this code - kind of wrote it on the fly without bringing it into Maya. I'm more concerned about errors popping up in the re-parent function, so you may need a try/except in there, but hopefully it'll just skip any empty dict items and get you close to what you need for the in-parenting/re-parenting. This is also assuming all of your nodes have unique short names...

哦,关于递归函数的说明...请确保它们会在某个时刻终止,否则您可能会陷入无限循环中(这很好,因为我们正在跟踪具有确定结尾的层次结构-即:不再有子节点

Oh, a note about recursive functions... make sure they'll terminate at some point or else you can get stuck in an infinite loop (this should be fine since we're tracing a hierarchy that has a definite end - ie: no more child nodes)

这篇关于如何存储然后检索父子依赖项数据(Maya MEL/Python脚本)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆