1

I have a bone with various custom properties and I want to change the value of all of them, from 0 to 1 at the same time (pressing a button).

nexxio
  • 72
  • 6

1 Answers1

1

Have a look at the operator simple template in the text editor. Replace main(context) with this

def main(context):
    #active pose bone
    pb = context.active_pose_bone
    # all custom properties on bone
    for key in pb.keys():
        #ignore _RNA_UI
        if key == '_RNA_UI':
            continue
        pb[key] = 1
        # insert a keyframe
        pb.keyframe_insert('["%s"]' % key)

and the poll method to

def poll(cls, context):
    return context.active_pose_bone is not None

to make sure that there is an active pose bone selected.

and change the idname from object.simple_operator to something meaningful like posebone.set

Then have a look at the panel simple template and

row.operator("posebone.set")

will give you a UI button.

batFINGER
  • 84,216
  • 10
  • 108
  • 233
  • how can I insert a keyframe to all of them, using this method? – nexxio Jun 07 '16 at 21:18
  • @nexxio Added pb.keyframe_insert('["%s"]' % key) to code. Will add a keyframe for each custom property at the frame of button press. – batFINGER Jun 08 '16 at 05:17
  • thanks a lot! @batFINGER ...and, I wanted this operator duplicated so I could have and operator for 3 more buttons that will make the same but each one will add a new value for one specific custom property. I tested and it works great, but if I copy and paste the script in the same operator simple template and change the idname of the pasted one it does not work. so my question is that: can you have more than one custom operator defined in the same script(I mean something like: custom operators.py)? – nexxio Jun 08 '16 at 15:52
  • Each operator will also need a new class name eg SimpleOperator2 (think of a better one) and be registered bpy.utils.register_class(SimpleOperator2) in the register method. Could also pass operator arguments look at http://blender.stackexchange.com/questions/17751/how-display-and-use-operator-properties-in-a-python-blender-ui-panel/17755#17755 – batFINGER Jun 08 '16 at 16:37
  • I did change the class name, and registered and unregistered, but only one works( I just make one copy of the code and pasted it under to see if it works). I thing that maybe is because they both are in main(context): (by the way i dont know what it is for) I tried changin one to: def execute(self, context): but it shows error. – nexxio Jun 08 '16 at 19:12
  • Maybe put it together as another question. Did you look at the link re passing props to ops? – batFINGER Jun 08 '16 at 19:42
  • yes, I will put it as another question. and I look at the link but I dont understand, at the moment I have not enough knowledge in python, but I will!! thanks. – nexxio Jun 08 '16 at 22:21