Jump to content

Eterea

Help translating this script to Python?

Recommended Posts

Hi guys!

 

I’m here trying to convert this small COFFEE script to Python, without luck… :-/

 

Could you give me a helping hand?

 

It’s an interesting script to toggle between Default Perspective and the linked camera(s) on Stage Object.

 

Specially useful for when you have, say, 10 different animated cameras (linked to a single animated Stage Object) and you need to frequently switch between those cameras view and the Default Perspective.

 

Here is the COFFEE script by B.Horgan:

 

// CamSwitch.CSC
// CamSwitch V1 B.Horgan 2010 - works with Stage Object to toggle between editor and and linked cameras in stage object
//     (just switches the stage on and off and refreshes the view )
// NB - rename the Stage Object to 'CamSwitch' for the script to work

var switcher,state;

switcher=doc->FindObject("CamSwitch");//Find the Stage object (searching by name)
if(switcher)//if the stage object exists - 
{
state = switcher#ID_BASEOBJECT_GENERATOR_FLAG;
if (state==0)
{
switcher#ID_BASEOBJECT_GENERATOR_FLAG=1;
CallCommand(12147);//redraw view to activate linked camera in Stage Object
}
else 
{
switcher#ID_BASEOBJECT_GENERATOR_FLAG=0;
CallCommand(12202); // Switch to the editor camera
}
}

}

Thank you so much in advance! :-)

Share this post


Link to post
Share on other sites

What is the problem you encountered?

 

With the help of MAXON's Python SDK documentation I was able to convert it into the following Python script.

Having no experience with the Stage object, I actually don't know what to expect. I hope the converted script does what it is supposed to ...

 

# CamSwitch.py
# CamSwitch.CSC V1 B.Horgan 2010, converted to Python Daniel Sterckx 2018
# works with Stage Object to toggle between editor and and linked cameras in stage object
# (just switches the stage on and off and refreshes the view )
# NB - rename the Stage Object to 'CamSwitch' for the script to work

import c4d

def main():
    
    switcher = doc.SearchObject("CamSwitch")
    if switcher:
        state = switcher[c4d.ID_BASEOBJECT_GENERATOR_FLAG]
        if state == 0:
            switcher[c4d.ID_BASEOBJECT_GENERATOR_FLAG] = 1
            c4d.CallCommand(12147) # redraw view to activate linked camera in Stage Object
        else: 
            switcher[c4d.ID_BASEOBJECT_GENERATOR_FLAG] = 0
            c4d.CallCommand(12202) # Switch to the editor camera


if __name__=='__main__':
    main()

 

  • Namaste 1

Share this post


Link to post
Share on other sites
  • Topic Author
  • Wow, you are fast!

     

    THANK YOU SO MUCH, C4DS. It works like a charm :-)

     

    And looking at you code (and comparing with the original COFFEE) I thing I will be able to translate some other short similar scripts. I'm a complete novice with C4D scripting…

     

    Very grateful :-)

     

    PD. The Stage object allows to animate which camera is active on each segment of an animation.

     

    — Imagine you have 3 cameras, A-B-C, and each one of these cameras is animated

    — Cam_A will render from 000 to 099

    — Cam_B will render from 100 to 199

    — Cam_C will render from 200 to 299

     

    With Stage object you can animate the automatic change from Cam_A to Cam_B from 099 to 100. And later, the change from Cam_B to Cam_C from 199 to 200

     

    Once you add a Stage object to a scene and animate the active cameras (no matter you have 1, 2 o 30 cameras) this Stage object takes control of what you see on viewport (and what is rendered) if that Stage is enabled.

     

    And with this script you can quickly switch from those Custom Cameras POV to the Regular Default Perspective camera, go and back (you need to rename the Stage object to “CamSwitch” . I use the CMD-0 shortcut for that.

     

    Hope this explanation is good enough :-)

     

    Best regards.

    • Like 1

    Share this post


    Link to post
    Share on other sites

    You're welcome.

    Glad the script is working and useful to learn from.

     

    Just a note that the original COFFEE script you posted is not complete.

    So, for others learning to convert COFFEE into Python, note that that the COFFEE script is missing something like

    main()
    {

    right between the end of the comment and the var declaration.

    • Namaste 1

    Share this post


    Link to post
    Share on other sites

    Create an account or sign in to comment

    You need to be a member in order to leave a comment

    Create an account

    Sign up for a new account in our community. It's easy!

    Register a new account

    Sign in

    Already have an account? Sign in here.

    Sign In Now

    • Recently Browsing   0 members

      No registered users viewing this page.

    ×