Created
March 8, 2014 15:13
-
-
Save fereria/9432026 to your computer and use it in GitHub Desktop.
Export Import UserDefinedAttribute
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os.path | |
import json | |
#自分用File読み書きモジュール | |
import fileIO | |
import pymel.core as pm | |
import maya.mel as mm | |
#選択したノードのAddTtterされたアトリビュートをExport | |
def exportUserDefinedAttribute(exportPath): | |
selectNode = pm.ls(sl=True) | |
jsonSetVal = {} | |
for node in selectNode: | |
jsonAttrVal = [] | |
userAddAttr = node.listAttr(ud=True) | |
check = [] | |
for attr in userAddAttr: | |
infoDic = {} | |
if attr not in check: | |
info = getAttrDic(attr) | |
jsonAttrVal.append(info) | |
check.append(attr) | |
if attr.type() == "double3": | |
child = attr.children() | |
for c in child: | |
cVal = getAttrDic(c) | |
cVal["p"] = attr.name() | |
check.append(c) | |
jsonAttrVal.append(cVal) | |
jsonSetVal[node.name()] = jsonAttrVal | |
fileIO.fileIO(exportPath).writeLines([json.dumps(jsonSetVal)]) | |
#importPathで指定したアトリビュートのJSONファイルを読み込む | |
def importUserDefinedAttribute(importPath): | |
jsonfile = open(importPath,"r") | |
attrVal = json.load(jsonfile) | |
jsonfile.close() | |
print attrVal | |
for node in attrVal.keys(): | |
if pm.objExists(node) == True: | |
nObj = pm.PyNode(node) | |
for attr in attrVal[node]: | |
cmds = "addAttr " | |
cmds += "-" + attr[u"attrflag"] + " " + attr["type"] | |
if attr["type"] == "enum": | |
cmds += " -en " + attr["enum"] + " " | |
if attr.has_key("min") == True: | |
cmds += " -min " + str(attr["min"]) | |
if attr.has_key("max") == True: | |
cmds += " -max " + str(attr["min"]) | |
if attr.has_key("dv") == True: | |
cmds += " -dv " + str(attr["dv"]) | |
cmds += " -k " + str(attr["k"]) | |
cmds += " -ln \"" + str(attr["ln"]) + "\"" | |
cmds += " -sn \"" + str(attr["sn"]) + "\"" | |
cmds += " " + node + ";" | |
if nObj.hasAttr(attr["ln"]) != True: | |
mm.eval(cmds) | |
#AttributeInfoをDic型にして返す | |
def getAttrDic(attr): | |
infoDic = {} | |
infoDic["ln"] = attr.attrName(longName=True) | |
infoDic["sn"] = attr.attrName() | |
infoDic["type"] = attr.type() | |
if attr.isKeyable() == True: | |
infoDic["k"] = 1 | |
else: | |
infoDic["k"] = 0 | |
if attr.type() == "string": | |
infoDic["attrflag"] = "dt" | |
else: | |
infoDic["attrflag"] = "at" | |
# MEMO | |
#-Numelic系のアトリビュート情報を出力する。 | |
if attr.type() != "double3": | |
if attr.getMin() != None: | |
infoDic["min"] = attr.getMin() | |
if attr.getMax() != None: | |
infoDic["max"] = attr.getMax() | |
if attr.type() == "enum": | |
infoDic["enum"] = ":".join(attr.getEnums().keys()) | |
#default値を取得 | |
if attr.type() != "double3": | |
defaultVal = pm.attributeQuery(attr.name().split(".")[1],n=attr.node().name(),listDefault=True) | |
infoDic["dv"] = defaultVal[0] | |
return infoDic |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment