3
$\begingroup$

I have a panel with a play button, which starts a timer. Every time I click on the button, it changes from play to pause and vice versa:

class PlayPanel(bpy.types.Panel):
    is_playing = False

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

def play_panel_draw(context, layout):
    row.operator("Play", text="", icon='PLAY' if not PlayPanel.is_playing else 'PAUSE')

Now, I want to change the icon also when the timer stops. How can I do that? In the draw function I'm passing the context and the layout. How can I pass them from the timer class?

$\endgroup$
2
$\begingroup$

The correct way to add a property to a class in blender is by using bpy.props, in your situation that doesn't help as adding bpy.props doesn't work with panels.

The solution would be to add your is_playing property to the scene and use that for your operator icon choice.

def play_panel_draw(context, layout):
    scn = context.scene
    row = layout.row()
    row.operator("scene.play", text="", icon='PLAY' if not scn.is_playing else 'PAUSE')

Here is a sample addon to demonstrate.

$\endgroup$
0
$\begingroup$

Your timer object should be None after removal. So you can add a test to your condition that checks if the timer object is False (None checks as false in an implicit condition):

row.operator(
    "Play",
    text = "",
    icon = 'PLAY' if not PlayPanel.is_playing or not timer else 'PAUSE'
)

timer is of course the arbitrary name I chose for your timer object, replace if needed.

$\endgroup$
  • $\begingroup$ Maybe I wasn't clear (I've just edited the question), in the draw function I'm passing the context and the layout. How can I pass them from the timer class? $\endgroup$ – Noam Peled Jan 15 '16 at 2:53
  • $\begingroup$ @NoamPeled Could you paste more of your code? It's pretty hard to answer this kind of question without the broader picture. $\endgroup$ – TLousky Jan 15 '16 at 8:37

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.