2024 NodeData Init isCloneInit argument for older version
-
Hello!
In R2024 for NodeData plugins Init method now requires isCloneInit argument. But now, after code changing, older versions gives an error:TypeError: Init() takes exactly 3 arguments (2 given)
Is there a way to make this new code feature compatible with older Cinema 4D versions? Now I have to create two pypv files: up_to_2024.pypv and 2024_and_after.pypv. Which is not convinient for developement obviously.
Thank you! -
Hi @mikeudin,
I changed my python plugins to
def Init(self, op, isCloneInit=False):
So in older versions of Cinema4D it supposed to act as it act before
-
Hey @mikeudin,
Thank you for reaching out to us. @baca is right, default values are an elegant way to achieve signature overloading in Python. In cases where this does not suffice, you can also make use of the fact that everything is an object in Python, which means syntax akin to a preprocessor macro will work in Python.
"""Realizes a class with a method Bar which has a different number of arguments and output depending on a global constant, here the version of Cinema 4D. This makes use of the fact that functions are first class objects in Python. While Python has no function overloading in the strict sense, we can simply attach functions based on context. We could even do this at runtime based on a non-constant value. """ import c4d class Foo: """Implements Foo.Bar differently based on the version of Cinema 4D. """ if c4d.GetC4DVersion() >= 2024000: def Bar(self, arg1: int, arg2: bool) -> None: print ("NEW", arg1, arg2) else: def Bar(self, arg1: int) -> None: print ("OLD", arg1) # Calls Foo.Bar. The version guard is only here so that this example can be run in all versions of # Cinema without throwing an error (as the number of arguments changed). if c4d.GetC4DVersion() >= 2024000: Foo().Bar(42, True) else: Foo().Bar(42)
2024.0.0
and higher will run the two argument version and printNEW 42 True
, everything else will run the other version and printOLD 42
.Cheers,
Ferdinand -
@baca Thank you! Works like a charm!