1
$\begingroup$

I am trying to set a string attribute per polygon on a mesh. To create the property, I use:

a = m.polygon_layers_string.new(name='object')
x = a.data[0]

but when I try to set the data:

x.value = 'blah'
Traceback (most recent call last):
  File "<blender_console>", line 1, in <module>
TypeError: bpy_struct: item.attr = val: MeshPolygonStringProperty.value expected a bytes type, not str

When I inspect the types:

type(x)
<class 'bpy.types.MeshPolygonStringProperty'>

and

type(x.value)
<class 'bytes'>

But the documentation here mentions the type of x.value should be a string, or am I misreading?

What is the correct way to store a string per polygon?

$\endgroup$
1
$\begingroup$

Use the bmesh API

Suggest using bmesh for polygon string layers.

import bpy
import bmesh   

context = bpy.context
ob = context.object
me = ob.data

bm = bmesh.new()
bm.from_mesh(me)
str_layer = bm.faces.layers.string.new("Foo")

for f in bm.faces:
    f[str_layer] = bytes(f'Face {f.index}', 'utf-8')  # b`foo`
    
bm.to_mesh(me)
bm.clear()

for s in me.polygon_layers_string:
    print(f" Layer {s.name}")
    for p in s.data:
        print(p.value.decode())
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.