Maxon Developers Maxon Developers
    • Documentation
      • Cinema 4D Python API
      • Cinema 4D C++ API
      • Cineware API
      • ZBrush GoZ API
      • Code Examples on Github
    • Forum
    • Downloads
    • Support
      • Support Procedures
      • Registered Developer Program
      • Plugin IDs
      • Contact Us
    • Categories
      • Overview
      • News & Information
      • Cinema 4D SDK Support
      • Cineware SDK Support
      • ZBrush 4D SDK Support
      • Bugs
      • General Talk
    • Unread
    • Recent
    • Tags
    • Users
    • Login

    Snap object in mouse input

    PYTHON Development
    0
    25
    15.9k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • H
      Helper
      last edited by

      On 12/01/2013 at 14:28, xxxxxxxx wrote:

      Originally posted by xxxxxxxx

      or you have to make an instance of your GeDialog class a memeber of your
      ToolData class.

      class ToolDataClass() :
          def __init__(self) :
              self.dialog = None
              self.data = None
          
          def doSomething(self) :
              if (self.dialog) :
                  print self.Dialog.linkBox.GetLink(document, c4d.Obase)
              else:
                  print 'dialog has not been raised yet.'
              
          def AllocSubDialog(self, bc) :
              self.Dialog = SettingsDialog(self.data)
              return self.Dialog
      

      edit : fixed a typo,  should be if (self.dialog) : of course and not if(self.data) :

      Work!!! I succeeded! littledevil Thanks!!!!!!! 🤝

      1 Reply Last reply Reply Quote 0
      • H
        Helper
        last edited by

        On 19/07/2013 at 09:28, xxxxxxxx wrote:

        Originally posted by xxxxxxxx

        You need to use the c4d.utils.GeRayCollider class to find an intersection on the surface of the
        destination-object, intersected by the line defined by the mouse-position in global space and
        the camera position.

        op = doc.GetActiveObject()
              op_next = op.GetNext()
              if not op_next:
                  return True

        # Convert op_next to a polygon object.
              # Simplified here, just for demonstration purpose.
              op_next = op_next.GetCache()
              if not op_next or not op_next.CheckType(c4d.Opolygon) :
                  print "Cache is not existent or is not a polygon object."
                  return True
             
              win.MouseDragStart(button=device, mx=int(mx), my=int(my), flags=c4d.MOUSEDRAGFLAGS_DONTHIDEMOUSE)
              result, dx, dy, channel = win.MouseDrag()
              while result==c4d.MOUSEDRAGRESULT_CONTINUE:
                  mx += dx
                  my += dy            
         
                  cursorpos = bd.SW(c4d.Vector(mx,my,500))     #screen to world conversion
                  if(axis == 1) :
                      cursorpos.x = 0
                  elif(axis == 2) :
                      cursorpos.y = 0                          #Constrain drawing along an axis based on the comboButton's value
                  elif(axis == 3) :
                      cursorpos.z = 0

        cam_pos = bd.GetSceneCamera(doc).GetAbsPos()
                  collider = c4d.utils.GeRayCollider()
                  collider.Init(op_next)

        length = 500000
                  direction = -(cam_pos - cursorpos).GetNormalized()

        did_intersect = collider.Intersect(cam_pos, direction, length)
                  if did_intersect:
                      position = collider.GetNearestIntersection()['hitpos']
                      print "Intersection at", position
                  else:
                      print "No intersection."
                      position = cursorpos

        doc.AddUndo(c4d.UNDOTYPE_CHANGE, op)
                  op.SetAbsPos(position)
                  op.Message(c4d.MSG_UPDATE)

        c4d.DrawViews(c4d.DA_ONLY_ACTIVE_VIEW|c4d.DA_NO_THREAD|c4d.DA_NO_ANIMATION)
                  result, dx, dy, channel = win.MouseDrag()

        -Nik

        an example for snap on point object? thanks 🙂

        1 Reply Last reply Reply Quote 0
        • H
          Helper
          last edited by

          On 30/08/2013 at 07:08, xxxxxxxx wrote:

          Originally posted by xxxxxxxx

          What do you mean with "the snap does not work"? You are not even trying to snap the
          object, just setting it to the cursor position.

          joint.SetAbsPos(cursorpos)

          If you want to to snap to a grid with a spacing of 50 units (just for the sake of example), you
          need to adjust the position vector.

          def snap_value(value, step) :
                rest = value % step
                on_grid = value - rest

          if rest / float(step) >= 0.5:
                    on_grid += step

          return on_grid

          def snap_grid(vector, gridsize) :
                vector = c4d.Vector(vector)
                vector.x = snap_value(vector.x, gridsize)
                vector.y = snap_value(vector.y, gridsize)
                vector.z = snap_value(vector.z, gridsize)
                return vector

          # ...

          gridsize = 50
            position = snap_grid(cursorpos)
            joint.SetAbsPos(position)

          See the full code here.

          -Niklas

          HI NiklasR How do you adjust the cursor position for snap points ? 🙂

          1 Reply Last reply Reply Quote 0
          • H
            Helper
            last edited by

            On 08/09/2013 at 16:07, xxxxxxxx wrote:

            Originally posted by xxxxxxxx

            an example for snap on point object? thanks 🙂

            Assuming you have a coordinate in 3D space, you can just use the nearest mesh point and maybe
            check if the point is within the snap radius.

                import c4d
            
                
            
                def snap_point(point, radius, obj) :
            
                    """
            
                    Calculate the snapping point from a coordinate in 3D space
            
                    to a point object within the defined \*radius\*. Requires
            
                    the \*point\* in global space.
            
                    """
            
                
            
                    mg = obj.GetMg()
            
                    points = obj.GetAllPoints()
            
                    point = ~mg \* point
            
                    radius \*\*= 2
            
                
            
                    nearest = None
            
                    for p in points:
            
                        delta = (p - point).GetLengthSquared()
            
                        if delta <= radius and (not nearest or nearest[1] > delta) :
            
                            nearest = p, delta
            
                
            
                    if nearest:
            
                        point = nearest[0]
            
                    return point
            
                
            
                def main() :
            
                    p = c4d.Vector(102, 99, 97)
            
                    print snap_point(p, 10, op)
            
                
            
                main()
            
              
            
            
            Best regards,
            
            -Niklas
            
            1 Reply Last reply Reply Quote 0
            • H
              Helper
              last edited by

              On 08/09/2013 at 16:10, xxxxxxxx wrote:

              Originally posted by xxxxxxxx

              HI NiklasR How do you adjust the cursor position for snap points ? 🙂

              You can not "adjust" the cursor position, the cursor is where the mouse points it to. There is also
              a snapping mode for snapping to object points, so what exactly do you need?

              Cheers,
              -Niklas

              1 Reply Last reply Reply Quote 0
              • First post
                Last post