1. skurmedel
  2. mayascripts

Commits

Skur...@Rommel  committed 93caabf

Added some scripts.

  • Participants
  • Branches default

Comments (0)

Files changed (3)

File various/SO_insertGammaCorrect.mel

View file
+//
+// Simon Otter 2012.
+// -----------------
+// Do what the fuck you want with this. I place this script in the public domain where 
+// applicable, if not, the first sentence applies.
+//
+// You can sell it, roll it, smoke it, deep fry it, wallpaper your room with it, print it
+// out and fold it into a cute little hat, use it on the loo, etc.
+//
+
+//
+// "SO_insertGammaCorrect" does the actual work.
+// Call "SO_insertGammaCorrectTool" to get an UI.
+//
+
+//
+// INSERTGAMMACORRECT COMMAND.
+//
+
+proc string[] SO_listAttributesByType(
+	string $node,
+	string $type)
+{
+	string $res[];
+
+	for ($a in `listAttr -w -c $node`)
+    {
+    	string $t = "";
+    	if (catchQuiet($t = `getAttr -type ($node + "." + $a)`))
+    		continue;
+    	if ($t == $type)
+        	$res[size($res)] = $a;
+    }
+
+    return $res;
+}
+
+proc string[] SO_listMaterials()
+{
+	return `ls -mat`;
+}
+
+global proc SO_insertGammaCorrect(string $plug, float $gamma)
+{
+	string $node = plugNode($plug);
+	string $attr = plugAttr($plug);
+
+	if (!attributeExists($attr, $node))
+	{
+		error("Unknown attribute: " + $plug);
+	}
+
+	if (`connectionInfo -isDestination $plug`)
+	{
+		error("Attribute already connected to: " + `connectionInfo -sfd $plug`);
+	}
+
+	if (`getAttr -type $plug` != "float3")
+		error("The destination attribute needs to be of type float3.");
+
+	string $gcNode = `createNode gammaCorrect`;
+	setAttr -type "float3" ($gcNode + ".gamma") $gamma $gamma $gamma;
+
+	vector $v = `getAttr $plug`;
+	setAttr -type "float3" ($gcNode + ".value") ($v.x) ($v.y) ($v.z);
+
+	connectAttr ($gcNode + ".outValue") $plug;
+}
+
+//
+// UI STUFF.
+//
+
+proc SO_updateAttributeList(string $v)
+{
+	menu -e -deleteAllItems "SO_insertGammaCorrectTool_al";
+	for ($a in SO_listAttributesByType($v, "float3"))
+	{
+		menuItem -p "SO_insertGammaCorrectTool_al" -label $a;
+	}
+}
+
+proc SO_createMaterialsMenu()
+{
+	optionMenu -label "Materials" -changeCommand "SO_updateAttributeList(\"#1\")" "SO_insertGammaCorrectTool_ml";
+	for ($m in SO_listMaterials())
+	{
+		menuItem -label ($m);
+	}
+}
+
+proc float SO_roundGamma(float $v)
+{
+	// Eliminate all but one decimal.
+    return ceil($v * 10.0) / 10.0;
+}
+
+proc SO_updateGammaText(float $v)
+{
+	text -edit -label (SO_roundGamma($v)) "SO_insertGammaCorrectTool_gtx";
+}
+
+proc SO_insertGammaCorrectTool_doAction()
+{
+	string $n = `optionMenu -q -value "SO_insertGammaCorrectTool_ml"`;
+	string $a = `optionMenu -q -value "SO_insertGammaCorrectTool_al"`;
+
+	float $g = SO_roundGamma(`floatSlider -q -value "SO_insertGammaCorrectTool_gv"`);
+
+	// Gamma is entered as the "gamma to correct for", but the gamma correct node
+	// has the inverse relationship.
+	SO_insertGammaCorrect($n + "." + $a, 1.0 / $g);
+}
+
+global proc SO_insertGammaCorrectTool()
+{
+	string $windowName = "SO_insertGammaCorrectTool_w";
+
+	if (`window -ex $windowName`)
+	{
+		deleteUI $windowName;
+	}
+
+	window -title "Gamma Correct Colour" $windowName;
+
+	columnLayout -adj 1;
+
+		SO_createMaterialsMenu();
+
+		optionMenu -label "Attribute" "SO_insertGammaCorrectTool_al";
+
+		separator;
+
+		rowLayout -adj 2 -nc 3;
+			text -label "Source Gamma";
+			floatSlider -min 0.1 -max 3.0 -v 2.2 -s 0.1 -dragCommand "SO_updateGammaText(#1)" "SO_insertGammaCorrectTool_gv";
+			text -label "2.2" "SO_insertGammaCorrectTool_gtx";
+
+		setParent ..;
+
+		button -label "Create" -command "SO_insertGammaCorrectTool_doAction()";
+
+	showWindow $windowName;
+
+	window -edit -width 250 -height 1 $windowName;
+}

File various/epicSpiral.mel

View file
+// Simon Otter DG11a (2012)
+
+proc vector rotateThree(vector $p, vector $angles)
+{
+	$p = `rot $p <<1, 0, 0>> ($angles.x)`;
+	$p = `rot $p <<0, 1, 0>> ($angles.y)`;
+	$p = `rot $p <<0, 0, 1>> ($angles.z)`;
+	return $p;
+}
+
+// Skapar en episk spiral längs Z-axeln.
+//   
+// 	$c			antalet koner, bestämmer även spiralens sträcka.
+//	$radius		spiralens radie.
+//  $curveN		en kurva spiralen följer.
+//  $revs		hur många varv spiralen skall ha.
+//
+// Exempel:
+//  source "epicSpiral";
+//  epicSpiral(250, 5, "curveShape1", 15);
+global proc epicSpiral(int $c, float $radius, string $curveN, float $revs) 
+{
+	float $fullCircle = 3.142 * 2;
+
+	if ($revs <= 0.0)
+	{
+		print "revs is less or equal to zero, defaulting to 5.0";
+		$revs = 5.0;
+	}
+
+	if ($radius <= 0.0)
+	{
+		print "radius must be bigger than zero.";
+	} 
+	else if (!`objExists $curveN`)
+	{
+		print "No curve with that name exists.";
+	} 
+	else if ($c <= 0)
+	{
+		print "c must be bigger than zero.";
+	}
+	else 
+	{
+		string $n = "";
+		vector $curr = <<0.0, 0.0, 0.0>>;
+		vector $next = <<0.0, 0.0, 0.0>>;
+
+		float $coneSize = 1.0;
+		float $stepSize = 1.0 / $c;
+
+		for ($i = 0.0; $i <= 1.0; $i += $stepSize)	
+		{
+			vector $cvPos = `pointOnCurve -top true -pr $i -p ($curveN)`;
+			// Vi behöver de här för att beräkna axlarna på vår rotationsmatris.
+			// De är alla i worldspace.
+			vector $cvTangent = `pointOnCurve -top true -pr $i -nt ($curveN)`;
+			vector $cvNormal = `pointOnCurve -top true -pr $i -nn ($curveN)`;
+			vector $cvBinormal = `cross $cvTangent $cvNormal`;
+
+			// Bygg vår rotationsmatris.
+			matrix $m[3][3] = <<
+				$cvNormal.x,   $cvNormal.y,   $cvNormal.z;
+				$cvBinormal.x, $cvBinormal.y, $cvBinormal.z;
+				$cvTangent.x,  $cvTangent.y,  $cvTangent.z>>;
+
+			float $angP = $revs * ($i * $fullCircle);
+			
+			// Bygg våran spiral i X/Y-planet, men...
+			vector $offset = <<cos($angP) * $radius, sin($angP) * $radius, 0>>;
+			// rotera helvetet i rätt riktning.
+			$offset = $offset * $m;
+
+			$curr = $next;
+			$next = $cvPos + $offset;
+
+			// Pekar en kon till nästa som skall byggas.
+			$axis = $next - $curr;
+
+			string $n[] = `polyCone -ax ($axis.x) ($axis.y) ($axis.z) -r ($coneSize/2) -h $coneSize`;
+
+			// Flytta till rätt världsposition.
+			move ($curr.x) ($curr.y) ($curr.z) $n[0];
+		}
+
+		print ("Created " + $c + " epic cones!");
+	}
+}

File various/mengerSponge.mel

View file
+// Köttar upp en kub till 3^3 - 7 (20) mindre kuber som fyller samma utrymme.
+//
+// $extents		två vektorer som definerar ett hörn och nästa hörn diagonalt 
+//				till det i world space.
+//
+// returnerar   en platt array med min och max för varje kub, 20 * 2 element stor.
+proc vector[] divideCube(vector $min, vector $max)
+{
+	vector $diagonal = $max - $min;
+	
+	vector $incr = $diagonal * (1.0 / 3.0);
+
+    vector $cubes[40];
+    int $i = 0;
+	for ($y = 0; $y < 3; $y++)
+	{
+		for ($x = 0; $x < 3; $x++)
+		{
+			for ($z = 0; $z < 3; $z++)
+			{
+                $finalPos = $min + <<$x * $incr.x, $y * $incr.y, $z * $incr.z>>;
+
+				if (($y == 0 || $y == 2) && $x == 1 && $z == 1) { /* do nothing */ }
+				else if ($y == 1 && ($x == 1 || $z == 1)) { /* zzz */ }
+				else
+				{
+				    $cubes[$i++] = $finalPos;
+				    $cubes[$i++] = $finalPos + $incr;
+				}
+			}
+		}
+	}
+
+	return $cubes;
+}
+
+// Skapar en Menger-Sponge.
+proc createSponge(vector $min, vector $max, int $iterations) 
+{
+    vector $old[] = { $min, $max };
+    vector $new[];
+    
+    for ($i = 0; $i < $iterations; $i++)
+    {
+        for ($j = 0; $j < size($old); $j+=2)
+        {
+            vector $res[] = divideCube($old[$j], $old[$j + 1]);
+            for ($v in $res)
+            {
+                $new[size($new)] = $v;
+            }
+        }
+        $old = $new;
+        $new = {};
+    }
+    
+    string $old_n = "";
+    for ($i = 0; $i < size($old); $i+=2)
+    {
+        vector $min = $old[$i];
+        vector $diag = $old[$i + 1] - $min;
+        float $s = $diag.x;
+
+        string $n[] = `polyCube -w $s -h $s -d $s`;
+        move ($min.x) ($min.y) ($min.z) $n[0];
+
+        if ($old_n != "")
+        {
+            polyCombine -ch 0 $old_n $[0];
+        }
+    }
+}
+
+createSponge(<<0,0,0>>, <<10,10,10>>, 3);