Project cursor

Wednesday, March 17th, 2010

Downloadable Files

This script places the cursor at the intersection of the vertex normals of selected vertexes.


Requirements

This script is written for blender 2.45 but should work with older versions as well


Usage

There are no settings for the script.

Simply select the required verts in your mesh and run the script.

Here’s an example of what I use the script for

Here we have part of a uvsphere and neither the object centre nor the cursor are where the centre of the full sphere would be.

Now say that you wanted to move or resize this mesh as if it was a full sphere.

You’d normally put the cursor at the centre of the sphere, but this won’t work here.

So we select a bunch of verts and run the script… see where the cursor ends up?

Now the cursor is where it would be if we set it to the centre if the sphere were full!

Remember that it uses vertex normals… so in this case… you don’t want to select the outer edge of the mesh… cause we really don’t know where those vert normals point.


That’s all

Nothing much to it… but usefull. Enjoy…
Dolf

Pivot constraint

Wednesday, March 17th, 2010

Downloadable Files

This script helps you to use a bone in an armature as an animated pivot point.

It can be very helpfull in making things like a heel/toe rig for a foot.


Requirements

This script is written for blender versions newer than 2.45


Setting up your rig for the script

You will basicly need 2 bones for the script to work.

1. the bone that the script is assigned to (this bone is affected by the script)

2. the pivot bone that manipulates bone nr 1.

You want both bones to be in exactly the same location/rotation in edit mode when you create them.

In the example file I named the pivot bone pivot and the bone that the script is assigned to foot.

I also made a parent bone for the both of them called location, that way you can move them together, but it’s not nessecary for the script to work.


Assigning the script

You have to have the script open in blender to be able to assign it.

So open it in a text editor window.

Now you can select the bone you want to assign the script to andOadd a script constraint.

The script should be available in the script dropdown box.

Set the spaces to local space and assign the pivot bone as the target.


Options

If you click on Options you will get a small popup with one setting.

If you have Only location selected the rotation of the bone that the script is assigned to isn’t affected by the script.

Normally you want the location and rotation both to be affected, but I thought it might be nice to have the option.

I didn’t add an Only rotation option because then it would just be a copy rotation constraint, and we already have one of those.


Animating with the pivot setup

The idea is that now you have your pivot bone, you don’t need to animate/rotate/move your foot bone anymore. All you use is the pivot bone, and/or the parent(s) of the foot/pivot bones.

For a step simpy think like this… move the location bone (the parent) to where the ground is, then move the pivot to the point the foot rotates around… then rotate the pivot to the angle you need… voila.

If you’re not carefull you can make some really strange things happen, but I purposely left everything simple (script wise). This way it’s up to you to make it usefull ;)


That’s all

It can be a bit tricky to get the hang of this functionality, but I’m sure it’ll prove usefull to some.

enjoy
Dolf

Shorten text

Wednesday, March 17th, 2010

This is a small function that shortens a string.
It looks if the string is longer than a preset length, and if so cuts the text there.
Then it removes the last word from the string (so your string ends neatly at a space), and adds three dots.

  1. // Shorten a text if nessecary
  2. function shortenText($txt='', $len=0){
  3.  
  4.         if(strlen($txt) > $len){
  5.  
  6.                 $txt = substr($txt, 0, $len);
  7.  
  8.                 $t = split("[\n\r\t ]+", $txt);
  9.                 $t = array_slice($t, 0, -1);
  10.                 $txt = join(' ', $t);
  11.  
  12.                 $txt .= '...';
  13.  
  14.         }
  15.  
  16.         return $txt;
  17. }

Display clean php code for copying

// Shorten a text if nessecary
function shortenText($txt='', $len=0){

	if(strlen($txt) > $len){

		$txt = substr($txt, 0, $len);

		$t = split("[\n\r\t ]+", $txt);
		$t = array_slice($t, 0, -1);
		$txt = join(' ', $t);

		$txt .= '...';

	}

	return $txt;
}

Rot13

Wednesday, March 17th, 2010

Basic rot13 function for PHP (remember that this is NOT secure encryption!).
It’s built into php nowadays, but if you have an old version you may not.

  1. // Rot 13 function for older php versions
  2. function rot13($text){
  3.         $length = strlen($text);
  4.         $newtext = '';
  5.          for($i = 0; $i < $length; $i++){
  6.                  $index = ord($text[$i]);
  7.                  // Rotate upper case.
  8.                  if($index > 64 && $index < 91){
  9.                          $index += 13;
  10.                          if($index > 90) $index -= 26;
  11.                          $newtext .= chr($index);
  12.                 // Rotate lower case.
  13.                 }elseif($index > 96 && $index < 123){
  14.                         $index += 13;
  15.                         if($index > 122) $index -= 26;
  16.                         $newtext .= chr($index);
  17.                  }else{ $newtext .= $text[$i]; }
  18.          }
  19.          return $newtext;
  20. }

Display clean php code for copying

// Rot 13 function for older php versions
function rot13($text){
	$length = strlen($text);
	$newtext = '';
	 for($i = 0; $i < $length; $i++){
		 $index = ord($text[$i]);
		 // Rotate upper case.
		 if($index > 64 && $index < 91){
			 $index += 13;
			 if($index > 90) $index -= 26;
			 $newtext .= chr($index);
		// Rotate lower case.
		}elseif($index > 96 && $index < 123){
			$index += 13;
			if($index > 122) $index -= 26;
			$newtext .= chr($index);
		 }else{ $newtext .= $text[$i]; }
	 }
	 return $newtext;
}

Make a random string

Wednesday, March 17th, 2010

This is a tiny function that makes a random string of predefined length.
It does not use letters and numbers that could be confusing, like O and 0.

  1. // Make a random string
  2. function makeRandomString($length){
  3.         $str = array('a', 'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'W', 'X', 'Y', 'Z', '2', '4', '5', '6', '7', '8', '9');
  4.         $nkeys = array_rand($str, $length);
  5.         $nstr = '';
  6.         foreach($nkeys as $key){ $nstr .= $str[$key]; }
  7.         return $nstr;
  8. }

Display clean php code for copying

// Make a random string
function makeRandomString($length){
	$str = array('a', 'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'W', 'X', 'Y', 'Z', '2', '4', '5', '6', '7', '8', '9');
	$nkeys = array_rand($str, $length);
	$nstr = '';
	foreach($nkeys as $key){ $nstr .= $str[$key]; }
	return $nstr;
}

Make image version

Wednesday, March 17th, 2010

This is a function I use to make multiple versions such as thumbnails of uploaded images.
There’s three methods for resizing available in it (mask, fit & limit).

  1. // Function for making a new resized copy of a file
  2. // mask makes the shortest edge match the mask, so it will be bigger than the mask, always resizes!
  3. // fit makes the longest edge match, always resizes!
  4. // limit makes the image fit, but won't stretch if it's smaller.
  5. function makeImageVersion($newWidth, $newHeight, $filePath, $newName, $fileExtension, $method){
  6.  
  7.         // Find out the original size of the image
  8.         list($wO, $hO) = getimagesize($filePath);
  9.  
  10.         // Get the ratios it would take to resize
  11.         $wR = $newWidth / $wO;
  12.         $hR = $newHeight / $hO;
  13.  
  14.         // Figure out which ratio we need
  15.         switch($method){
  16.                 case 'mask':
  17.                         if($wR > $hR){ $ratio = $wR; }
  18.                         else{ $ratio = $hR; }
  19.                         break;
  20.                 case 'limit':
  21.                         if($wR < 1 && $wR < $hR){ $ratio = $wR; }
  22.                         elseif($hR < 1){ $ratio = $hR; }
  23.                         else{ $ratio = 1; }
  24.                         break;
  25.                 case 'fit':
  26.                 default:
  27.                         if($wR > $hR){ $ratio = $hR; }
  28.                         else{ $ratio = $wR; }
  29.                         break;
  30.         }
  31.  
  32.         $wN = round($wO * $ratio);
  33.         $hN = round($hO * $ratio);
  34.  
  35.         $iP = imagecreatetruecolor($wN, $hN);
  36.         imageAlphaBlending($iP, false);
  37.         imageSaveAlpha($iP, true);
  38.  
  39.         if($fileExtension == 'jpg'){ $iI = imagecreatefromjpeg($filePath); }
  40.         elseif($fileExtension == 'png'){        $iI = imagecreatefrompng($filePath); }
  41.         else{ $iI = imagecreatefromgif($filePath); }
  42.  
  43.         imagecopyresampled($iP, $iI, 0, 0, 0, 0, $wN, $hN, $wO, $hO);
  44.  
  45.         if($fileExtension == 'jpg'){ imagejpeg($iP, $newName, 90); }
  46.         elseif($fileExtension == 'png'){ imagepng($iP, $newName); }
  47.         else{ imagegif($iP, $newName); }
  48. }

Display clean php code for copying

// Function for making a new resized copy of a file
// mask makes the shortest edge match the mask, so it will be bigger than the mask, always resizes!
// fit makes the longest edge match, always resizes!
// limit makes the image fit, but won't stretch if it's smaller.
function makeImageVersion($newWidth, $newHeight, $filePath, $newName, $fileExtension, $method){

	// Find out the original size of the image
	list($wO, $hO) = getimagesize($filePath);

	// Get the ratios it would take to resize
	$wR = $newWidth / $wO;
	$hR = $newHeight / $hO;

	// Figure out which ratio we need
	switch($method){
		case 'mask':
			if($wR > $hR){ $ratio = $wR; }
			else{ $ratio = $hR; }
			break;
		case 'limit':
			if($wR < 1 && $wR < $hR){ $ratio = $wR; }
			elseif($hR < 1){ $ratio = $hR; }
			else{ $ratio = 1; }
			break;
		case 'fit':
		default:
			if($wR > $hR){ $ratio = $hR; }
			else{ $ratio = $wR; }
			break;
	}

	$wN = round($wO * $ratio);
	$hN = round($hO * $ratio);

	$iP = imagecreatetruecolor($wN, $hN);
	imageAlphaBlending($iP, false);
	imageSaveAlpha($iP, true);

	if($fileExtension == 'jpg'){ $iI = imagecreatefromjpeg($filePath); }
	elseif($fileExtension == 'png'){	$iI = imagecreatefrompng($filePath); }
	else{ $iI = imagecreatefromgif($filePath); }

	imagecopyresampled($iP, $iI, 0, 0, 0, 0, $wN, $hN, $wO, $hO);

	if($fileExtension == 'jpg'){ imagejpeg($iP, $newName, 90); }
	elseif($fileExtension == 'png'){ imagepng($iP, $newName); }
	else{ imagegif($iP, $newName); }
}

File put contents

Wednesday, March 17th, 2010

file_put_contents() is in php nowadays, but if you’re on an old server you may need your own.
This simply checks if the function’s there, and if not it makes the function.

  1. // Replacement for file_put_contents if it doesn't exist
  2. if(!function_exists('file_put_contents')){
  3.         function file_put_contents($file, $data){
  4.                 $fp = fopen($file, 'w');
  5.                 fwrite($fp, $data);
  6.                 fclose($fp);
  7.         }
  8. }

Display clean php code for copying

// Replacement for file_put_contents if it doesn't exist
if(!function_exists('file_put_contents')){
	function file_put_contents($file, $data){
		$fp = fopen($file, 'w');
		fwrite($fp, $data);
		fclose($fp);
	}
}

Check url

Wednesday, March 17th, 2010

This is a function that checks to see if a url exists.
Not mine btw, but for the life of me I can’t remember where I got it.

  1. // Check if a url exists
  2. function isValidUrl($url){
  3.                 $url = @parse_url($url);
  4.  
  5.                 if ( ! $url) {
  6.                         return false;
  7.                 }
  8.  
  9.                 $url = array_map('trim', $url);
  10.                 $url['port'] = (!isset($url['port'])) ? 80 : (int)$url['port'];
  11.                 $path = (isset($url['path'])) ? $url['path'] : '';
  12.  
  13.                 if ($path == '')
  14.                 {
  15.                         $path = '/';
  16.                 }
  17.  
  18.                 $path .= ( isset ( $url['query'] ) ) ? "?$url[query]" : '';
  19.  
  20.                 if ( isset ( $url['host'] ) AND $url['host'] != gethostbyname ( $url['host'] ) )
  21.                 {
  22.                         if ( PHP_VERSION >= 5 )
  23.                         {
  24.                                 $headers = get_headers("$url[scheme]://$url[host]:$url[port]$path");
  25.                         }
  26.                         else
  27.                         {
  28.                                 $fp = fsockopen($url['host'], $url['port'], $errno, $errstr, 30);
  29.  
  30.                                 if ( ! $fp )
  31.                                 {
  32.                                         return false;
  33.                                 }
  34.                                 fputs($fp, "HEAD $path HTTP/1.1\r\nHost: $url[host]\r\n\r\n");
  35.                                 $headers = fread ( $fp, 128 );
  36.                                 fclose ( $fp );
  37.                         }
  38.                         $headers = ( is_array ( $headers ) ) ? implode ( "\n", $headers ) : $headers;
  39.                         return ( bool ) preg_match ( '#^HTTP/.*\s+[(200|301|302)]+\s#i', $headers );
  40.                 }
  41.                 return false;
  42. }

Display clean php code for copying

// Check if a url exists
function isValidUrl($url){
		$url = @parse_url($url);

		if ( ! $url) {
			return false;
		}

		$url = array_map('trim', $url);
		$url['port'] = (!isset($url['port'])) ? 80 : (int)$url['port'];
		$path = (isset($url['path'])) ? $url['path'] : '';

		if ($path == '')
		{
			$path = '/';
		}

		$path .= ( isset ( $url['query'] ) ) ? "?$url[query]" : '';

		if ( isset ( $url['host'] ) AND $url['host'] != gethostbyname ( $url['host'] ) )
		{
			if ( PHP_VERSION >= 5 )
			{
				$headers = get_headers("$url[scheme]://$url[host]:$url[port]$path");
			}
			else
			{
				$fp = fsockopen($url['host'], $url['port'], $errno, $errstr, 30);

				if ( ! $fp )
				{
					return false;
				}
				fputs($fp, "HEAD $path HTTP/1.1\r\nHost: $url[host]\r\n\r\n");
				$headers = fread ( $fp, 128 );
				fclose ( $fp );
			}
			$headers = ( is_array ( $headers ) ) ? implode ( "\n", $headers ) : $headers;
			return ( bool ) preg_match ( '#^HTTP/.*\s+[(200|301|302)]+\s#i', $headers );
		}
		return false;
}

Normal smooth method

Wednesday, March 17th, 2010

On this post I’ll try to explain how the method for the Normal Smooth script works.


Concept

The idea is to reposition verticles so your mesh ends up nice and smooth. Of course there is already a function in Blender that does it, but that doesn’t take the actual “surface” of the mesh into account. So say you have a part of a perfect sphere selected, and you run the current internal function, it would flatten that selection, or even make it concave (hollow). That’s what we don’t want. In stead if you try to smooth a perfect sphere, it should not change anything. You can’t get smoother than a sphere!

See below here… that’s not nicely smoothed!


Smoothing the normal way

I’ll explain the concept in 2D. Lets say we want to smooth the position of vertex A and it’s connected to vertex B and C. Then we get the vertex normals for B and C. We then rotate those normal vectors 90 degrees towards A and make them half the length of the distance between that specific vert (B or C) and A. Once we have those, we find the points at the ends of those two vectors and place vert A at the midpoint between them.

But let me explain with a picture, which should help.


That’s just the basics

Of course there is a lot more that you can do with a script. Like looking further along the surface and using more normals. Also in 3d at times you have quads and you need to figure out what you want to do with the vert at the far end of the quad. Having non manifold meshes can be tricky too.

At least I hope this explains the idea a bit, and as ideas go…. it’s not too bad

Dolf

Normal smooth

Wednesday, March 17th, 2010

Downloadable Files

This script is an alternative to the mesh smooth function in blender. The mesh smooth function in blender uses only vertex position.This script uses vertex normals, which is why it’s called normal smoothing.In version 6 the code has been cleaned (versions 2, 3, and 4 were never released). The script now no longer needs you to have part of the mesh selected, and part unselected. So you can run it on an entire mesh, but it still works best for a few edge loops/verts. I also solved the cases in which it made ‘flat surfaces’ “wavy”. You may need more iterations than in older versions.


Requirements

This script is written for blender 2.45 but should work with older versions as well


Example meshes the script was used on (parts of them)


Comparison

Before we get going…. Here is a comparison between blender’s internal function and the script.


Settings

There is only one setting which shows up in a popup when you run the script.

The number of iterations is the total ammount of times the script will run one after the other… normally a value of one or two is enough.


Usage

Select the verticles you want smoothed and run the script.

Pre script example:

Post script example:


That’s it

That’s all for now… I hope someone can help solve the large area issue… untill then it’s already quite usefull to me ;)
Dolf

click here to close

Help keep these files free,
and support further development!