make chain script

Become a member of the CGSociety

Connect, Share, and Learn with our Large Growing CG Art Community. It's Free!

THREAD CLOSED
 
Thread Tools Search this Thread Display Modes
Old 05 May 2013   #1
make chain script

so i need to write a script which creates a window and makes a chain, but im having trouble getting my xform lines to work! i want it to move each chain the radius+girth of the link so it sits in the right place and rotate it 90degrees but im not having much luck. any help would be awesome thanks!

global proc cc(string $name, float $radius, float $chainGirth, int $chainLength)

{
string $chainLink[] = `polyTorus -r $radius -sr $chainGirth -tw 0 -sx 20 -sy 20 -ax 0 1 0 -cuv 1 -ch 1 -n $name`;

for ($i=0;$i<$chainLength;$i++)
{

float $distBetween = (($radius+$chainGirth)*($i - 1));
polyTorus -r $radius -sr $chainGirth -tw 0 -sx 20 -sy 20 -ax 0 1 0 -cuv 1 -ch 1 -n $name;
xform -ro 90 0 0 $name[0];
xform -t ($distBetween) 0 0 $name[0]; // changed that from -r
}

}
 
Old 05 May 2013   #2
Hi,

Did you expect something like that?
global proc cc(string $name, float $radius, float $chainGirth, int $chainLength)
{
	float $offset = 0;//this variable will keep each torus' offset and add it to the next one
	for ($i = 0 ; $i < $chainLength ; $i++)
	{
		$myTorus = ($name + $i);//changes the name
		polyTorus -r $radius -sr $chainGirth -tw 0 -sx 20 -sy 20 -ax 0 1 0 -cuv 1 -ch 1 -n $myTorus;
		if ($i)//we don't want to change anything on the first torus
		{
			xform -t (($radius*2) - ($chainGirth*2) + $offset) 0 0 $myTorus;
			$offset += (($radius*2) - ($chainGirth*2));//adds offset

			if ($i%2)//applies rotation if $i is odd
				xform -ro 90 0 0 $myTorus;
		}
	}
}
 
Old 05 May 2013   #3
Hope this simplifies it for you:

import pymel.core as pm

def makeChain(name, radius, girth, lenght):
    
    for i in range(0,lenght):
        obj = pm.polyTorus(radius = radius, sr = girth, tw= 0, sx = 20, sy = 20, ax = (0,1,0), ch = 1, n = name)    
        if i % 2 == 1:
            pm.rotate(0, 90, 0)
        distance = 2*(radius - girth)*i
        
        pm.move(distance,0,0)
makeChain("test", 1, 0.1, 10)
__________________
www.nealbuerger.com
 
Old 05 May 2013   #4
You're right, borgFriend, the way to calculate offset is much nicer like you did. Let's re-write it in MEL (I've added a random input, just in case...)
global proc cc(string $name, float $radius, float $chainGirth, int $chainLength, float $random)
{
	for ($i = 0 ; $i < $chainLength ; $i++)
	{
		$myTorus = ($name + $i);//changes the name
		polyTorus -r $radius -sr $chainGirth -tw 0 -sx 20 -sy 20 -ax 0 1 0 -cuv 1 -ch 1 -n $myTorus;
		xform -t ((($radius - $chainGirth)*2) * $i) 0 0 $myTorus;

		if ($i%2)//applies rotation if $i is odd
			xform -ro 90 0 0 $myTorus;
		
		//adds a little bit of random...
		float $randomRot = `rand (-$random) $random`;
		xform -r -ro $randomRot 0 0 $myTorus;
	}
}
cc("test",10,2,8,20);
 
Old 05 May 2013   #5
Thumbs up

thanks u guys!! works a treat! I've gotta rig it too so i'll have a go at that.. thanks for the help!
 
Old 05 May 2013   #6
Well, I played around with it some more, and added rig support:
It takes a curve as input and creates a chain.

Full code around 150 lines:
http://nealbuerger.com/2013/05/auto...ks-along-curve/
__________________
www.nealbuerger.com

Last edited by Borgfriend : 05 May 2013 at 03:50 PM.
 
Old 05 May 2013   #7
Thread automatically closed

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.
__________________
CGTalk Policy/Legalities
Note that as CGTalk Members, you agree to the terms and conditions of using this website.
 
Thread Closed share thread



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
CGSociety
Society of Digital Artists
www.cgsociety.org

Powered by vBulletin
Copyright 2000 - 2006,
Jelsoft Enterprises Ltd.
Minimize Ads
Forum Jump
Miscellaneous

All times are GMT. The time now is 04:46 AM.


Powered by vBulletin
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.