PDA

View Full Version : Cant understand how my own script works =/


H3ro
11-12-2007, 12:51 PM
Hallo,

I am having huge problems with understanding how my script works
The way I wanted it to be is so that if an object is inside a circle, the rigidBody node get set to active. But for some reason it is always active.

If anyone have a few minutes extra, please take a look:

// Get the information needed to calculate if a point is inside
// the circle or not
float $circleRadius = nurbsCircle1.radius;
float $circlePos[2] = `xform -q -t -ws nurbsCircle1`;
float $objPos[2] = `xform -q -t -ws polySurface3`;

// Variable to check if the object has been activated or not
int $active;

// If we are at the startFrame, initialize the needed variables
if (frame == 1)
{
$active = 0;
rigidBody3.active = 0;
}

// Check if the object is inside the circle, if it is, turn the
// RigidBody on. Since the calculation is so expensive, we first check
// if the object already has been activated
if ($active == 0)
{
float $distanceX = pow(($objPos[0] - $circlePos[0]),2);
float $distanceZ = pow(($objPos[2] - $circlePos[2]),2);
if (pow(($distanceX + $distanceZ),1/2) <= $circleRadius )
{
// The object is inside the circle
rigidBody3.active = 1;
$active = 1;
}
else
{
rigidBody3.active = 0;
}
}



Regards,
Ole Kristian

Segmoria
11-12-2007, 01:08 PM
This is used as an expression, not in the script editor right?

Segmoria
11-12-2007, 02:16 PM
Ok I edited some stuff in your expression resulting in the following simpler approach:

float $circleRadius = `getAttr makeNurbCircle1.radius`;
float $circlePos[] = `xform -q -t -ws nurbsCircle1`;
float $objPos[] = `xform -q -t -ws polySurface3`;

float $distanceX = abs($objPos[0] - $circlePos[0]);
float $distanceZ = abs($objPos[2] - $circlePos[2]);

if ( ($distanceX <= $circleRadius) && ($distanceZ <= $circleRadius) )
{
rigidBody1.active = on;
}
else
{
rigidBody1.active = off;
}

This seems to work when put in as an expression to control the rigidBody1.active attribute though there seems to be a problem in Maya not refreshing its UI to reflect the true value of that attribute. If you scrub the timeline, it will force it to refresh and you see that the expression actually works.

I haven't figured out a way to correct this but I suggest you use a scriptJob with the -attributeChange flag that will monitor the translation of your rigidBody object and will enable turn the rigidBody1.active on as soon as it enters the circle.
If you want more help on this, let me know!

H3ro
11-12-2007, 02:53 PM
Thank you, your way is much better, as there is less calculation. I have a mel script that applies this expression to n objects, so speed is important.

For some reason I could not get "xform -t" to work, as it always returned 0 0 0, but changing it to -piv fixed that, not sure why though. And pow(x,1/2) is not the same as sqrt(x).

Just a quick question, how can I add a set prefix to the name of objects? I have it almost working, but are struggling with the last line here:

{
// -------------------------------------------------
// Create controls for the script
// -------------------------------------------------
string $preFix = "OK_"; // Add a uniqe prefix to avaoid name problems
int $nCircles = 3; // Controls how many circles to make
int $collisions = 0; // Bool variable for turning on and off collision
int $nCollisionLayers = 1; // Bool variable for adding each rBody to a new cLayer


// -------------------------------------------------
// Create the interactive controls
// -------------------------------------------------

// Create n circles, each one unit apart in the y-axis
for($i = 0; $i< $nCircles; $i++)
{
circle -c 0 $i 0 -nr 0 1 0 -sw 360 -r 1 -d 3 -ut 0 -tol 0.01 -s 8 -ch 1;
rename ($preFix + ("controlCircle_" + $i));

// Add a radius attribute to easy both animation and scripting
addAttr -ln "radius" -at double ($preFix + ("controlCircle_"+$i+""));
setAttr -e -keyable true (""+$preFix+"controlCircle_"+$i+"".radius);
}
}


Thank you again Segmoria, appreciate it very much :)

Segmoria
11-13-2007, 12:21 AM
And pow(x,1/2) is not the same as sqrt(x).
print `pow 2 0.5`;
print `sqrt(2)`;

Both of these give out the same result, so I would say they are the same.

Now as far as the above code snippet is concerned, what is the following line supposed to achieve? The attribute you just added is keyable by default, I dont see why you have to use this.
setAttr -e -keyable true (""+$preFix+"controlCircle_"+$i+"".radius);

The following seems to execute fine for me.
{{
// -------------------------------------------------
// Create controls for the script
// -------------------------------------------------
string $preFix = "OK_"; // Add a uniqe prefix to avaoid name problems
int $nCircles = 3; // Controls how many circles to make
int $collisions = 0; // Bool variable for turning on and off collision
int $nCollisionLayers = 1; // Bool variable for adding each rBody to a new cLayer

// -------------------------------------------------
// Create the interactive controls
// -------------------------------------------------

// Create n circles, each one unit apart in the y-axis
for($i = 0; $i< $nCircles; $i++)
{
circle -c 0 $i 0 -nr 0 1 0 -sw 360 -r 1 -d 3 -ut 0 -tol 0.01 -s 8 -ch 1 -name ($preFix + ("controlCircle_" + $i));
// Add a radius attribute to easy both animation and scripting
addAttr -ln "radius" -at double ($preFix + ("controlCircle_"+$i+""));
}}}

H3ro
11-13-2007, 12:16 PM
If you do not add the "-keyable true", the attribute sometimes acts like it is hidden, so you cant access it from the channel control.

The correct syntax is:
setAttr -e -keyable true (""+$preFix+"controlCircle_"+$i+".radius");

I have an other question, its not really a problem but more out of curiosity:
What is the difference between the following commands?

xform -q -t -ws
xform -q -t
xform -q -rp -ws

The two first ones seems to always return the local transformation of the object, while the last gives the world space. I thought the first should return the world position as well?

Segmoria
11-13-2007, 01:00 PM
xform -q -t -ws
Queries the translation values of an object in world space
xform -q -t
Queries the translation values of an object in object space
xform -q -rp -ws
Queries the coordinates of the rotational pivot in world space

To understand this better, take an object, change its pivot and parent it to another one, then run all the above commands together to see the different results.

CGTalk Moderation
11-13-2007, 01:00 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.