Take 2 on UTF8 BOM : Remove BOM with PHP

Some people asked me about my UTF8 BOM problems in PHP and XML. They were wondering if it was possible to remove the BOM from the files, without damaging them. And if PHP could do this. They had hundreds of files with UTF8 BOM in them and it would be time consuming to remove by hand, if they weren’t able to find a solution.

My answer was, “of course”. PHP can read and remove BOM from every file. As we encounter this problem only in text based files, a string remover will do the trick. Applause for substr().

I wrote a php based bom remover that works both from web and command line. You can download/fork the code from the github repository.

Please backup your files before running the script.

 

This slideshow requires JavaScript.

 

Create sitemap.xml In WordPress Without Using Any Plugins

Everybody likes plugins, and most of them are really great (in case of WordPress). But some people like me, tend to do the work by themselves.

So, here is a simple code that will re-create a sitemap.xml file, everytime you publish a post or page. Copy the code and paste it to your theme’s functions.php file (wp-content/themes/your_theme_folder/functions.php).

add_action("publish_post", "eg_create_sitemap");
add_action("publish_page", "eg_create_sitemap");

function eg_create_sitemap() {
  $postsForSitemap = get_posts(array(
    'numberposts' => -1,
    'orderby' => 'modified',
    'post_type'  => array('post','page'),
    'order'    => 'DESC'
  ));

  $sitemap = '<?xml version="1.0" encoding="UTF-8"?>';
  $sitemap .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';

  foreach($postsForSitemap as $post) {
    setup_postdata($post);

    $postdate = explode(" ", $post->post_modified);

    $sitemap .= '<url>'.
      '<loc>'. get_permalink($post->ID) .'</loc>'.
      '<lastmod>'. $postdate[0] .'</lastmod>'.
      '<changefreq>monthly</changefreq>'.
    '</url>';
  }

  $sitemap .= '</urlset>';

  $fp = fopen(ABSPATH . "sitemap.xml", 'w');
  fwrite($fp, $sitemap);
  fclose($fp);
}

If you’ve got custom post types, you’ll need to add the registered taxonomy to ‘post_type’ array part of the code.

For a big blog with thousands of posts, creating a sitemap.xml file with every post publish action might not be a good solution, so keep that in mind.

Random Rotate Maxscript Version 1.1

The script rotates the selected meshes in any (or any combination of, or all) axis. Just enable one or more of the axises, adjust minimum, maximum values to your liking, press the random rotate button. Check out the gallery for how to install and run the script.

Download Random Rotate

You can use “random rotate” with my “random scale” script for more randomness.

This slideshow requires JavaScript.

Random Material Assigner for Objects with Multiple MaterialIDs

Works on one or multiple object selections. Script detects the materialID count and creates a multi-sub material(s) for the selected object(s), and changes the diffuse channel with a random color. If there is only one materialID on the object, script simply creates a standart material.

Download Random Material Assigner for Objects with MaterialID

As this is a macroscript, after running it, you need to add it to a quad menu, or an icon on a toolbar, or to a menu. Script’s category is “emrahgunduz.com”.

This slideshow requires JavaScript.

Keep these in mind

  • Created materials are named after the object’ own name.
  • Materials in the multi-sub object will be “Standard”.
  • Changes diffuse channel color with a random one.
  • Works only with poly, mesh or patch object. There is no nurbs or basic object support.

Randomly Delete Objects With Maxscript

The script randomly deletes meshes from the selection. As the script depends on the random numbers created by 3dsmax, the percentage can be %0-1 off what you expected. Just select the meshes you want to randomly delete. Enter a percentage, click the Delete button.

Download Random Delete

This slideshow requires JavaScript.

Random Color Maxscript

I hate how wire coloring (default color max assigns to objects when they first created) works in max. After working a few hours, all objects start to appear in lots of colors which really distract me. So, a few months back, I wrote a script to color them in gray. For sharing, I changed the script, added a gui and some improvements.

Download Random Color

This one can randomly color the selected objects in RGB, or simply red, green, blue or a color of gray. If you need to color every object for ObjectID in post processing, you might need something like this.

This slideshow requires JavaScript.

Random Face Deletion / Selection with Maxscript

For sometime now, some of my friends asked me to write a script for random face selection for poly objects. Here is a simple code for doing exactly what it’s asked for:

sel = selection as array
count = 1

undo on
(
	if sel.count > 0 then
	(
		while count <= sel.count do
		(
			select sel[count]
			count += 1
			
			facenumber = polyop.getNumFaces $
			randomset = #()
			for i=1 to facenumber do
			(
				rand = random 0 100
				if rand < 50 do append randomset i
			)
			
			polyop.deleteFaces $ randomset delIsoVerts:true
		)
	)
)
select sel

It is not hard to write a simple random face select/delete script. This script will go through every selected poly object, and randomly delete some of the faces. I did not add any error checking in this, so if you select any object that’s not a poly, you’ll get an error. You can also disable the viewport update and enable after the faces get deleted. This will make the script run faster on high-poly count objects.

If you are experiencing errors before script deletes any faces, change the random range’s 0 to 1. For more randomness use a big number for the second part of the random statement. 100 seems OK. If you do not want to delete isolated vertices –vertices that do not belong to any faces-, just change delIsoVerts to false. For keeping the original object, just add an Edit Poly modifier and then run the script. By this way, removing the modifier might save you some production time.

Requested addition #1: And, this script will only randomly select faces, but not delete them :

sel = selection as array
count = 1

undo on
(
	if sel.count > 0 then
	(
		while count <= sel.count do
		(
			select sel[count]
			count += 1
			
			facenumber = polyop.getNumFaces $
			randomset = #()
			for i=1 to facenumber do
			(
				rand = random 0 100
				if rand < 50 do append randomset i
			)
			
			subobjectlevel = 4
			polyop.setFaceSelection $ randomset
		)
	)
)
select sel

Requested Addition #2: This one will assign a new material id that is not used on the mesh to the randomly selected polys.

sel = selection as array
count = 1
 
undo on
(
	if sel.count > 0 then
	(
		while count <= sel.count do
		(
			select sel[count]
			count += 1

			facenumber = polyop.getNumFaces $
			randomset = #()
			newID = 0
				for i=1 to facenumber do
				(
					if (polyop.getFaceMatID $ facenumber) > newID do newID = (polyop.getFaceMatID $ facenumber)+1
					rand = random 0 100
					if rand < 50 do append randomset i
				)

			polyop.setFaceMatID $ randomset newID
		)
	)
)
select sel

This slideshow requires JavaScript.

Make Unique (Deinstance) Maxscript

Here is a simple maxscript that works with instanced objects. While working with Maxwell, I always have problems with instanced light meshes. So, I thought writing a script for this job would be fine.

Download DeInstance

Just select some objects from the scene. Script checks every one of them, finds instances and makes them unique. As this is a macroscript, you need to add it to a quad menu, a toolbar or a menu. You can find the script in category “emrahgunduz.com”, named as “DeInstance”.

Detach Using Material IDs

This script detaches faces as new objects, using Material ID property of the faces. Most of the cases, matIDs are used to assign multiple materials on one object. Sometimes you might want to detach these as new objects.

Download Detach Using Material IDs Maxscript

It is possible to detach every matID as object, or simply a range, or just one with Min ID and Max ID ranges. If you keep the default settings, every matID becomes a new poly object. The remainder will be a faceless poly, script deletes this poly if this is the case. Naming will be done by adding the ID number at the end of the original name of the selected object (originalname_ID_#). Wirecolor will also be randomly changed. The pivots of the detached objects will be the same as the original object.

Before using the script please keep these in mind :

  • Script only works with polys. If you have a mesh or patch, you’ll need to convert it to poly mesh, or add an “edit poly” modifier on the stack.
  • Do not detach instanced objects, as this will cause deletion of the faces on the instances.
  • For objects with high poly count, it may take some time to process all the faces. I tried the script with a million poly object, it took 30 seconds or something close to detach every matID available. It might be better to detach some of the matID’s by hand, and then using the script.
  • Why is it slow with high poly objects ? There is no method in 3dsmax for detecting the matID count directly. Script goes through every face and count unique matID channels. Then detaches these if desired.

This slideshow requires JavaScript.

Auto Mirror Selected Using a Specific/Picked Object

One of the missing parts of 3ds max is, you can’t find any APIs related to most of the functions. Mirroring is one of them. It has no functions that you can use from maxscript. Just “max mirror” which actually opens the mirror dialog toolbox.

Yet, there is another way for controlling max: dialog monitoring. If you are so tired of selecting objects that needs to be mirrored to a specific object, and selecting or picking that object, plus changing the axis to “use transform center”, this is the script for you.

Download Auto Mirror Script

This script assumes that you’ve an object named MIRROROBJ in the scene, which will be used as the base. The axis of this object will be our transform center. So keep in mind that and before using this script, add your mirror object and move it to anywhere you like.

This slideshow requires JavaScript.