Transform mouse coordinates to c4d coordinates



  • I have a command plugin with a (floating) dialog.
    After the user gives a command, he must click on the screen to indicate the location where the command plugin should place the object.
    I can get the mouse coordinates, but I do not know how to convert them to coordinates to place the object.
    For example, if the user clicks on the Top View in the center, the object must be placed at (0,0,0).

    msg = c4d.BaseContainer()
    while self.GetInputState(c4d.BFM_INPUT_MOUSE, c4d.BFM_INPUT_MOUSELEFT, msg):    #While the left mouse button is held down
      if msg.GetLong(c4d.BFM_INPUT_VALUE)==0: break
    
    while self.GetInputState(c4d.BFM_INPUT_MOUSE, c4d.BFM_INPUT_MOUSELEFT, msg):    #Wait for next mouse click
      if msg.GetLong(c4d.BFM_INPUT_VALUE)==1: break
    
    while self.GetInputState(c4d.BFM_INPUT_MOUSE, c4d.BFM_INPUT_MOUSELEFT, msg):    #While the left mouse button is held down
      if msg.GetLong(c4d.BFM_INPUT_VALUE)==0: break
    
    
    mouseX = int(msg[c4d.BFM_INPUT_X])
    mouseY = int(msg[c4d.BFM_INPUT_Y])
    print mouseX, mouseY
    
    


  • Hi @pim, I'm not sure a CommandData is the best place for doing this stuff, a ToolData offers the InputEvent which is far more suited for this.

    With that's said in a CommandData you actually get now way to be sure in which space you are currently retrieving the coordinate from.

    Finally, you can retrieve the active BaseDraw, then call BaseView.SW.

    If you have any questions, please let me know.
    Cheers,
    Maxime.



  • Hi @m_adam,

    Thanks for the quick reply.

    I agree with you with you that I can better use a toolplugin.
    But that means calling the toolplugin from the command plugin and I thought it should also be possible from a command plugin.

    Unfortunately bd.SW(p) does not return what I expect.
    When I click in the origin of the front view, I get strange results.

    b06da0a0-e041-4b2c-8ed0-5fa1cd584662-image.png

    Here is what I do:

    mouseX = int(msg[c4d.BFM_INPUT_X])
    mouseY = int(msg[c4d.BFM_INPUT_Y])
    print "Mouse coord: ", mouseX, mouseY
    
    bd = doc.GetActiveBaseDraw()         
    print "Converted coord: ", bd.SW(c4d.Vector(mouseX, mouseY, 0))
    

    It seems, I read it somewhere, that the mouse coordinates I get, are relative to the command plugin dialog.
    Could that be the issue and if so, how to solve it?



  • Yes you are right this is relative to the current frame being drawn (understand it as the current GeDialog, SubDialog, GeUserArea) and there is no way to retrieve the frame position in the screen. As I said cinema 4D does not work this way.

    May I ask you why SubDialog of a ToolData is not enough for your needs?



  • Thanks for the answer.

    I just started with a command plugin.
    New requirements emerged and yes, perhaps I now change over to a tool plugin.


Log in to reply