Coding a Python Field
darrellp last edited by m_adam
I've coded in C#/C++ since both those respective languages started but haven't programmed much in Python though I don't think that's a serious problem. I'm trying to find the sample positions local to the field object in the Sample routine for a Python field. The sample code provided when you create a python field varies based on time and is totally agnostic on positions so doesn't really offer any insight. It appears to me that the values in the Sample function's inputs parameter for inputs._position are all global positions. I thought that I'd be able to multiply them by inputs._transform but that seems to always be an identity matrix no matter whether I move the object I'm dealing with or the field so I guess I must really not understand it's purpose. It says in the documentation "The transform matrix for the points in the input block. Use to bring the position and direction lists into world space or the local space of a FieldObject." but I don't see how this happens if it stays as an identity matrix. I'm enclosing a file which has what I'm working on. It has a sphere with a red material applied to a face selection set determined by the Python field. The code in the field just specifies 1 if the input point has a z value < 0 and 0 otherwise. This always colors red based on the world origin showing that the inputs are world coordinates. I have printed out inputs._transform to verify it's the identity matrix. I can't seem to find any examples of a Python Field anywhere on the internet except the default one produced initially which, as noted above, is no help. Can somebody help me out here? Thanks in advance!
Hi @darrellp, first of all, welcome in the plugincafe community!
No worries at all since it's your first post, but please make sure to read and apply the rules defined on the following topics:
With that's said regarding your issue, the array _transformation is used to know the current transformation matrix of the current 3D point (for example a previous field can define a direction, and you would like to know this transformation).
Since you want to do something in the local space of the Python FieldObject. You firstly have to retrieve the FieldObject, which is defined in op variable. Then as you already figured the point in _positions array are stored in global positions.
So you simply have to write.
localpos = inputs._position[i] * ~op.GetMg()
localpos = inputs._transform.Mul(inputs._position[i])
In order to retrieve points in local space of the FieldObject.
If you have any others question, please let me know!
darrellp last edited by darrellp
@m_adam Yes - thanks. I actually got it working after posting. I'm not sure what kind of rule infraction you seem to be referring to here. I'd really like to comply with regulations but I'm not sure what I did wrong. I read the two articles you referenced and didn't see anything I might have done wrong. Maybe you're just suggesting that after my first post? In any event, if I'm doing something wrong please let me know and I'll try to take care of it. Thanks again!
darrellp last edited by
@m_adam Oh - okay - I see you moved it. I thought that was the mistake at first but then saw it was in "Coding & Development" and assumed I put it there even though I didn't remember it that way. I was a little confused by the two different categories. Got it now.
Hey @darrellp as you figured out, it was not on the correct category. But in any case don't worry, maybe I was too harsh and unprecise, I'm sorry about that but in the end, as I said, you are new here, so don't worry if you break the posting rules the first times :)