How do I change parameters in Camera using user data



  • I'm trying to get a user data button to change the parameter of a camera using a Python Generator as I was adviced on C4D cafe that this was the only way...I've managed to get it working by acknowledging the button press and printing to the console but for the life of me I can't get it to change the focal length of the camera...

    Here's the code I currently have, it's what I assume would work depending on which button you press..The camera focal length is driven by user data so it's sitting in the Python Generator which is the parent.

    Here's the file just a simple camera rig I'm trying to make which has button presets...I know it's simple but I just can't get it to pass on the info and feel so lost in python documentation :/

    CameraRig_002_pythonized.c4d

    And here's the code...

    import c4d
    #Welcome to the world of Python
    
    
    def main():
        return c4d.BaseObject(c4d.Onull)
    
    def message(msgId, msgData):
        if msgId == c4d.MSG_DESCRIPTION_COMMAND:
            if msgData['id'] == c4d.DescID(c4d.DescLevel(c4d.ID_USERDATA), c4d.DescLevel(9)):
            op.GetObject()[c4d.ID_USERDATA,8] = 36
                print "36mm"
            elif msgData['id'] == c4d.DescID(c4d.DescLevel(c4d.ID_USERDATA), c4d.DescLevel(13)):
            op.GetObject()[c4d.ID_USERDATA,8] = 50
                print "50mm"
            elif msgData['id'] == c4d.DescID(c4d.DescLevel(c4d.ID_USERDATA), c4d.DescLevel(15)):
            op.GetObject()[c4d.ID_USERDATA,8] = 300   
                print "80mm"
        return True
    


  • Hi woodstar, thanks for reaching out us.

    With regard to your issue, the problem is that you're addressing an invalid object when you call `op.GetObject(). This is typically the case when you've a Python Tag attached to an object and by using that statement you're able to retrieve the parent object of the Python Tag.
    In your case instead it's enough to write something like:

    ...
    def message(msgId, msgData):
        if msgId == c4d.MSG_DESCRIPTION_COMMAND:
            if msgData['id'] == c4d.DescID(c4d.DescLevel(c4d.ID_USERDATA), c4d.DescLevel(9)):
                op[c4d.ID_USERDATA,8] = 36
                print "36mm"
            elif msgData['id'] == c4d.DescID(c4d.DescLevel(c4d.ID_USERDATA), c4d.DescLevel(13)):
                op[c4d.ID_USERDATA,8] = 50
                print "50mm"
            elif msgData['id'] == c4d.DescID(c4d.DescLevel(c4d.ID_USERDATA), c4d.DescLevel(15)):
                op[c4d.ID_USERDATA,8] = 80
                print "80mm"
        return True
    ...
    

    That said, considering the usual scope of a Python Generator which actually is to "generate" geometry and the one pertaining to a Python Tag which instead is to react programmatically to certain user interactions (like pressing a button) I suggest to reconsider your solution, eventually getting rid of the Xpresso network, and embedd the whole functionality in a simpler scripting tag.

    Best, Riccardo



  • Thanks for the reply!

    Finally managed to get it working, but only when the user data is placed on the Python tag itself...Which is a little messy to say the least, as you mentioned..

    I would prefer to do this as a script but don't know where to start...The Cinema4d Documentation is very overwhelming...

    Could you answer these few questions it would really help clear things up for me as I would love to get into plugin development?

    Can you create user interfaces with Python or do you somehow build the tool interface with user data? If I wanted this camera rig to be setup via a button would I have to create the xpresso interface or would this be done another way?

    Also, is it best to learn Python basics before jumping into C4D plugin development? If so what are the best starting points?

    While working in Cinema4d for the past few years I've had so many ideas for workflow plugins that I'd absolutely love to make.



  • Can you create user interfaces with Python or do you somehow build the tool interface with user data?

    Yes you can! Cinema 4D Python API comes with a complete set of classes to deliver full-fledged UI to your scripts or plugins. Please start looking at the examples in the Python Github repository to get an idea on Python API potentials.

    If I wanted this camera rig to be setup via a button would I have to create the xpresso interface or would this be done another way?

    Depending on the final purposes, skipping the Xpresso graph in favor of a Python-based logic could be preferred if the logic is supposed not to be changed by development-agnostic guys. The initial idea of Xpresso was exactly of providing a convenient and easy-to-use interface to implement programmatic behaviors or models without writing a single line of code.

    Also, is it best to learn Python basics before jumping into C4D plugin development? If so what are the best starting points?

    Definitively it is! I warmly recommend going through a first dive in Python, get comfortable with those concepts representing its development foundation and then move to the Cinema 4D Python API. This will help you to better understand the examples.

    Best, Riccardo


Log in to reply