Maxon Developers Maxon Developers
    • Documentation
      • Cinema 4D Python API
      • Cinema 4D C++ API
      • Cineware API
      • ZBrush Python 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

    Sampling a point in space from a userdata Field List

    Cinema 4D SDK
    python
    2
    7
    1.2k
    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.
    • jenandesignJ
      jenandesign
      last edited by

      Hello! It seems that it should be possible to sample a Field List from userdata in a Python Tag, however I am having a hard time getting going since there aren't many examples in existence.

      What I would really like to be able to do is this (sorry for the semi-psuedocode):

      # get the field list from userdata on the python tag
      fieldList = op[c4d.ID_USERDATA,1]
      
      # sample the field at a specific point in global 3D space
      weight = fieldList.Sample(c4d.Vector(0, 0, 0))
      

      I know the above code does not work, but maybe it is helpful describing what I'm after.

      Is this somehow possible in a Python Tag?

      1 Reply Last reply Reply Quote 0
      • jenandesignJ
        jenandesign
        last edited by

        I found an example by @mikeudin that did the trick!

        Thanks so much for sharing!

        1 Reply Last reply Reply Quote 0
        • jenandesignJ
          jenandesign
          last edited by jenandesign

          my final working code(in a Python Tag, only mildly styled from mike's original):

          def FieldListSample(op, doc, positions, fields):
          
              cnt = len(positions)
              sampleFlags = c4d.FIELDSAMPLE_FLAG_VALUE | c4d.FIELDSAMPLE_FLAG_COLOR
          
              fieldInput = c4d.modules.mograph.FieldInput(positions, cnt)
              fieldInfo = c4d.modules.mograph.FieldInfo.Create(sampleFlags, None, doc, 0, 1, fieldInput, op)
              fieldOutput = c4d.modules.mograph.FieldOutput.Create(cnt, sampleFlags)
          
              fields.DirectInitSampling(fieldInfo)
              samplingSuccess = fields.DirectSample(fieldInput, fieldOutput.GetBlock(), fieldInfo)
              fields.DirectFreeSampling(fieldInfo)
          
              return fieldOutput
          
          
          # list of positions
          positions = [c4d.Vector(0, 0, 0)]
          
          # store the output (assumes that 'fields' is the FieldList)
          fieldOutput = def FieldListSample(op, doc, positions, fields)
          
          # grab the weights as a list from fieldOutput
          weights = [fieldOutput._value[x] for x in range(fieldOutput.GetCount())]
          
          

          cheers, thanks again to @mikeudin

          1 Reply Last reply Reply Quote 0
          • jenandesignJ
            jenandesign
            last edited by

            Extremely useful stuff if writing python generators / effectors or working with thinking particles in python.

            A feature request: adding some straight-to-the-point methods or a helper/abstraction class to streamline this in the future. Perhaps the dev team might even consider a few XPresso nodes for sampling Field Lists? Fields are so powerful and super exciting to use. Having a blast!

            ferdinandF 1 Reply Last reply Reply Quote 0
            • ferdinandF
              ferdinand @jenandesign
              last edited by

              Hey @jenandesign,

              Thank you for reaching out to us and I am glad that you found your solution.

              As an FYI for future readers, and just from looking at the code, Jena's last code listing does not seem to be valid Python grammar. The line fieldOutput = def FieldListSample(op, doc, positions, fields) is illegal syntax, you must remove the def.

              Cheers,
              Ferdinand

              MAXON SDK Specialist
              developers.maxon.net

              1 Reply Last reply Reply Quote 1
              • jenandesignJ
                jenandesign
                last edited by

                ooh good catch! I would edit that comment if I could. thanks @ferdinand !

                pasting again w/ the fix:

                def FieldListSample(op, doc, positions, fields):
                
                    cnt = len(positions)
                    sampleFlags = c4d.FIELDSAMPLE_FLAG_VALUE | c4d.FIELDSAMPLE_FLAG_COLOR
                
                    fieldInput = c4d.modules.mograph.FieldInput(positions, cnt)
                    fieldInfo = c4d.modules.mograph.FieldInfo.Create(sampleFlags, None, doc, 0, 1, fieldInput, op)
                    fieldOutput = c4d.modules.mograph.FieldOutput.Create(cnt, sampleFlags)
                
                    fields.DirectInitSampling(fieldInfo)
                    samplingSuccess = fields.DirectSample(fieldInput, fieldOutput.GetBlock(), fieldInfo)
                    fields.DirectFreeSampling(fieldInfo)
                
                    return fieldOutput
                
                
                # list of positions
                positions = [c4d.Vector(0, 0, 0)]
                
                # store the output (assumes that 'fields' is the FieldList)
                fieldOutput = FieldListSample(op, doc, positions, fields)
                
                # grab the weights as a list from fieldOutput
                weights = [fieldOutput._value[x] for x in range(fieldOutput.GetCount())]
                
                
                ferdinandF 1 Reply Last reply Reply Quote 0
                • ferdinandF
                  ferdinand @jenandesign
                  last edited by ferdinand

                  Hey @jenandesign,

                  you can edit your postings, just click on the three dots button below a posting of yours. You will not be able to move a post like I can, but except for that have the same options as I do:

                  46c3d9dd-ed4c-4fe4-a683-684eb5553f80-image.png

                  Cheers,
                  Ferdinand

                  MAXON SDK Specialist
                  developers.maxon.net

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