Jump to content

Blender trisoup exporter (WIP, functioning)


Recommended Posts

My first python script, took a while to bugfix it and get it working. It's WIP as I'd like to convert it to a blender Addon, in which I can let the user define things like scale, a few parameters to control the 4th vertex production, etc.

 

Atm, it generates trisoup so its great for making terrain. You could import a height map into blender and displace a subdivided plane for a quick 'easygen' like terrain. Or you can sculpt out something.

 

 

Set blender's grid to metric centimeters. 

1 meter in blender will now be 64 units in radiant.

This is a good size for a terrain triangle, I wouldn't go much smaller than that overall, just in areas of intense detail (yes, we can have intense detail now! :) )

 

The brush generated is a 4 sided prism. I chose this because you can edit the vertices of surrounding terrain tris very easily within Radiant without worrying about breaking the brush. The 4th vertex is positioned at the apex of the triangle and shifted back along the normal. It is controlled by the 'depth' variable in the script.

import bpy
import bmesh

bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_mode(type='FACE')

obj = bpy.context.active_object
mesh = obj.data
bm = bmesh.new()
bm.from_mesh(mesh)
bm.normal_update()
rounding = '.0f'
depth = 8
f = open("export_to_trisoup.map", "w")

f.write('// entity 0\n{\n"classname" "worldspawn"\n')
brush = 0
e = []
for face in mesh.polygons:
	f.write("// brush %s \n{\n" % brush)
	brush += 1 
	normal_x = face.normal.x; normal_y = face.normal.y; normal_z = face.normal.z
	center_x = face.center.x; center_y = face.center.y; center_z = face.center.z
	vertices = face.vertices[:]
	v = []
	for vert in vertices:
		v.append(format(mesh.vertices[vert].co.x, rounding) )
		v.append(format(mesh.vertices[vert].co.y, rounding) )
		v.append(format(mesh.vertices[vert].co.z, rounding) )
	e = format(center_x - normal_x * depth, rounding), format(center_y - normal_y * depth, rounding), format(center_z - normal_z * depth, rounding)
	f.write('( {0} {1} {2} ) ( {3} {4} {5} ) ( {6} {7} {8} ) system/clip 0 0 0 0.1 0.1 0 0 0\n'.format  (v[3],v[4],v[5],v[0],v[1],v[2],v[6],v[7],v[8]))
	f.write('( {0} {1} {2} ) ( {3} {4} {5} ) ( {6} {7} {8} ) system/nodraw 0 0 0 0.1 0.1 0 0 0\n'.format(v[6],v[7],v[8],e[0],e[1],e[2],v[3],v[4],v[5]))
	f.write('( {0} {1} {2} ) ( {3} {4} {5} ) ( {6} {7} {8} ) system/nodraw 0 0 0 0.1 0.1 0 0 0\n'.format(v[0],v[1],v[2],e[0],e[1],e[2],v[6],v[7],v[8]))
	f.write('( {0} {1} {2} ) ( {3} {4} {5} ) ( {6} {7} {8} ) system/nodraw 0 0 0 0.1 0.1 0 0 0\n'.format(v[3],v[4],v[5],e[0],e[1],e[2],v[0],v[1],v[2]))
	f.write('}\n')

bpy.ops.object.mode_set(mode='OBJECT')
f.write('}\n')
f.close()

To use, copy the above text and paste into the python console in Blender. It works only on the currently selected object.

 

Note: Ridiculously small brushes may crash Radiant. Do not use 'Import .map' in Radiant with an unsaved map file to avoid the small possibility that an incorrectly scaled model might crash radiant on import.

If you stick to the grid size mentioned above, the tris should could out pretty good. 

 

 

 

d9a5c48822.jpg

 

 

 

Showing the back side:

 

fa59293315.jpg

 

AshuraDX, Boothand, Jeff and 4 others like this
Link to comment
  • 3 weeks later...

No, you lose the ability to texture in Radiant and would have to define the textures in Blender first. You also lose the weapon marks on models, I don't understand why but I think Razor fixed that in one of his mods. Lastly, the game's collision detection can really screw up on terrain models, leaving problems like being able to climb a wall because of strange invisible surfaces jutting out from the model, possible in the SP jawa droid level for example.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...