Solved R20 Script State Function

So it was announced that scripts will now get access to an on/off state for their icons.
I see that right in the script manager it tells how to work with it using the state() function. I have 2 questions about this.

  1. Only passing True, False and c4d.CMD_ENABLED|c4d.CMD_VALUE will effect the icon state correct? it can't be done outside of the state() function?
  2. I noticed when I put a print function inside of the state() function it seemed to loop endlessly? if I use this code:
def state():
    obj = doc.GetSelection()
    if obj != []:
        print "disabled"
        return c4d.CMD_ENABLED
    else:
        print "enabled"
        return c4d.CMD_ENABLED|c4d.CMD_VALUE

It also seems to update in real time, if I select something the icon becomes instantly enabled without executing the script. I feel like I'm doing it wrong haha.

  1. Only passing True, False and c4d.CMD_ENABLED|c4d.CMD_VALUE will effect the icon state correct? it can't be done outside of the state() function?

Yes. You may be able to perform calculations elsewhere and store a state value in the document's container that you access in the state() method, but I don't believe you can change the state elsewhere.

  1. I noticed when I put a print function inside of the state() function it seemed to loop endlessly? if I use this code:

Yes, it's "looping" every time the Cinema 4D interface updates/redraws.

Background

The state() function is seemingly run every time Cinema 4D's interface is redrawn. It's really there for determining whether a command that requires an object be selected is enabled or not. As it's run so frequently, any code you put in there could slow down all of C4D, so ensure that your state check is actually important, and if so, do your check as quickly as possible.

Example

For example, a script that prints the name of the selected object should only be enabled when an object is selected.

"""Name-en-US: Echo Name
Description-en-US: Opens a message dialog with the name of the selected object.
"""

import c4d
from c4d import gui

def state():
    """Gray out icon if no objects are selected, or more than one object is selected.
    """

    # `op` is a variable provided by C4D that represents the selected object.
    # I'm not certain, but I think this is faster than call doc.GetActiveObject()
    # but it will return `False` if more than one object is selected.
    if op is not None:
        return c4d.CMD_ENABLED
    
    else:
        return False

def main():
    """Open a dialog with the selected object's name.
    """
    
    if op is None:
        return

    active_obj_name = op.GetName()
    gui.MessageDialog("You selected: " + active_obj_name)

if __name__=='__main__':
    main()

References

Hi,

state() is called by Cinema 4D whenever needed to update the UI.
main() of a script is called when a script is executed but state() does not get called.
If you implement state() it is important to use main() like the default script does. Otherwise any code called outside of main() is evaluated with state().

Note returning c4d.CMD_ENABLED enables the state and returning 0 disables the state. Also True or False can be returned to respectively enable/disable the state.
c4d.CMD_VALUE can be returned in association with c4d.CMD_ENABLED to enable and check the script state.

The state() implementation posted by Donovan can be optimized and simply be defined as:

def state():
    return op is not None

Former MAXON SDK Engineer

@y_puech and @dskeith Thank you both, that is extremely helpful as I didn't see any other documentation on that. I can see how state() could become very dangerous very quickly. I will certainly proceed with caution and hope this will help others will to not abuse the state() function as well:)

I guess I'll append my question here.
How could this be optimized ?

def state():
    test = doc.GetActiveBaseDraw()
    if test[c4d.BASEDRAW_DATA_SHOWSAFEFRAME] == 1:
        return c4d.CMD_ENABLED|c4d.CMD_VALUE 
    else:
        return c4d.CMD_ENABLED

Thanks in advance,
kind regards
mogh

Hi mogh,

I don't see much room for optimization here.

Cheers,
Andreas

Thanks a_block,

Sadly I had to disable the state() function because it breaks (delays forever) Material Preview/Shader rendering.

This State() function should somehow be isolated if possible ... but i guess this is the limitation of "checking the gui state".

kind regards
mogh