- 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.
- 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.
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.
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.
from c4d import gui
"""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:
"""Open a dialog with the selected object's name.
if op is None:
active_obj_name = op.GetName()
gui.MessageDialog("You selected: " + active_obj_name)