2
$\begingroup$

I try to add custom enum properties to a bone in order to change sub-surf levels and masking vertex groups of a mesh. Every time I run the script, it assigns the property like the following:

enter image description here
Enum property state after running the script

When I close the blender, restart it and open up the file, the properties appear in integer values like:

 Why does enum changes to integer?

Note: I've figured out that this does not happen, if I reopen the file without restarting blender.

Why does this happen?


.blend files (including the script in EnumPropCheck.blend)

$\endgroup$
  • $\begingroup$ Store the properties per scene. This should solve your issue: blender.stackexchange.com/questions/7438/… $\endgroup$ – p2or Aug 5 '15 at 10:53
  • $\begingroup$ @poor thanks for the help. But i could not understand it !! $\endgroup$ – ashwin Aug 5 '15 at 11:10
  • $\begingroup$ Create an addon and put your property definition in register(), and delete them in unregister(). Install the addon, enable it and save user preferences, so that it loads automatically every time. Or use the Register checkbox in Text editor instead of using an addon if the script is specific to that .blend file. $\endgroup$ – CoDEmanX Aug 5 '15 at 21:34
  • $\begingroup$ @ CodeEmax i have clicked register toggle in text editor. And the problem is not if get resset to default but its supposed to be enum but becoming intiger. $\endgroup$ – ashwin Aug 6 '15 at 11:41
  • $\begingroup$ @poor i clarified the question please check $\endgroup$ – ashwin Aug 6 '15 at 14:01
1
$\begingroup$

If only a single .blend uses your property, tick the Register checkbox for the script in Text Editor. The text datablock name must end with .py!

The script will be run when the .blend is loaded. Note that auto-run is disabled by default. Click Reload Trusted in the Info header and confirm (Revert).

import bpy

def my_enum_cb(self, context):
    print(self, self.my_enum, context)

bpy.types.PoseBone.my_enum = bpy.props.EnumProperty(
    items=(
        ('identi_1', 'Name 1', 'Descript 1'),
        ('identi_2', 'Name 2', 'Descript 2'),
        ('identi_3', 'Name 3', 'Descript 3'),
    ),
    name="My Enum",
    default="identi_2",
    update=my_enum_cb
)

bpy.data.objects["Armature"].pose.bones["Bone"].my_enum = "identi_3"

If you need this property to be globally available, turn the code into an addon, install and enable it. Don't forget to save user prefs after enabling it, or you will have to tick the checkbox every time you start Blender:

bl_info = {
    "name": "My Enum Addon",
    "author": "Your Name Here",
    "version": (1, 0),
    "blender": (2, 75, 0),
    "location": "",
    "description": "",
    "warning": "",
    "wiki_url": "",
    "category": "Rigging",
}

import bpy

# Define our own panel (optional)
class HelloWorldPanel(bpy.types.Panel):
    """Creates a Panel in the Bone properties window"""
    bl_label = "Hello World Panel"
    bl_idname = "OBJECT_PT_hello"
    bl_space_type = 'PROPERTIES'
    bl_region_type = 'WINDOW'
    bl_context = "bone"

    def draw(self, context):
        layout = self.layout

        pbone = context.active_pose_bone
        if pbone is not None:
            layout.prop(pbone, "my_enum")
        else:
            layout.label("No active PoseBone.")


def my_enum_cb(self, context):
    print(self, self.my_enum, context)


def register():
    bpy.types.PoseBone.my_enum = bpy.props.EnumProperty(
        items=(
            ('identi_1', 'Name 1', 'Descript 1'),
            ('identi_2', 'Name 2', 'Descript 2'),
            ('identi_3', 'Name 3', 'Descript 3'),
        ),
        name="My Enum",
        default="identi_2",
        update=my_enum_cb
    )
    bpy.utils.register_module(__name__)


def unregister():
    bpy.utils.unregister_module(__name__)
    del bpy.types.PoseBone.my_enum


if __name__ == "__main__":
    register()

    # This is not supposed to be done in an addon.
    # Every access to bpy.data and bpy.contet during init is prohibited.
    #bpy.data.objects["Armature"].pose.bones["Bone"].my_enum = "identi_3"
$\endgroup$
  • $\begingroup$ What causes the change from Enum to float? If i dont register whole of properties should not be visible till i run script but here they are only change their type. Is the way i am assigning bpy.data.objects["Armature"].pose.bones["Bone"].my_enum = "identi_3" is this wrong? $\endgroup$ – ashwin Aug 7 '15 at 2:12
  • 1
    $\begingroup$ No, it's just that there's no difference between individual ID / Custom Properties and API-defined bpy.props properties internally. They are stored the same. Without the bpy.props definition, it will look like an ID property to Blender (it doesn't know that it's an enum, it only sees a string or number associated with an individual datablock). ID properties support very few data types. Complex data types like enums, collections etc. are build on top of that and need to be declared by a script. $\endgroup$ – CoDEmanX Aug 7 '15 at 9:26

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.