2
$\begingroup$

I'm a total beginner at coding and I'm having some troubles with my first script.

I copied the main structure from a script that toggles layer visibility for some bones from a rigging course I did at IHMAN 3D School. Now I'm trying to tweak it to fit my needs.

Aside from a quicker access to some Armature functions via Toolshelf, I want to be able to set all the selected bones to Deform=True/False at once with a button. I marked the line I wrote to accomplish this.

The script as is now goes as follows:

 import bpy

 class dwarfButtons(bpy.types.Panel):


    bl_space_type = "VIEW_3D"
    bl_region_type = "TOOLS"
    bl_category = "Rigging"
    bl_label = "Dwarf Rig Layers"

    @classmethod
    def poll(self,context):
        if(bpy.context.active_object.type == 'ARMATURE'):
            return(True)

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

        row.label("Edit/Display options:")
        row = layout.row()
        row.prop(context.active_object.data, "use_mirror_x", toggle=True, text="edit: x mirror")
        row = layout.row()
        row.prop(context.active_object.data, "show_names", toggle=True, text="names")
        row = layout.row()
        row.prop(context.active_object.data, "show_bone_custom_shapes", toggle=True, text="shapes")
        row = layout.row()
        row.prop(context.active_object.data, "show_axes", toggle=True, text="axe display")
        row = layout.row()
        row.prop(context.active_object, "show_x_ray", toggle=True, text="xRay")
        row = layout.row()
        row.prop(context.selected_bones, "use_deform", toggle=True, text="Deform OFF") #THIS LINE



def register():
    bpy.utils.register_class(dwarfButtons)

def unregister():
    bpy.utils.unregister_class(dwarfButtons)


if __name__=="__main__":
    register()

I don't totally know what I'm doing yet. I discovered what functions to call trough the info panel and quick searches through the Python API.

$\endgroup$
  • $\begingroup$ You have to say what exactly the problem is if you paste some code. Does the script fail or does nothing etc? $\endgroup$ – iKlsR Aug 24 '14 at 18:10
5
$\begingroup$

In edit mode select the bones you want to change and press ShiftW (on windows) and you get "Toggle Bone Options" -popup. There you can alter the deform option for all the bones selected.

Tested with Blender 2.75

| improve this answer | |
$\endgroup$
  • 1
    $\begingroup$ You can also with bones selected right-click the Deform checkbox and click Copy To Selected. That will set it for all the bones. $\endgroup$ – Jaroslav Jerryno Novotny Sep 20 '15 at 11:23
2
$\begingroup$

The class you have created defines a panel, which is a visual element that contains items within a box that can be collapsed. The properties you display with row.prop(context.active_object.data...) will always correspond to the one active object, that is the selected object that has lighter selection highlighting than the others.

The buttons that can appear in a panel are a way to execute an operator that performs a task. An operator can perform it's task on any available data, although it is often on all selected items (not just the active item).

To change the properties on all selected bones you want to define an operator. Using blender's text editor you can start with a template for Operator Simple and you can add a panel based class to the same script if you want to. The bl_label for the operator can be used to search for it in the space bar menu, or you can use the bl_idname to refer to it in a panel or calling it directly from python.

The example operator has bl_idname set to object.simple_operator to start with, you can place a button in a panel to use it with -

row.operator("object.simple_operator")

The task you describe will use data that is not always defined so you need to check the existence before trying to access them, for the operator this will best be done in it's poll method.

@classmethod
def poll(cls, context):
    return context.active_object is not None and context.active_object.type == 'ARMATURE'

You can then alter the function used by the example operator to turn off the deform for the bones.

def main(context):
    if context.active_object.mode == 'POSE':
        for pb in context.selected_pose_bones:
            pb.bone.use_deform = False
    elif context.active_object.mode == 'EDIT':
        for pb in context.selected_bones:
            pb.use_deform = False
| improve this answer | |
$\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.