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

    Getting cache of a Python Generator under a Python Generator

    Cinema 4D SDK
    python sdk
    2
    3
    684
    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.
    • orestiskonO
      orestiskon
      last edited by orestiskon

      Hi all,
      using a python generator as a source for another python generator can be helpful for prototyping. But I'm having trouble getting the cache of the child generator.

      GetCache always returns me "None", and using current state to object also returns "None".

      This is a code sample:

      def main():
          obj = op.GetDown().GetClone()
          print obj.GetCache()
          return obj
      

      I tried to see if it has children or siblings with GetDown or GetNext but I get:
      "AttributeError: 'NoneType' object has no attribute 'GetDown'"

      Code with current state to object:

      def currentState(obj) :
          return c4d.utils.SendModelingCommand(c4d.MCOMMAND_CURRENTSTATETOOBJECT,[obj],c4d.MODELINGCOMMANDMODE_ALL,c4d.BaseContainer(),doc)[0]
      
      def main():
          obj = op.GetDown().GetClone()
          collapsed = currentState(obj)
          print collapsed.GetChildren() //returns []
          return collapsed
      

      In the docs there is a recursion function for the cache but I'm not sure how to use it.

      I'm trying to understand what I'm doing wrong, are there suggestions on how to achieve this?
      Here is a sample file:
      python_under_python.c4d

      orestiskonO 1 Reply Last reply Reply Quote 0
      • orestiskonO
        orestiskon @orestiskon
        last edited by

        Update:

        def main():
            obj = op.GetDown().GetCache().GetClone()
            print obj
            return obj
        

        getting the cache before cloning does seem to work, I couldn't see the object as it was off screen before.
        Still it opens some questions such as why the clone didn't initially work; I assume I needed to insert it into the doc, which I had tried without much success (crashed cinema when I tried to evaluate the doc).

        In any case, the problem seems solved for now.

        1 Reply Last reply Reply Quote 0
        • M
          m_adam
          last edited by m_adam

          Hi @orestiskon SendModelingCommand and especially CurrentStateToObject need the object to be actually in the passed document.

          So you need to create a temporary doc and use it to operates.
          As bellow:

          import c4d
          
          def currentState(obj) :
              return c4d.utils.SendModelingCommand(c4d.MCOMMAND_CURRENTSTATETOOBJECT,[obj],c4d.MODELINGCOMMANDMODE_ALL,c4d.BaseContainer(),obj.GetDocument())[0]
          
          def main():
              # Retrieve the python generator
              obj = op.GetDown()
              if obj is None:
                  return
          
              objClone = obj.GetClone()
              if objClone is None:
                  raise RuntimeError("Failed to clone the object")
          
              # Make the children Python Generator disapear
              obj.Touch()
          
              # Creates a temporary document that will be used to evaluate the cache of the object
              tempDoc = c4d.documents.BaseDocument()
              if tempDoc is None:
                  raise RuntimeError("Failed to create a temporary doc")
          
              # Inserts the child python generator that exists only in memory into our tempo doc
              tempDoc.InsertObject(objClone)
          
              returnedObj = currentState(objClone)
          
              return currentState(objClone)
          

          Cheers,
          Maxime.

          MAXON SDK Specialist

          Development Blog, MAXON Registered Developer

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