Output active camera name from a python node



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 11/06/2012 at 04:27, xxxxxxxx wrote:

    Hi guys,

    as title says, I'm trying to output the active camera name in a document from a Python node inside xpresso. I did a search and find this post: https://plugincafe.maxon.net/topic/5961/6071_activate-camera-a-very-simple-question-for-a-noob and thought It could solve the question, but I can't get it to work.

    A little background: I'm doing this to solve a problem I have on another setup. I made a simple xpresso that toggle the visibility of an object based on wich camera is active at the moment. Lets say you have a cube that is visible only when camera_2 is active and invisible when others cameras are active. In xpresso is pretty easy, I used a Perspective node to output the active camera link, feeded into a generic camera object to get the name out of it, then compared the two names and used the bool result to toggle visibility of the cube. The problem with this setup is that if you copy it in another scene, the Perspective node looses its link becoming an undefinied object.

    Hence I thought I could replace the Perspective node with a Python node that outputs (with a 'link' output port) the same information.. My setup so far consist in a Python node with this code:

    import c4d
      
    def main() :
         bd = doc.GetActiveBaseDraw()
         camera = bd.GetSceneCamera()
    

    and this is what I'm getting:

    If I try to provide the argument "doc" as stated in the console (like bd.GetSceneCamera(doc) ) the error in console becomes "Could not find port value for 'camera' in node 'Python'".

    Sorry for the long and noob post, just trying to be clear as possible :)

    Thanks
    Massimiliano



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 11/06/2012 at 05:59, xxxxxxxx wrote:

    I think You have to declare the variable 'camera' as a global variable in the 'main' function, as it is necessary for every outport.

    import c4d
    #Welcome to the world of Python
      
      
    def main() :
        global camera
        
        bd = doc.GetActiveBaseDraw()
        camera = bd.GetSceneCamera(doc)
    

    Cheers
    Peter



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 11/06/2012 at 10:30, xxxxxxxx wrote:

    Hi and thanks for your reply !

    I didn't know outputs needs to be global variables :) I modified the script and now Its working...but just until I move one of the cameras. If I move something the node outputs a generic "camera" name (that is editor camera I think). 
    The code I'm using now:

    import c4d
      
    def main() :
         global camera_link
         global camera_name
         bd = doc.GetActiveBaseDraw()
         camera_link = bd.GetSceneCamera(doc)
         camera_name = camera_link.GetName()
         print "camera_link link = ", camera_link
         print "camera_link name = ", camera_name
    

    the file: https://dl.dropbox.com/u/8062081/active_camera.c4d.zip

    cheers
    Massimiliano



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 11/06/2012 at 13:57, xxxxxxxx wrote:

    When your Python node has finished processing, Py4D searches the names of output-ports (only "camera" in your case) in the global scope. In order to put the variable into the global scope, rather than into the local, one has to declare the name as global. You could also modify the global dictionary.

    Theese two examples have the equivalent effect:

    def main() :   
        global camera   
        camera = doc.GetActiveBaseDraw().GetSceneCamera(doc)
    
    def main() :   
        camera = doc.GetActiveBaseDraw().GetSceneCamera(doc)   
        globals()['camera'] = camera
    

    Cheers,
    -Niklas



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 11/06/2012 at 14:53, xxxxxxxx wrote:

    If You're moving the camera (or any other object) in one of the ortho views, then this view panel is the active basedraw with her own scene camera.
    If You want to receive the camera of the render view, you have to call

    bd = doc.GetRenderBaseDraw()
    

    instead of 'GetActiveBaseDraw()'

    I hope that helps You.

    Cheers
    Peter



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 11/06/2012 at 15:01, xxxxxxxx wrote:

    yeah, that helps a lot ! Thanks no photo :) and tnx Niklas too !


Log in to reply