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
    • Register
    • Login

    SendModelingCommand(CURRENTSTATETOOBJECT) Unable to get correct results

    Cinema 4D SDK
    2024 python
    3
    3
    413
    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.
    • J
      JACK0319
      last edited by

      Hi,

      I want to create a plane and spline and use the SplineWrap effector to wrap the plane around the spline and then return the cached polygon.
      5968dc73-b0bd-463c-9ba5-5ced6b05decf-image.png

      But the code doesn't return the correct result,
      Did I write something wrong somewhere?

      import c4d
      
      def main():
          height = 300
      
          temp_doc = c4d.documents.BaseDocument()
      
          # spline
          spline = c4d.BaseObject(5186)
          temp_doc.InsertObject(spline)
          
          # plane
          plane = c4d.BaseObject(5168)
          temp_doc.InsertObject(plane)
      
          plane[c4d.PRIM_PLANE_WIDTH] = 1000
          plane[c4d.PRIM_PLANE_HEIGHT] = height
          plane[c4d.PRIM_AXIS] = 4  # +z
          plane[c4d.PRIM_PLANE_SUBW] = int(plane[c4d.PRIM_PLANE_WIDTH]/10)
          plane[c4d.PRIM_PLANE_SUBH] = 1
      
          spline_wrap = c4d.BaseObject(1019221)
          spline_wrap.InsertUnderLast(plane)
          spline_wrap[c4d.MGSPLINEWRAPDEFORMER_SPLINE] = spline
          spline_wrap[c4d.MGSPLINEWRAPDEFORMER_AXIS] = 1
      
          # poly
          #c4d.documents.InsertBaseDocument(temp_doc) # Enabling this line will give different results
          temp_doc.ExecutePasses(bt=None, animation=True, expressions=True, caches=True, flags=0)
          
          bc = c4d.BaseContainer()
          bc[c4d.MDATA_CURRENTSTATETOOBJECT_INHERITANCE] = True
          bc[c4d.MDATA_CURRENTSTATETOOBJECT_BUILDFLAGS] = c4d.BUILDFLAGS_INTERNALRENDERER
          res = c4d.utils.SendModelingCommand(c4d.MCOMMAND_CURRENTSTATETOOBJECT, 
                                              [plane],
                                              mode=c4d.MODELINGCOMMANDMODE_ALL, 
                                              bc=bc, 
                                              doc=temp_doc, 
                                              flags=c4d.MODELINGCOMMANDFLAGS_NONE
                                              )
                                              
          print(res)
          cache = res[0]
          doc.InsertObject(cache.GetClone(0))
      
      
      if __name__ == '__main__':
          main()
          c4d.EventAdd()
      
      M 1 Reply Last reply Reply Quote 0
      • M
        mogh @JACK0319
        last edited by

        just a hunch - try a c4d.EventAdd() before converting
        and also at the end ...

        i_mazlovI 1 Reply Last reply Reply Quote 0
        • i_mazlovI
          i_mazlov @mogh
          last edited by

          Hello @JACK0319 ,

          I'm not sure what you consider the "correct result", but it looks good here:
          9888272b-b870-4514-80e9-e16f01e6dc14-image.png

          However, there're a couple things to mention about your script.

          First, there SendModelingCommand() function executes passes itself, so there's no need in munally running ExecutePasses() function.

          Second, in the last line you insert the result object from the CSTO command into document stored in doc variable. At this point doc still points to the document that was active before you executed function InsertBaseDocument(). After this function call, GetActiveDocument() returns the same document that you have in temp_doc. However, there's one edge case, which happens if you execute InsertBaseDocument() function while currently active document is empty. In this case, current document is deallocated and doc points to the invalid document. This is why you see the corresponding log in python console:

          ReferenceError: the object 'c4d.documents.BaseDocument' is not alive
          

          By the way, this is similar behavior you notice when clicking on [+] button 0dd7ae71-413a-4cf6-b981-b42ca06e488b-image.png multiple times: it kills currently active empty document and activates new empty document instead.

          There're multiple solutions for you depending on what are your intentions with this script:

          • If you only need baked object inserted to currently active document, there's no need in attaching your temporary document to cinema with the InsertBaseDocument() function
          • If otherwise you need the temporary document to be inserted to cinema, consider attaching it after you insert cache to the old document.
          • If you're attaching temp_doc to cinema only for debugging purposes, you can early exit your script to not let it go to the insertion step. Another option could be to "touch" original document (e.g. by inserting c4d.Onull object at the beginning of the script and removing it in the end).

          Cheers,
          Ilia

          MAXON SDK Specialist
          developers.maxon.net

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