C4D的python api允许查看和修改对象上的用户自定义数据。python API并没有完全实现c++ API中的Description和DynamicDescription 对应的类。因此用户数据现在就是创建动态描述的唯一途径。
浏览数据
使用BaseList2D.GetUserDataContainer()查看用户数据
for id, bc in op.GetUserDataContainer():
print id, bc
BaseContainer和描述BaseContainer是一一对应的。这个版本的函数遍历c4d的符号来展示每一帧对应的常量。
for descId, bc in op.GetUserDataContainer():
print "*"*5,descId,"*"*5
for key, val in bc:
for txt, i in c4d.__dict__.iteritems():
if key ==i and txt[:5]=="DESC_":
print txt,"=",val
break
else:
print key,"=",val
创建用户数据
使用BaseList2D.AddUserData()来添加用户数据。下面的代码就显示了如何创建用户数据:
import c4d
from c4d import gui
#Welcome to the world of Python
def CreateUserDataGroup(obj, name, parentGroup=None, columns=None, shortname=None):
if obj is None: return False
if shortname is None: shortname =name
bc =c4d.GetCustomDatatypeDefault(c4d.DTYPE_GROUP)
bc[c4d.DESC_NAME] = name
bc[c4d.DESC_SHORT_NAME] = shortname
bc[c4d.DESC_TITLEBAR] = 1
if parentGroup is not None:
bc[c4d.DESC_PARENTGROUP] = parentGroup
if columns is not None:
#DESC_COLUMNS VALUE IS WRONG IN 15.057 - SHOULD BE 22
bc[22] =columns
return obj.AddUserData(bc)
def CreateUserDataLink(obj, name, link, parentGroup=None, shortname=None):
if obj is None: return False
if shortname is None: shortname =name
bc =c4d.GetCustomDatatypeDefault(c4d.DTYPE_BASELISTLINK)
bc[c4d.DESC_NAME] =name
bc[c4d.DESC_SHORT_NAME] =shortname
bc[c4d.DESC_DEFAULT] =link
bc[c4d.DESC_ANIMATE] =c4d.DESC_ANIMATE_OFF
bc[c4d.DESC_SHADERLINKFLAG] =True
if parentGroup is not None:
bc[c4d.DESC_PARENTGROUP] =parentGroup
element =obj.AddUserData(bc)
obj[element] =link
return element
def CreateUserDataFloat(obj, name, val=0, parentGroup=None, unit=c4d.DESC_UNIT_REAL):
if obj is None: return False
bc =c4d.GetCustomDatatypeDefault(c4d.DTYPE_REAL)
bc[c4d.DESC_NAME] =name
bc[c4d.DESC_SHORT_NAME] =name
bc[c4d.DESC_DEFAULT] =val
bc[c4d.DESC_ANIMATE] =c4d.DESC_ANIMATE_ON
bc[c4d.DESC_UNIT] =unit
bc[c4d.DESC_CUSTOMGUI] =c4d.CUSTOMGUI_REALSLIDER
bc[c4d.DESC_MINSLIDER] =-500
bc[c4d.DESC_MAXSLIDER] =500
bc[c4d.DESC_STEP] =1
if parentGroup is not None:
bc[c4d.DESC_PARENTGROUP] =parentGroup
element =obj.AddUserData(bc)
obj[element] =val
return element
def CreateUserDataInEx(obj, name, parentGroup=None):
if obj is None: return False
bc =c4d.GetCustomDatatypeDefault(c4d.CUSTOMDATATYPE_INEXCLUDE_LIST)
bc[c4d.DESC_NAME] =name
bc[c4d.DESC_SHORT_NAME] =name
bc[c4d.DESC_CUSTOMGUI] =c4d.CUSTOMGUI_INEXCLUDE_LIST
bc[c4d.IN_EXCLUDE_FLAG_NUM_FLAGS] =1
bc[c4d.IN_EXCLUDE_FLAG_INIT_STATE] =1
bc[c4d.IN_EXCLUDE_FLAG_IMAGE_01_ON] =1018640
bc[c4d.IN_EXCLUDE_FLAG_IMAGE_01_OFF] =1018641
bc[c4d.DESC_HIDE] =not bool(obj[c4d.ART_USEMOGRAPH])
if parentGroup is not None:
bc[c4d.DESC_PARENTGROUP] = parentGroup
element =obj.AddUserData(bc)
return element
想创建一个顶层的并在属性管理器中显示的组,就调用 CreateUserDataGroup,其中用parentGroup=c4d.DescID(0)
;或者使用bc[c4d.DESC_PARENTGROUP]=c4d.DescID(0)
self.layerGroup = self.CreateUserDataGroup(node,"Layers",c4d.DescID(0))
全部代码:
def main():
layerGroup =CreateUserDataGroup(op,"Layers",c4d.DescID(0))
subGroup =CreateUserDataGroup(op,"Sub Group",layerGroup,3)
float1 =CreateUserDataFloat(op,"Float 1",0,subGroup)
float2 =CreateUserDataFloat(op,"Float 2",0,subGroup)
float3 =CreateUserDataFloat(op,"Float 3",0,subGroup)
float4 =CreateUserDataFloat(op,"Float 4",0,subGroup,c4d.DESC_UNIT_PERCENT)
float5 =CreateUserDataFloat(op,"Float 5",0,subGroup,c4d.DESC_UNIT_PERCENT)
float6 =CreateUserDataFloat(op,"Float 6",0,subGroup,c4d.DESC_UNIT_PERCENT)
subGroup2 =CreateUserDataGroup(op,"Sub Group 2",layerGroup,1)
link1 =CreateUserDataLink(op,"Link",None,subGroup2)
c4d.EventAdd()