Sampling a point in space from a userdata Field List
-
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?
-
I found an example by @mikeudin that did the trick!
Thanks so much for sharing!
-
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
-
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!
-
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 thedef
.Cheers,
Ferdinand -
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())]
-
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:
Cheers,
Ferdinand