Skip to content
Zaher Dirkey edited this page May 3, 2024 · 4 revisions

Z Tools

Tips

Access attribute Object Mode

convex = bpy.context.object.data.attributes['convex']
print(convex.data[4].value)
convex.data[4].value = False

Access attribute in Edit Mode

import bpy
import bmesh

obj = bpy.context.object
mesh = bpy.context.object.data
bm = bmesh.from_edit_mesh(mesh)
if "convex2" in bm.faces.layers.int:
    layer = bm.faces.layers.int["convex2"] 
else:
    layer = bm.faces.layers.int.new('convex2')

if layer == None:
    print("No Convex")
else:
    print(bm.faces[6][layer])
    bm.faces[6][layer]=True

Add attribute to Mesh Data

obj = bpy.context.object
obj.data.attributes.add(name="myAttributeFloat", type="BOOLEAN", domain="FACE")
obj.data.attributes.remove(name="myAttributeFloat", type="BOOLEAN", domain="FACE")

Add Custom Attribute to the Data

import bpy

class MyGroup(bpy.types.PropertyGroup):
    myItems = []
    enums = bpy.props.EnumProperty(name="My Items", items=myItems)

bpy.utils.register_class(MyGroup)
bpy.types.Mesh.my_group = bpy.props.PointerProperty(type=MyGroup)

bpy.context.active_object.data.my_group.myItems.append(100)
bpy.context.active_object.data.my_group.myItems.append(101)

print(bpy.context.active_object.data.my_group.myItems)

List faces Attributes

import bpy
import bmesh
bm = bmesh.new()
bm.from_mesh(bpy.context.object.data)
for attr in dir(bm.faces.layers):
    if attr.startswith('__'):
        continue
    print(attr, getattr(bm.faces.layers, attr).keys())

Check attribute if exists (Edit,Object) Modes

print(bpy.context.object.data.attributes.get('z_convex'))
print('z_convex' in bpy.context.object.data.attributes)

Delete attribute

attr = bpy.context.object.data.attributes['z_convex']
if attr != None:
    bpy.context.object.data.attributes.remove(attr)