Method `BaseTake.SetCamera(self, takeData, camera)` does not set the camera

Hello everyone,

I am developing Python Plugins for C4D R21(did not update to Python3 yet) and I am having issues with generating takes. I am trying to set the render camera, but for some reason, after execution of the command, camera is not set (BaseTake.GetCamera(self, takeData) returns None). This method works completely fine in other parts of the plugin (that my ex-colleague wrote), with almost the same conditions.

The method I am using to set the camera is: BaseTake.SetCamera(self, takeData, camera)
Description here: https://developers.maxon.net/docs/Cinema4DPythonSDK/html/modules/c4d.modules/takesystem/BaseTake/index.html?highlight=setcamera#BaseTake.SetCamera

I am sure that camera I want to set is a proper C4D camera object, it is not Null. My take object is also initialized and render settings loaded. Are there any specific limitations to this method, or something else that needs to be taken into consideration when using the method, that I just cannot see at the moment?

Thank you very much in advance!

Kind regards,
Paula

Hello @paula-kokic,

thank you for reaching out to us. We have an example on Github for using BaseTake.SetCamera. What also might be an obstacle is to allow overrides for the take you are trying to modify. Please refer to user manual to learn more about overrides. A source for confusion could also be simply not making the created take the active one, i.e., have it reflected in the viewport, or flag it as being "checked". I did provide a modified variant of the GitHub example for these two cases below.

But in the end, this is all very speculative. Specifically for the scenario where "[...] this method works completely fine in other parts of the plugin [...], with almost the same conditions [..]", it is impossible to give any good answers without having executable code. When you are not at liberty to share your code publicly, you can send it to our sdk-email address. Please refer to our Forum Guide under Confidential Data when there are NDAs involved.

Cheers,
Ferdinand

The very slightly modified example:

""" Example for handling cameras with takes.

Based on the GitHub example, I just added two lines prefixed with the the
[FH] comment at the end.

Based upon:
    https://github.com/PluginCafe/cinema4d_py_sdk_extended/blob/
    13f9d2a95b3a2593f2f72ab49c542a922b32dcbb/scripts/04_3d_concepts/
    scene_elements/take_system/takesystem_cameras_r17.py

As discussed in:
    https://plugincafe.maxon.net/topic/13398/
"""
import c4d


def main():
    # The TakeData from the active document, holding all take information.
    if takeData is None:
        raise RuntimeError("Failed to retrieve the take data.")

    # Gets the currently selected objects
    selectedObjects = doc.GetActiveObjects(c4d.GETACTIVEOBJECTFLAGS_0)

    # Iterates the selected Objects
    for element in selectedObjects:

        # Checks if the object is a Camera object
        if not element.CheckType(c4d.Ocamera):
            continue

        # Adds a Take for this camera
        cameraTake = takeData.AddTake(
            "Take for Camera " + element.GetName(), None, None)
        if cameraTake is None:
            continue

        # Defines the active camera for this Take
        cameraTake.SetCamera(takeData, element)
        
        # [FH] Set the camera take as checked, i.e., being respected in a 
        # rendering.
        cameraTake.SetChecked(True)
        
    # [FH] Set the last camera take we did create as the active one.
    takeData.SetCurrentTake(cameraTake)

    # Pushes an update event to Cinema 4D
    c4d.EventAdd()


if __name__ == '__main__':
    main()

@ferdinand Thank you very much for your answer!

I checked if all that you wrote is present in my code - and it is.

Indeed, my take is generated, but constantly from the wrong camera - to be precise: from the main scene camera instead the one I want to render from.

Even if I dynamically create a new camera with c4d.CameraObject() and put it into SetCamera() method, it will work.

However, I realized today that somehow hierarchical position of the camera object makes a difference. When the render camera is attached to the root of the document it works. If it's under a Null object (e.g. 3rd level), it doesn't work. Do you have any explanation why would this be a game changer?

At the moment, my workaround would be to make a clone of my camera and insert it at the root of the document, create a take, and then delete it after usage - this seems to work completely fine.

Even though I have a workaround, I would be interested in knowing what is the connection between object hierarchy and using SetCamera() method for the Take.

Thanks again!

Kind regards,
Paula

Hello @paula-kokic,

as I said, it is impossible to give an answer without additional data: code, scenes, etc. We found a bug regarding cameras and Takes in the batch renderer not long ago, so, you could also encounter an undiscovered bug. The hierarchy constraint you mention sounds a bit like a bug to me. But a user error is more likely in the end, especially when you say it is working in one place of the code but not in another. One thing that could also be a problem, is that you might be doing what you are doing from a threaded context. Adding takes is of course a way to modify the scene graph. Which is forbidden in a threaded context. Read more about this topic here.

But again, without code and a scene where this does happen for you, it is not possible to help you here reliably. By either confirming that this is a bug or by pointing out a user-induced problem or misunderstanding.

Cheers,
Ferdinand

Hello @ferdinand ,

I understand, of course. However, it's hard for me to give any code samples, not just because of privacy, but also because it's a really complex process we're doing in 5000+ lines of code in 4 Plugins that work together. Therefore, even trying to isolate/simplify something and send you the code and scene that you could test out yourself is quite impossible. And I really don't wanna waste your time by sending you the whole project and asking you for debugging.

In any case - thank you very much again for your engagement, I highly appreciate it! I have a workaround for now and might take a closer look at this another time. We can mark the topic as "solved" 🙂

Best Regards,
Paula

Hello Paula,

of course, I understand these restrictions, but on the other hand it is in this case hard to just guess the problem. Feel free to reopen this subject at any time.

Cheers,
Ferdinand