[Closed] self reference script controller
hi :
a problem need help :
for a object , set the position controller to position list . then add a script controller to available .
when i edit the script controller , i need reference the object itself . then i add a variable ,i set the name “thisobj”, then assign thisobj to the object itself . after run it . max popup a error message : illegal self-reference in controller script.
for in the script controller , we can use ” this ” to reference this controller . but how to get the object itself ? do we have to use the $theobject to reference itself .
im finding if there exist a command like “baseobject” or “thisobject” or “upper controller >>uppercontroller >>…>> base node ” … to help me get this object at once .
to set a custom attribute and make a node type parameter ,point to itself. but in this way , it looks a little complex for doing this simple work .
thanks all for reply .
the problem you got is because you enter in a dependecy loop. Normally you get dependecpy looops in rigging with look at, hierarchy…
let me explain sample:
pos =5
pos = pos+5 ,so if you plus 5 to pos it will be 10, but because pos is still on the script in will recalculate and add 5 again. so it wil be adding 5 all the time , without stoping.
the solution is easy use a another object as reference , a helper so it not referencing himself and it will do the job.
(refs.dependents this)[3].name gets the name of the object which this script controller is applied.
It works but I don’t know if it’s dangerous in any way.
Create Variable > Create “myVar”, Assign Node mySceneObject, and you can access to the object inside the script controller.
But as luigi said, you cannot establish a new position to this object based on mySceneObject.position, (dependecy loop).
Fernando
The reason you can’t get the object the script controller is assigned to is because there could be instances of the script controller on many objects.
You also run the risk of calling a function that requires your script controller to be evaluated, which would get you stuck in an infinite circle of function calls.
This sounds like a bad design for your script controller. What is it your trying to do, there is always a better approach.
Assign the node a CA with a paramBlock2 attribute maxObject or a maxObject tab like so:
testCA = attributes Attributes
(
parameters testParams
(
reference type:#maxobject
)
)
custAttributes.add $ testCA
$.reference = (nodeTransformMonitor node:$ forwardTransformChangeMsgs:false)
Give the object a position list controller, and a script_controller and you can reference itself:
p0 = $.reference.node.*attribute.*parameter
Mustan9,
Instancing script_controllers such as instancing a struct is good for a variable that needs to be redefined whilst affecting a function inside the struct.
Weak referencing, uses a callback method to only get the definition or attribute when the object has changed, it doesnt constantly look for itself, like using the this.method. Weak referencing is very powerful in speeding up rigs, and the ability for bi-directional constraints.
So eek, the CA stuff and:
(nodeTransformMonitor node:$ forwardTransformChangeMsgs:false)
is better than:
myController.AddNode “myNode” $
after the creation of the script controller in a script?
Thanks
Fernando
The add node method is for script handling of script_controllers, Node is a name non-dependant reference to an object. Its not a weak reference and so is always being called when the script is evaluated.
I have a tut on my site about setting up weak referencing if you are interested.
Also if the person still wants to get the position value in the positions script I think that you are still going to get a dependancy loop. If all you want is one of the parameters then just add that parameter to the script controller. That should work without a problem.
thanks eek and PEN,
A similar script controller will be repeated for a lot of objetcts in the scene. So, I’m looking for the more efficient way.
I need the radius (added as an attribute) and the position of the scene node which get this script controller applied.
I understand that the best way to know both inside this controller is to create an attribute (to the controller) with a weak dependency (to the scene node).
As eek shows here, I can do it.
But what do I have to write inside the controller.script ?