blend2panda.py

I quickly threw together script that exports meshes from blender through .x format.

Tested on windows, should work on linux i guess. In case you have blender on non-standard path - you should it put on PATH or edit script so it can find blender.

Use:
copy script to panda3d-1.8.0\bin\blend2panda.py
run: python blend2panda.py mesh.blend mesh.(x|egg|bam) [-d] [-f]
-d - mesh is dynamic (export animations)
-f - export full animations (not only key frames)

You probably will want to use -d -f always for animated meshes.
As you can see you also can export to x, egg and bam formats.

My test mesh came out sort of messed up, i dont know if its fault of exporter or what. Dont feel like testing any more today. Im leaving script for you kids to play with :slight_smile:

import sys, os
import subprocess
from subprocess import CalledProcessError

if os.path.basename(sys.argv[0]) in ['blender', 'blender.exe']:
	import bpy
	from io_export_directx_x import DirectXExporterSettings, ExportDirectX
	args = sys.argv[6:]
	output = args[0]
	is_dynamic = '-d' in args
	anim_mode  = 2 if '-f' in args else 0
	cfg = DirectXExporterSettings(
		bpy.context,
		output,
		CoordinateSystem=1,
		RotateX=True,
		FlipNormals=False,
		ApplyModifiers=True,
		IncludeFrameRate=True,
		ExportTextures=True,
		ExportArmatures=is_dynamic,
		ExportAnimation=anim_mode,
		ExportMode=1,
		Verbose=False)
	ExportDirectX(cfg)
else:
	blender = 'blender'
	if os.name == 'nt':
		drive = os.getenv('SystemDrive')
		if os.path.exists(drive + r'\Program Files\Blender Foundation\Blender\blender.exe'):
			blender = drive + r'\Program Files\Blender Foundation\Blender\blender.exe'
		elif os.path.exists(drive + r'\Program Files (x86)\Blender Foundation\Blender\blender.exe'):
			blender = drive + r'\Program Files (x86)\Blender Foundation\Blender\blender.exe'
	
	input  = sys.argv[1]
	output = sys.argv[2]
	def get_path(ext):
		if output.endswith(ext):
			return output
		return '__tmp__%s%s' % ( os.path.basename(output), ext )
		
	x_path   = get_path('.x')
	egg_path = get_path('.egg')
	bam_path = get_path('.bam')
	
	try:
		rest_of_args = sys.argv[3:]
	except:
		rest_of_args = []

	try:
		cmdline = [blender, '-b', input, '-P', 'blend2panda.py', '--', x_path]
		cmdline.extend(rest_of_args)
		subprocess.check_call(cmdline)
	except CalledProcessError as e:
		print('Error while exporting to .x: '+str(e))
		sys.exit(-1)
	
	if output.endswith('.egg') or output.endswith('.bam'):
		try:
			subprocess.check_call(['x2egg', '-o', egg_path, x_path])
			os.unlink(x_path)
		except:
			print('Error while exporting to .egg')
			sys.exit(-2)
		
		if output.endswith('.bam'):
			try:
				subprocess.check_call(['egg2bam', '-o', bam_path, egg_path])
				os.unlink(egg_path)
			except:
				print('Error while exporting to .egg')
				sys.exit(-2)
			
	print('Exported!')
	sys.exit(0)