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

    Knife Tool Control via Python

    PYTHON Development
    0
    4
    576
    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 31/12/2016 at 16:07, xxxxxxxx wrote:

      I am trying to develop a python script that will allow me to make a single knife cut across an individual polygon surface. 
      I am able to do this manually using the knife tool set to Line mode with "Restrict to Selection" enabled.
      I am able to make a knife cut across the surface using a Python script, however I am unable to select any of the specific knife parameters that would allow me to restrict the cut to a single polygon surface (for example the knife mode and restrict to selection option). 
      The code below makes a cut across a cube. This is adapted from the very helpful code example provided by Yannick Puech

      import c4d  
      from c4d import utils
      def SplineCutter(doc, op, bd, x1, y1, x2, y2, bSelect) :    
      	p1 = c4d.Vector(-50,200,-100)  
      	v1 = c4d.Vector(-50,200,-400)  
      	p2 = c4d.Vector(-50,-200,-100)  
      	v2 = c4d.Vector(-50,-200,-400)     
      	bc = c4d.BaseContainer()  
      	bc.SetVector(c4d.MDATA_KNIFE_P1, p1)  
      	bc.SetVector(c4d.MDATA_KNIFE_P2, p2)  
      	bc.SetVector(c4d.MDATA_KNIFE_V1, v1)  
      	bc.SetVector(c4d.MDATA_KNIFE_V2, v2)  
      	bc.SetBool(c4d.MDATA_KNIFE_RESTRICT,True)  
      	bc.SetBool(c4d.MDATA_KNIFE_NGONS, True)  
      	bc.SetLong(c4d.MDATA_KNIFE_MODE, c4d.MDATA_KNIFE_MODE_LOOP)
              if op is not None :  
      		utils.SendModelingCommand(command = c4d.MCOMMAND_KNIFE,  
      			list = [op],  
      			mode = c4d.MODELINGCOMMANDMODE_POLYGONSELECTION,  
      			bc = bc,  
      			doc = doc,  
      			flags = c4d.MODELINGCOMMANDFLAGS_CREATEUNDO)
              	c4d.EventAdd()
      def main() :  
      	frame = doc.GetTime().GetFrame(doc.GetFps())
          	if frame == 10:  
      		bd = doc.GetActiveBaseDraw()  
      		frame = bd.GetFrame()  
      		middleX = (frame['cr']-frame['cl'])/2  
      		spline = op.GetObject()      
      		SplineCutter(doc, spline, bd, middleX, frame['ct'], middleX, frame['cb'], True)
      if __name__=='__main__':
          main()
      

      However the below lines of code don't actually seem to have any effect:

      bc.SetBool(c4d.MDATA_KNIFE_RESTRICT,True)    
          bc.SetBool(c4d.MDATA_KNIFE_NGONS, True)
          bc.SetLong(c4d.MDATA_KNIFE_MODE, c4d.MDATA_KNIFE_MODE_LOOP)

      There is no effect when I change MDATA_KNIFE_RESTRICT from True to False. Either way the cut is not restricted to the polygon surface.
      There is no effect when I set c4d.MDATA_KNIFE_MODE to c4d.MDATA_KNIFE_MODE_SINGLE or c4d.MDATA_KNIFE_MODE_PATH or c4d.MDATA_KNIFE_MODE_LOOP. The cut mode always remains the same. 
      As a separate, but related question, setting "command = c4d.ID_MODELING_KNIFE_TOOL" instead of "command=c4d.MCOMMAND_KNIFE" (a format that works with other modeling commands such as c4d.ID_MODELING_EXTRUDE_TOOL) makes the knife cut stop working all together. 
      I am using c4D R2017

      Any ideas about how to actually set KNIFE_RESTRICT to True and actually set MDATA_KNIFE_MODE to Single would be helpful!

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

        On 02/01/2017 at 07:47, xxxxxxxx wrote:

        Hi and Welcome to the Plugin Cafe forums!

        Which version of Cinema 4D are you using? R17 or R18? R2017 version doesn't exist.

        Are you sure manually cutting really works as expected with "Restrict to Selection" and "Create N-gons" set?
        I get the same behavior with both the script and mouse interaction in the view: additional edges are created even on unselected polygon.

        c4d.ID_MODELING_KNIFE_TOOL is meant to be used with c4d.CallCommand() to activate the Knife tool in the UI.

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

          On 02/01/2017 at 16:25, xxxxxxxx wrote:

          Hi Yannick,

          I am using R17 (so the knife tool has multiple modes of line, path, loop, etc. rather than r18 where the functionality seems to be divided into different tools).

          If I have selected a specific face of a cube and use the settings I described I am able to get a cut in the face that is restricted to that face. 
          Here is a screenshot: 
          https://drive.google.com/file/d/0BzI60EdvLS19SDNvenRYX3hLd2M/view?usp=sharing

          And this is what it looks like when I use the script: 
          https://drive.google.com/file/d/0BzI60EdvLS19Q1ppejBhdnRvbFE/view?usp=sharing

          Thanks for the clarification about c4d.ID_MODELING_KNIFE TOOL

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

            On 04/01/2017 at 02:55, xxxxxxxx wrote:

            Hi,

            In fact the Knife tool (ID_MODELING_KNIFE TOOL) and Knife modeling command (MCOMMAND_KNIFE) are different in their implementation and parameter support.
            The Knife modeling command only supports the following parameters:
            MDATA_KNIFE_P1
            MDATA_KNIFE_P2
            MDATA_KNIFE_V1
            MDATA_KNIFE_V2
            MDATA_KNIFE_RESTRICT
            MDATA_KNIFE_SELECTCUTS (splines only)

            For these reasons the same results can't be obtained with both the interactive tool and modeling command.

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