C++ -> Python of DOCUMENTSETTINGS_GENERAL & DOCUMENT_STATEX
-
I found this C++ code to retreive the Axis State of C4D
axisstate.x = doc->GetData(DOCUMENTSETTINGS_GENERAL).GetBool(DOCUMENT_STATEX) ? 1 : 0;
I am trying to translate this to python including the weird syntax of
? 1:0
what does this do ? Set True to 1 and false to 0 ?
The Documentation only gave me this which didn't enlighten me.
Thank you for your time.
mogh -
result = <statement> ? 1 : 0;
When the statement before the question mark evaluates to true, then the part before the colon is assigned, else the right part is assigned.
In your case when the flag ofDOCUMENTSETTINGS_GENERAL
is set (= true) then the value 1 is assigned to axisstate.x, if flag is cleared (= false) then value 0 is set.in other words, it is a shortcut for
if (doc->GetData(DOCUMENTSETTINGS_GENERAL).GetBool(DOCUMENT_STATEX)) axisstate.x = 1; else axisstate.x = 0;
-
Hi @mogh This is a ternary operation,
python support it this way.
axisstate.x = 1 if doc.GetData(c4d.DOCUMENTSETTINGS_GENERAL).GetBool(DOCUMENT_STATEX) else 0
but regarding the statement, it could also be used without the ternary operator, since True == 1 as an integer so it will produce the same result
axisstate.x = int(doc.GetData(c4d.DOCUMENTSETTINGS_GENERAL).GetBool(DOCUMENT_STATEX))
Cheers,
Maxime -
thanks to all of you, got it working with
axisstate.x = int(doc.GetData(c4d.DOCUMENTSETTINGS_GENERAL).GetBool(c4d.DOCUMENT_STATEX))
kind regards mogh