简体   繁体   English

我如何用Python在Blender中读出自定义属性?

[英]How do i read out Custom Properties in Blender with Python?

I want to read out Custom Properties of a Blender Object using the scripting mode in Blender itself. 我想在Blender本身中使用脚本模式读出Blender对象的自定义属性。 So far I found only possibilities to read out Custom Properties you created yourself in the scripting mode. 到目前为止,我发现只能读出您在脚本模式下自己创建的自定义属性。 But I want to read out Custom Properties which I tagged myself per hand. 但是我想读出我自己用手标记的自定义属性。 This means I dont have a local variable to use. 这意味着我没有要使用的局部变量。

I want this to be in the following context: I have a Loop going through all objects: 我希望它在以下上下文中:我有一个循环遍历所有对象:

for obj in bpy.data.objects:
if not 'Camera' in obj.name and not 'Lamp' in obj.name and not 'Armature' in obj.name:
    #here I get the location of the current Object
    loc.append(obj.location)

Now what would be perfect, would be something like: 现在什么是完美的,将是这样的:

obj.getCustomProperties

Is there a way to do this with the Blender Python mode? 有没有办法用Blender Python模式做到这一点?

Thanks, Daniel 谢谢,丹尼尔

Let's say we add a custom property called 'testprop' to object 'Cube' - you can access that property within python as bpy.data.objects['Cube']['testprop'] 假设我们在对象'Cube'中添加一个名为'testprop'的自定义属性 - 您可以在python中访问该属性,如bpy.data.objects['Cube']['testprop']

If you don't know the property names you can get a list of available custom properties by calling keys() for the object. 如果您不知道属性名称,则可以通过调用对象的keys()来获取可用自定义属性的列表。

This leads to the following to print the custom properties - 这导致以下内容打印自定义属性 -

bad_obj_types = ['CAMERA','LAMP','ARMATURE']
for obj in bpy.data.objects:
    if obj.type not in bad_obj_types:
        if len(obj.keys()) > 1:
            # First item is _RNA_UI
            print("Object",obj.name,"custom properties:")
            for K in obj.keys():
                if K not in '_RNA_UI':
                    print( K , "-" , obj[K] )

You may also notice I test obj.type instead of obj.name which can be changed by the user and also multiple items may exist with numeric extensions in the name. 您可能还会注意到我测试obj.type而不是obj.name,它可以由用户更改,并且名称中可能还有多个项目和数字扩展名。

Related : 相关

This github has a Python script to read from a csv file and update the Custom Properties for a mesh(es): 这个github有一个Python脚本可以从csv文件中读取并更新网格的自定义属性:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM