- 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.
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
- Python API Docs: c4d.plugins.CommandData.GetState
- C++ API Docs: Command Data Class