[Bf-python] Texturing an object with python

Paul Coones paulcoones at comcast.net
Thu Jul 12 23:52:39 CEST 2012


I have coded in python everything to read a file and import verts, normals, uvs, faces.
When I try to have the object textured in the 3D window with python, it does not succeed.

I can select the object and it goes into edit mode and the uv's appear in the image window on the uv image in the correct places. I have to select the texture map by hand then I can then can press "A" and "tab" and the object will then be textured. But using python code I cannot get it to do this.

Here's the section of python code which I'm using to texture objects:

		print('160 - Importing materials...')		
		#mesh.materials.append(material)
		tex_map = [('use_map_color_diffuse', 0)]
		for map, i in tex_map:  
			mapName = os.path.basename(str(mapname))
			tex = bpy.data.textures.new(objname + 'Tex', type='IMAGE')
			import bpy_extras.image_utils
			tex.image = bpy_extras.image_utils.load_image(mapName, '/Users/paulcoones/UNPODDED_/art')
			if tex.image != None:
				print("189 - smf_import: !INFO! Loaded %s" % (mapName))
				#bpy.ops.image.view.Link(tex.image)
				use_image_pin = True
			else:
				print("191 - smf_import: !WARNING! Cannot find texture \"%s\"" % (mapName))
			
			tex.use_alpha = True
			mat = bpy.data.materials.new(objname + 'Mat')
			mat.use_shadeless = True
			mtex = mat.texture_slots.add()
			mtex.texture = tex
			mtex.texture_coords = 'UV'  
			mtex.use_map_color_diffuse = True
			smf_obj.data.materials.append(mat)

			# Attempt to texture this part
			bpy.ops.object.editmode_toggle()			
			#mn = bpy.data.images[mapName]   
			#bpy.ops.image.space_data.image = mn
			#bpy.ops.image.reload()
			bpy.ops.uv.select_all(action='TOGGLE')  
			bpy.ops.object.editmode_toggle()
			bpy.ops.object.select_all(action='TOGGLE')
			bpy.ops.object.editmode_toggle()
			bpy.ops.uv.select_all(action='TOGGLE')
			bpy.ops.object.editmode_toggle()
			#setattr(uvtex,map,True)
			#mesh.update(calc_edges=True)		 
			print('192 - this part unwrapped')

It appears that I am having trouble selecting the texture map in code.
Perhaps you could take a look at the python code and suggest a solution. Thanks.





More information about the Bf-python mailing list