3
$\begingroup$

For instance if I have this code from @batFINGER that adds a Custom Property for every Shape Key in a Mesh, but it ends up that both min and max values here are - and + infinity and aren't being set up properly to 0 and 1.

[https://i.imgur.com/cKZ2OH2.png]

Also, after a Custom Property is created on 3.0, what's the correct way of updating values like min, max, step, precision, etc..?

import bpy

context = bpy.context
scene = context.scene
object = context.object
mesh = object.data
shape_keys = mesh.shape_keys

frame = scene.frame_start
scene.frame_set(frame)
# dictionary of shape_key fcurves keyed by data_path
fcurve_dict = {}
for key, keyblock in shape_keys.key_blocks.items():
    if keyblock == shape_keys.key_blocks[0]:
        print("keyblock %s assumed Basis and ignored" % key)
        continue
    # create a custom property for the key
    rna = {}
    rna[key] =  {"name": key,
                "min": keyblock.slider_min,
                "max": keyblock.slider_max,
                "description": "Shape Key %s" % key,
                "soft_min": keyblock.slider_min,
                "soft_max": keyblock.slider_max,
                }
    object[key] = keyblock.value
    object["_RNA_UI"] = rna
    
    # add a driver
    
    fcurve = shape_keys.driver_add('key_blocks["%s"].value' % key)
    driver = fcurve.driver
    driver.type = 'SCRIPTED'
    driver.expression = "shape"
    var = driver.variables.new()
    var.name = "shape"
    var.type = 'SINGLE_PROP'
    target = var.targets[0]
    target.id_type = "OBJECT"
    target.id = object.id_data
    target.data_path = '["%s"]' % key
$\endgroup$
2
  • $\begingroup$ Perhaps the 3.0 Python Release notes might be useful to you? But as far as I know from my own add-ons, there are only additions for custom properties and no changes that break anything, so the existing ways should work. $\endgroup$ Commented Jan 2, 2022 at 16:52
  • 1
    $\begingroup$ After reading and re-reading the Release notes I found my answer (attention deficit is a pain). I'll post this as an answer to my own question :) $\endgroup$
    – elamhut
    Commented Jan 2, 2022 at 22:19

1 Answer 1

3
$\begingroup$

Ok I found an answer in the Release Notes: https://wiki.blender.org/wiki/Reference/Release_Notes/3.0/Python_API

For Blender 3.0 you can change the beginning of the for loop to:

    for key, keyblock in shape_keys.key_blocks.items():
        if keyblock == shape_keys.key_blocks[0]:
            continue

        # create a custom property for the key and update its values
        object[key] = keyblock.value
        id_props = object.id_properties_ui(key)
        id_props.update(min=keyblock.slider_min,
                        max=keyblock.slider_max,
                        description="Shape Key %s" % key,
                        soft_min=keyblock.slider_min,
                        soft_max=keyblock.slider_max,
                        )

I create the custom property inside the object then I get it's ID Properties by calling the new 3.0 function object.id_propertoes_ui('property_name')

Then I just update() its properties with new values.

This is not found in the API currently (not that I found), I had to dig a bit inside the release notes to see how to properly update() the properties.

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .