IndentationError: unexpected indent

Scripting in Blender with Python, and working on the API

Moderators: jesterKing, stiv

Post Reply
rich1812
Posts: 0
Joined: Wed Sep 18, 2013 3:37 pm

IndentationError: unexpected indent

Post by rich1812 » Wed Sep 18, 2013 3:51 pm

Hi, I am new to this forum. The reason I write is because I have a python scrip question. I am not a python guy, my knowledge to python is next to nil.
I try to export a .blender model to a C header file. Does anyone know of a python script can do that?

I have the script by JEFF LAMARCHE

Code: Select all

bl_info = {
	"name": "Export Objective-C Header (.h)",
	"author": "Jeff LaMarche",
	"version": (1, 0),
	"blender": (2, 5, 7),
	"api": 35622,
	"location": "File > Export",
	"description": "Export Model to Objective-C Header file",
	"warning": "",
	"wiki_url": "",
	"tracker_url": "",
	"category": "Import-Export"}

'''
    Usage Notes:
    
    
    '''

import bpy
from bpy.props import *
import mathutils, math, struct
import os
from os import remove
import time
import bpy_extras
from bpy_extras.io_utils import ExportHelper
import time
import shutil
import bpy
import mathutils

def triangulateNMesh(object):
	bneedtri = False
	scene = bpy.context.scene
	bpy.ops.object.mode_set(mode='OBJECT')
	for i in scene.objects: i.select = False #deselect all objects
	object.select = True
	scene.objects.active = object #set the mesh object to current
	print("Checking mesh if needs to convert quad to Tri...")
	for face in object.data.faces:
		if (len(face.vertices) > 3):
			bneedtri = True
			break
    
	bpy.ops.object.mode_set(mode='OBJECT')
	if bneedtri == True:
		print("Converting quad to tri mesh...")
		me_da = object.data.copy() #copy data
		me_ob = object.copy() #copy object
		#note two copy two types else it will use the current data or mesh
		me_ob.data = me_da
		bpy.context.scene.objects.link(me_ob)#link the object to the scene #current object location
		for i in scene.objects: i.select = False #deselect all objects
		me_ob.select = True
		scene.objects.active = me_ob #set the mesh object to current
		bpy.ops.object.mode_set(mode='EDIT') #Operators
		bpy.ops.mesh.select_all(action='SELECT')#select all the face/vertex/edge
		bpy.ops.mesh.quads_convert_to_tris() #Operators
		bpy.context.scene.update()
		bpy.ops.object.mode_set(mode='OBJECT') # set it in object
		print("Triangulate Mesh Done!")
		print("Remove Merge tmp Mesh [ " ,object.name, " ] from scene!" )
		bpy.ops.object.mode_set(mode='OBJECT') # set it in object
		bpy.context.scene.objects.unlink(object)
	else:
		print("No need to convert tri mesh.")
		me_ob = object
	return me_ob


def writeString(file, string):
	file.write(bytes(string, 'UTF-8'))

def do_export(context, props, filepath):
	mat_x90 = mathutils.Matrix.Rotation(-math.pi/2, 4, 'X')
	ob = context.active_object
	me_ob = triangulateNMesh(ob)
	current_scene = context.scene
	apply_modifiers = props.apply_modifiers
    
	mesh = me_ob.to_mesh(current_scene, apply_modifiers, 'PREVIEW')
    
	basename = mesh.name.capitalize()
    
	if props.world_space:
		mesh.transform(ob.matrix_world)
    
	if props.rot_x90:
		mesh.transform(mat_x90)
    
	file = open(filepath, "wb")
	theHeader = '//If not using MC3D, change 1 to 0 to add needed types\n#if 1\n\t#import "MC3DTypes.h"\n#else\n\
	struct texCoord\n\
	{\n\
    GLfloat		u;\n\
    GLfloat		v;\n\
	};\n\
	typedef struct texCoord texCoord;\n\
	typedef texCoord* texCoordPtr;\n\
    \n\
	typedef struct vec2 vec2;\n\
	typedef vec2* vec2Ptr;\n\
    \n\
	struct vec3\n\
	{\n\
    GLfloat x;\n\
    GLfloat y;\n\
    GLfloat z;\n\
	};\n\
    \n\
	typedef struct vec3 vec3;\n\
	typedef vec3* vec3Ptr;\n\
    \n\
	struct vec4\n\
	{\n\
    GLfloat x;\n\
    GLfloat y;\n\
    GLfloat z;\n\
    GLfloat w;\n\
    };\n\
    \n\
	typedef struct vec4 vec4;\n\
	typedef vec4* vec4Ptr;\n\
	\n\
    #endif\n\
    \n'
	writeString(file, theHeader)
    
	if len(mesh.uv_textures) > 0:
		writeString(file, 'struct vertexDataTextured\n\
                    {\n\
                    vec3		vertex;\n\
                    vec3		normal;\n\
                    texCoord	texCoord;\n\
                    }\n\
                    typedef struct vertexDataTextured vertexDataTextured;\n\
                    typedef vertexDataTextured* vertexDataTexturedPtr;\n\n\n')
                    writeString(file, 'static const vertexDataTextured %sVertexData[] = {\n' % basename)
                    #for face in uv: #loop through the faces
                    uv_layer = mesh.uv_textures.active
                    for face in mesh.faces:
                        faceUV = uv_layer.data[face.index]
                        i=0
                        for index in face.vertices:
                            if len(face.vertices) == 3:
                                vert = mesh.vertices[index]
                                writeString(file, '\t{/*v:*/{%f, %f, %f}, ' % (vert.co.x, vert.co.y, vert.co.z) )
                                writeString(file, '/*n:*/{%f, %f, %f}, ' % (vert.normal.x, vert.normal.y, vert.normal.z))
                                writeString(file, '/*t:*/{%f, %f}' % ( faceUV.uv[i][0], faceUV.uv[i][1] ) )
                                writeString(file, '},\n')
                                i+=1
                    writeString(file, '};\n\n')
	elif len(mesh.vertex_colors) > 0:
		writeString(file, 'struct vertexDataColored\n\
                    {\n\
                    vec3		vertex;\n\
                    vec3		normal;\n\
                    vec4		color;\n\
                    }\n\
                    typedef struct vertexDataColored vertexDataColored;\n\
                    typedef vertexDataColored* vertexDataColoredPtr;\n\n\n')
                    writeString(file, 'static const vertexDataColored %sVertexData[] = {\n' % basename)
                    color_layer = mesh.vertex_colors.active
                    for face in mesh.faces:
                        if len(face.vertices) == 3:
                            faceC = color_layer.data[face.index]
                            i=0
                            for index in face.vertices:
                                vert = mesh.vertices[index]
                                writeString(file, '\t{/*v:*/{%f, %f, %f}, ' % (vert.co.x, vert.co.y, vert.co.z) )
                                writeString(file, '/*n:*/{%f, %f, %f}, ' % (vert.normal.x, vert.normal.y, vert.normal.z))
                                writeString(file, '/*c:*/{%f, %f, %f, %f}' % ( faceC.color1[i], faceC.color2[i], faceC.color3[i], faceC.color4[i]) )
                                writeString(file, '},\n')
                                i+=1
                    writeString(file, '};\n\n')
	else:
		writeString(file, 'struct vertexData\n\
                    {\n\
                    vec3		vertex;\n\
                    vec3		normal;\n\
                    }\n\
                    typedef struct vertexData vertexData;\n\
                    typedef vertexData* vertexDataPtr;\n\n\n')
                    writeString(file, 'static const vertexData %sVertexData[] = {\n' % basename)
                    for face in mesh.faces:
                        if len(face.vertices) == 3:
                            for index in face.vertices:
                                vert = mesh.vertices[index]
                                writeString(file, '\t{/*v:*/{%f, %f, %f}, ' % (vert.co.x, vert.co.y, vert.co.z) )
                                writeString(file, '/*n:*/{%f, %f, %f} ' % (vert.normal.x, vert.normal.y, vert.normal.z))
                                writeString(file, '},\n')
                    writeString(file, '};\n\n')
    
	writeString(file, '// Example OpenGL ES 1.1 Drawing Code:\n')
	writeString(file, '// glEnableClientState(GL_VERTEX_ARRAY);\n')
	if len(mesh.uv_textures) > 0:
		writeString(file, '// glEnableClientState(GL_TEXTURE_COORD_ARRAY);\n')
	elif len(mesh.vertex_colors) > 0:
		writeString(file, '// glEnableClientState(GL_COLOR_ARRAY);\n')
		writeString(file, '// glEnable(GL_COLOR_MATERIAL)\n')
	writeString(file, '// glEnableClientState(GL_NORMAL_ARRAY);\n')
	writeString(file, '// glVertexPointer(3, GL_FLOAT, sizeof(')
	if len(mesh.uv_textures) > 0:
		writeString(file, 'TexturedVertexData3D')
	elif len(mesh.vertex_colors) > 0:
		writeString(file, 'ColoredVertexData3D')
	else:
		writeString(file, 'VertexData3D')
	writeString(file, '), &%sVertexData[0].vertex);\n' % basename)
	writeString(file, '// glNormalPointer(GL_FLOAT, sizeof(')
	if len(mesh.uv_textures) > 0:
		writeString(file, 'TexturedVertexData3D')
	elif len(mesh.vertex_colors) > 0:
		writeString(file, 'ColoredVertexData3D')
	else:
		writeString(file, 'VertexData3D')
	writeString(file, '), &%sVertexData[0].normal);\n' % basename)
	if len(mesh.uv_textures) > 0:
		writeString(file, '// glTexCoordPointer(2, GL_FLOAT, sizeof(TexturedVertexData3D), &%sVertexData[0].texCoord);\n' % basename)
	elif len(mesh.vertex_colors) > 0:
		writeString(file, '// glColorPointer(4, GL_FLOAT, sizeof(ColoredVertexData3D), &%sVertexData[0].color);\n' % basename)
	writeString(file, '// glDrawArrays(GL_TRIANGLES, 0, k%sNumberOfVertices);\n' % basename)
	writeString(file, '// glDisableClientState(GL_VERTEX_ARRAY);\n')
	if len(mesh.uv_textures) > 0:
		writeString(file, '// glDisableClientState(GL_TEXTURE_COORD_ARRAY);\n')
	elif len(mesh.vertex_colors) > 0:
		writeString(file, '// glDisableClientState(GL_NORMAL_ARRAY);\n')
		writeString(file, '// glDisable(GL_COLOR_MATERIAL);\n')
	writeString(file, '// glDisableClientState(GL_NORMAL_ARRAY);\n\n\n')
    
    
	file.flush()
	file.close()
    
	return True


###### EXPORT OPERATOR #######
class Export_objc(bpy.types.Operator, ExportHelper):
	'''Exports the active
I run it in the python consul in Blender,
it keeps give me this error:
>>> exec(compile(open(filename).read(), filename, 'exec'))
Traceback (most recent call last):
File "<blender_console>", line 1, in <module>
File "/Users/rich/Desktop/gl2c.py", line 139
writeString(file, 'static const vertexDataTextured %sVertexData[] = {\n' % basename)
^
IndentationError: unexpected indent

How can I fix it?

TIA

stiv
Posts: 0
Joined: Tue Aug 05, 2003 7:58 am
Location: 45N 86W

Post by stiv » Wed Sep 18, 2013 5:28 pm

Python uses indentation to delimit control structures. Useful hints: don't mix spaces and tabs, and use a Python-aware text editor.

To fix your problem, make sure the indentation of your write statement( the error line) matches the indentation of the previous line.

Post Reply