maya嵌入python_Maya Python-将zip文件嵌入到maya文件中吗?

博客围绕在Maya文件中嵌入工具展开,探讨将Python包打包成Zip文件并嵌入Maya的方法。在操作时遇到将Zip文件以二进制形式嵌入的问题,还涉及编码、存储及解码写回文件的难题,最终给出使用base64模块解决的方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

This was a suggestion from another stack thread that I'm finally getting back to. It was part of a discussion about how to embed tools into a maya file.

You can write the whole thing as a python package, zip it, then stuff the binary contents of the zip file into a fileInfo. When you need to code, look for it in the user's $MAYA_APP_DIR; if there's no zip, write the contents of the fileInfo to disk as a zip and then insert the zip into sys.path

So far the programming is going okay, but I think I hit a snag. When I attempt this:

with open("..directory/myZip.zip","rb") as file:

cmds.fileInfo("myZip", file.read())

..and then I..

print cmds.fileInfo("myZip",q=1)

I get

[u'PK\003\004\024']

which is a bad translation of the first line of gibberish when reading the zip file as a text document.

How can I embed my zip file into my maya file as binary?

====================

Update:

Maya doesn't like writing to the file as a direct read of the utf-8 encoded zip file. I found various methods of making it into an acceptable string that could be written, but the decoding back to the file didn't appear to work. I see now that Theodox's suggestion was to write it to binary and put that in the fileInfo node.

How can one encode, store and then decode to write to file later?

If I were to convert to binary using for instance:

' '.join(format(ord(x), 'b') for x in line)

What code would I need to turn that back into the original utf-8 zip information?

解决方案

you can find related code here:

the relevant bit is

import base64

encoded = base64.b64encode(value)

decoded = base64.b64decode(encoded)

basically it's the same idea, except using the base64 module instead of binascii. Any method of converting an arbitary character stream to an ascii-safe representation will work fine, as long as you use a reversable method: the potential problem you need to watch out for is a character in the data block that looks to maya like a close quote - an open quote int he fileInfo will be messy in an MA file.

This example uses YAML to do arbitrary key-value pairs but that part is irrelevant to storing the binary stuff. I've used this technique for fairly large data (up to 640k if i recall) but I don't know if it has an upper limit in terms of what you can stash in Maya

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值