Jump to content

Juanf

Assign object to target

Recommended Posts

Hi all,

 

I did a small script on Python R20 where a camera is added to the scene with a target tag and a null, but what I can't find is a way to add the null to the target tag, could I get some assistance please?

 

Here is the script:

 

import c4d
#Welcome to the world of Python

def main():
    def tool():
        return c4d.plugins.FindPlugin(doc.GetAction(), c4d.PLUGINTYPE_TOOL)

    def object():
        return doc.GetActiveObject()

    def tag():
        return doc.GetActiveTag()

    def renderdata():
        return doc.GetActiveRenderData()

    def prefs(id):
        return c4d.plugins.FindPlugin(id, c4d.PLUGINTYPE_PREFS)

    c4d.CallCommand(5103) # Camera
    c4d.CallCommand(100004788, 50026) # New Tag
    object()[c4d.CAMERAOBJECT_USETARGETOBJECT] = True
    c4d.CallCommand(55000, 2) # Use Camera
    prefs(465001620)[c4d.PREF_INTERFACE_CENTEROBJECT] = True
    c4d.CallCommand(5140) # Null
    prefs(465001620)[c4d.PREF_INTERFACE_CENTEROBJECT] = False
    c4d.TARGETEXPRESSIONTAG_LINK

if __name__=='__main__':
    main()
    c4d.EventAdd()

 

Cheers,

Juan

Share this post


Link to post
Share on other sites

this is not really pythonic way to write a script :) Using callcommand will not get you very far in scripting.

Try this.

import c4d
from c4d import gui
#Welcome to the world of Python


def main():
    camera = c4d.BaseObject(c4d.Ocamera) # this creates the object in the memory, but does not add it to the scene
    tag = c4d.BaseTag(c4d.Ttargetexpression) # this creates the tag in memory
    null = c4d.BaseObject(c4d.Onull) # create the null in memory
    doc.InsertObject(camera) # insert the camera object in the scene
    doc.InsertObject(null) # insert the null
    camera.InsertTag(tag) # apply the tag to the camera
    tag[c4d.TARGETEXPRESSIONTAG_LINK] = null # set the target object in the tag to be the null
    c4d.EventAdd() # inform cinema that something has happend, so it updates.

if __name__=='__main__':
    main()

 

Share this post


Link to post
Share on other sites
  • Topic Author
  • Yeah you are right is just I'm reaaaaaaaaaaaally bad at coding or scripting, that's why I use only call commands :cry:

     

    Thank you so much for taking time to reply and sharing your script, the only problem I found is that the new created camera is in P 0,0,0 and R 0,-90,0 and I as trying to create the camera exactly where I'm looking at in the viewport, as it works in my call command thingy.

     

    Cheers,

    Juan

    Share this post


    Link to post
    Share on other sites
    import c4d
    from c4d import gui
    #Welcome to the world of Python
    
    
    def main():
        camera = c4d.BaseObject(c4d.Ocamera) # this creates the object in the memory, but does not add it to the scene
        tag = c4d.BaseTag(c4d.Ttargetexpression) # this creates the tag in memory
        null = c4d.BaseObject(c4d.Onull) # create the null in memory
        doc.InsertObject(camera) # insert the camera object in the scene
        doc.InsertObject(null) # insert the null
        camera.InsertTag(tag) # apply the tag to the camera
        tag[c4d.TARGETEXPRESSIONTAG_LINK] = null # set the target object in the tag to be the null
        bd = doc.GetActiveBaseDraw() # get the active base draw object (This class represents a Cinema 4D window)
        currentCam = bd.GetEditorCamera() # get the "point" you are looking from
        camMatrix = currentCam.GetMg() # get the matrix of that point (position, rotation, scale)
        camera.SetMg(camMatrix) # apply that matrix to your newly created camera
    
        c4d.EventAdd() # inform cinema that something has happend, so it updates.
    
    if __name__=='__main__':
        main()

     

    Share this post


    Link to post
    Share on other sites
  • Topic Author
  • That's excellent, thank you so much. Now the camera is in the right place, but the null is not created in the center of the viewport so the camera is facing to origin.

     

    I'm really sorry for bugging you with this, I think I should try again to learn python. I have tried three times already and I always give up, coding is not my thing.

     

    Cheers,

    Juan

     

     

    Share this post


    Link to post
    Share on other sites
    import c4d
    from c4d import gui
    #Welcome to the world of Python
    
    
    def main():
        camera = c4d.BaseObject(c4d.Ocamera) # this creates the object in the memory, but does not add it to the scene
        tag = c4d.BaseTag(c4d.Ttargetexpression) # this creates the tag in memory
        null = c4d.BaseObject(c4d.Onull) # create the null in memory
        doc.InsertObject(camera) # insert the camera object in the scene
        doc.InsertObject(null) # insert the null
        camera.InsertTag(tag) # apply the tag to the camera
        tag[c4d.TARGETEXPRESSIONTAG_LINK] = null # set the target object in the tag to be the null
        bd = doc.GetActiveBaseDraw() # get the active base draw object (This class represents a Cinema 4D window)
        currentCam = bd.GetEditorCamera() # get the "point" you are looking from
        camMatrix = currentCam.GetMg() # get the matrix of that point (position, rotation, scale)
        camera.SetMg(camMatrix) # apply that matrix to your newly created camera
    
        editor_size = bd.GetFrame() # gets the borders of the editor window in pixels
        x_center = editor['cr']/2 # 'cr' is the right border, divide by 2 and we get the x center
        y_center = editor['cb']/2 # 'cb' is the bottom border, divide by 2 and we get the y center
        center_point = bd.ProjectPointOnPlane(c4d.Vector(0, 0, 0), c4d.Vector(0, 1, 0), x_center, y_center)[0] # this function generates a 3D position on a virtual floor object, based on screen coordinates in x,y
        matrix = c4d.Matrix() # create an empty matrix
        matrix.off = center_point # set that matrix position to the center point
        null.SetMg(matrix) # set the null's matrix to the new matrix (with the center position)
        c4d.EventAdd() # inform cinema that something has happend, so it updates.
    
    if __name__=='__main__':
        main()

     

    • Like 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.

    ×