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.
obj = doc.GetSelection()
if obj != :
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.
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.
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.
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)
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:
return op is not None
@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 ?
test = doc.GetActiveBaseDraw()
if test[c4d.BASEDRAW_DATA_SHOWSAFEFRAME] == 1:
Thanks in advance,
I don't see much room for optimization here.
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".