PDA

View Full Version : a not really effective way to parent


H3ro
04-03-2006, 09:34 AM
I have a script, which creates one cube per vertex. I want the cubes to be parented to the vertexes, but in maya this is not possible. There for I tryed an other way of making this. I know there are others and more efficent ways of doing this, but as I am still learning the whole mel thing i want to keep it simple:p

---
{
//got tierd of cleaning my scene:p
file -f -new;
CreatePolygonCube;
//convert the object selected into vertexes
ConvertSelectionToVertices;

//save the selection
string $sel[] = `ls -sl -flatten`;
string $vertInSelection;
vector $coords;

for ($current in $sel)
{

//find the position of the vertex
$coords = `xform -ws -q -t $current`;

//create a cube
string $cube[] = `polyCube -w 1
-h 1
-d 1
-sx 1
-sy 1
-sz 1
-ax 0 1 0
-tx 1
-ch 1`;

float $x = $coords.x;
float $y = $coords.y;
float $z = $coords.z;

//move the cube to the positon of the vertex
setAttr ($cube[0] + ".translateX") $x;
setAttr ($cube[0] + ".translateY") $y;
setAttr ($cube[0] + ".translateZ") $z;

//rename cube
$cube[0] = `rename $cube[0] "CubeOnObject1"`;

//create the expression
expression -s "cubeScript"
-o $sel
-ae 1
-uc all;
}

//write a proc to use as a script
global proc cubeScript ()
{
//position of the vertex
vector $vtxPos = `xform -ws -q -t $current`;

//assign the position of the vertex to the position of the cube, so it looks like the cubes are parented to the object
float $vtxX = $vtxPos.x;
float $vtxY = $vtxPos.y;
float $vtxZ = $vtxPos.z;

setAttr ($cube[0] + ".translateX") $vtxX;
setAttr ($cube[0] + ".translateY") $vtxY;
setAttr ($cube[0] + ".translateZ") $vtxZ;
}
}

---

the last part dont work, and I have no idea on how to fix it. Any help would be superb:)

thanks in advance

wrend
04-03-2006, 03:16 PM
your global proc doesnt know what $cube[0] is ...unless you declare the whole proc by string evaluation, then the $cube[0] string will be seen from the scope earlier in the evaluating script. ...so maybe you want your global proc to accept a string/name to work on:

global proc cubeScript ( string $object )
{
...
setAttr ($object + ".translateX") $vtxX;
...
}

so you'd also need to change the expression that calls the proc to pass in the name of $cube[0].

c.

H3ro
04-04-2006, 07:29 AM
Could you please explane it a bit more? Could not get it to work
And one other thing, I dont think the setAttr comand will be the right one to use, as I want the objectPos = vertexPos, what would the syntax be for that in this case?

Thanks

H3ro
04-04-2006, 08:29 AM
treyed to do like this:
expression -s ("vector $vtxPos = `xform -ws -q -t "+ $current +"`;\n
"+ $cube[0] +".translateX = $vtxPos+".translateX";")


-o $cube[0]
-ae 1
-uc all;

But it returns "undeterment string"

I al really stuck on this

Mikademius
04-04-2006, 11:07 AM
You could look at Bash' "cbDuplicateOnSelected" script.
http://www.christerb.com/downloads.html

wrend
04-05-2006, 03:45 AM
sorry, my explanatory powers arent that brilliant at the moment.

if you jigged the cubeScript proc to accept a string ...
bake the current object held in $cube[0] into the proc call ..
expression -s ("cubeScript " + $cube[0])

in your new one,
it would be
"+ $cube[0] +".translateX = $vtxPos.x;")
rather than
"+ $cube[0] +".translateX = $vtxPos+".translateX";")


c.

H3ro
04-06-2006, 08:27 PM
ok, i do admit it, I feel a bit stupid as I still cant figure this simple thing out! :(

this is what i have now:

----
{
file -f -new;
CreatePolygonCube;
//convert the object selected into vertexes
ConvertSelectionToVertices;

//save the selection
string $sel[] = `ls -sl -flatten`;
string $vertInSelection;
vector $coords;

for ($current in $sel)
{

//find the position of the vertex
$coords = `xform -ws -q -t $current`;

//create a cube
string $cube[] = `polyCube -w 1
-h 1
-d 1
-sx 1
-sy 1
-sz 1
-ax 0 1 0
-tx 1
-ch 1`;

float $x = $coords.x;
float $y = $coords.y;
float $z = $coords.z;

//move the cube to the positon of the vertex
setAttr ($cube[0] + ".translateX") $x;
setAttr ($cube[0] + ".translateY") $y;
setAttr ($cube[0] + ".translateZ") $z;

//rename cube
$cube[0] = `rename $cube[0] "CubeOnObject1"`;

expression -s ("cubeScript " + $cube[0])
-o ""
-ae 1
-uc all;
}


//write a proc to use as a script
global proc cubeScript (string $object)
{
//position of the vertex
vector $vtxPos = `xform -ws -q -t $current`;

//assign the position of the vertex to the position of the cube, so it looks like the cubes are parented to the object
float $vtxX = $vtxPos.x;
float $vtxY = $vtxPos.y;
float $vtxZ = $vtxPos.z;


$cube[0] +".translateX" = $vtxPos.x;
}
}

---

Any tips?

wrend
04-07-2006, 04:43 AM
bah, dont feel stupid ...you just gotta have a few of those 'ohhhh' moments that make the waters clearer.

your earlier problems are to do with how you call your scripts, and variable scopes. its usually cleaner to do your scripts as procs that you call at will (rather than by sourcing whole thing)..the script only runs/executes when you call a 'hook-in' proc in the script that gets things going. when you source a script file composed of procs, it updates all the procs in memory to the current/saved version on file, rather than actually running portions of the script itself (which your current setup does ... even if your arent sourcing a file, and just entering it as a block in the script editor). again, my expanations are pretty poor, w/o going into a multi-paragraph disertation.

examples best illustrate.

ive done a couple of minor edits to your current state. there was one major change that i'd missed however when i was throwing suggestions out ...the global proc needs to accept both the name of the object to be moved, and, the name of the vertices/points to be querried ..in the last itteration the cube object was being querried for its position, then being set to it ( dah! ;) ). i've added the alternatives of either havign the expressions call the outside proc to get position and set it, or have that little proc's contents encapsulated/baked into the expression it self.


global proc h3ro_cubePerVertSetup ()
{
file -f -new;
CreatePolygonCube;
//convert the object selected into vertexes
ConvertSelectionToVertices;
//save the selection
string $sel[] = `ls -sl -flatten`;
string $vertInSelection;
vector $coords;

for ($current in $sel)
{
//find the position of the vertex
$coords = `xform -ws -q -t $current`;

//create a cube
string $cube[] = `polyCube
-w 0.2
-h 0.2
-d 0.2
-sx 1
-sy 1
-sz 1
-ax 0 1 0
-tx 1
-ch 1`;

//move the cube to the positon of the vertex
setAttr ($cube[0] + ".translateX") ($coords.x);
setAttr ($cube[0] + ".translateY") ($coords.y);
setAttr ($cube[0] + ".translateZ") ($coords.z);

//rename cube
$cube[0] = `rename $cube[0] "CubeOnObject1"`;

expression -s ("h3ro_cubePerVertExpCallback " + $cube[0] + " " + $current);

/*
//baked into expression version:
expression -s (""
+" vector $vtxPos = `xform -ws -q -t " + $current +"`;"
+"" + $cube[0] + ".tx = $vtxPos.x;"
+"" + $cube[0] + ".ty = $vtxPos.y;"
+"" + $cube[0] + ".tz = $vtxPos.z;"
+"");
*/

}
}

//write a proc (for expression to call back)
global proc h3ro_cubePerVertExpCallback (string $vertCube, string $vert)
{
//position of the vertex
vector $vtxPos = `xform -ws -q -t $vert`;

setAttr ($vertCube +".translateX") ($vtxPos.x);
setAttr ($vertCube +".translateY") ($vtxPos.y);
setAttr ($vertCube +".translateZ") ($vtxPos.z);
}


so stick this into a file named h3ro_cubePerVertSetup.mel, (if you are running latter versions of maya, you will need to "rehash;" to register the new file in your script path),
then you can "source h3ro_cubePerVertSetup;",
and then call the setup proc, "h3ro_cubePerVertSetup;".

feel free to ask specific questions,

c.

H3ro
04-07-2006, 09:18 AM
Thank you so much:)

A few questions:

In "global proc h3ro_cubePerVertExpCallback (string $vertCube, string $vert)"
Where are the string $vertCube, string $vert defined? How does maya know what they are?
How can i make the script work if i just move the object in my scene, not using using the time slider? I have tryed adding the -ae falg, but it dont help.

Thanks:)

wrend
04-08-2006, 02:50 PM
your welcome (dig you scandinavic dudes) :)

re: Where are the string $vertCube, string $vert defined? How does maya know what they are?
- they are defined then-and-there in the procedure declaration of input arguments ... just like you declare them within a procedure.

re: How can i make the script work if i just move the object in my scene, not using using the time slider?
- you can force the expression to evaluate upon movement by pushing transform attributes through the expression, such that changes to them triggers the downstream expression node to evaluate. i go about this by hooking the shape node's worldMatrix to a decomposeMatrix node (need to load the node in 'plug-in manager' if your on defaults) and dumping a 'float $phoBar = decomposeMatrix.otx;' in the expression. this will not consider component/shape level manipulations though .. which you can mildly get around .. but ...if you want interactive level (as opposed to frame stepping) you should probably look at a totally node based solution, using nodes such as pointOnMeshInfo or curveFromMeshEdge ..which people have automated the setup of in various scripts such as 'rivet' or the like. i prefer such methods myself as they dont require an interpreted mel step, but this is largely dependent on the scale of the operations your doing. doing a search just now i found a freely distibuted script by this fellow: http://www.youneedthisman.com/misctools.htm, im sure there are others on http://www.highend3d.com also.

hope im making some sense,
again, feel free to ask questions,
cheers,
c.

H3ro
04-23-2006, 06:22 PM
Hi again:)

I started to play with the script again and I wanted to find a way to make new cubes on the object, if it got new vertexes (eg smooth)

I dont think I have to say this, but I failed at adding a function like that.
Any ideas on how to do this?

Ole Kristian

CGTalk Moderation
04-23-2006, 06:22 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.