sharp construct importer, importing vertex color info

Scripting in Blender with Python, and working on the API

Moderators: jesterKing, stiv

Post Reply
butelo
Posts: 0
Joined: Wed Oct 27, 2004 12:28 pm
Location: corunha galicia

sharp construct importer, importing vertex color info

Post by butelo » Wed Mar 15, 2006 11:45 am

hi all

I'm trying to write a script for importing sharpconstruct meshes.

A sharpconstruct mesh is an obj with vertex color info:

here you have an example:

Code: Select all

# Exported from SharpConstruct 0.11 - http://sharp3d.sourceforge.net/

v 1 -0 -1
v -1 -0 -1
v -1 0 1
v 0.013841 1.24523 -0.127313
f 1 3 2
f 1 4 3
f 2 3 4
f 1 2 4
#vertexcolor 0 1 1 1
#vertexcolor 1 1 0.211778 0.211778
#vertexcolor 2 0.715574 1 0.715574
#vertexcolor 3 0.378977 0.378977 0.867324
Well, i`m very newbie in python stuff and writing code but i've come to this:

Code: Select all

import Blender
import string
import struct
from Blender import Mesh, Material, NMesh, Object


def import_obj(path):
	Blender.Window.WaitCursor(1)
	name = path.split('\\')[-1].split('/')[-1]
	mesh = Mesh.New( name )
	file = open(path, 'r')
        n = 0
        vertColList = []
	for line in file.readlines():
                words = line.split()
                if len(words) == 0 or words[0].startswith('# '):
                        pass
                elif words[0] == 'v':
                        x, y, z = float(words[1]), float(words[2]), float(words[3])

                        l = [(x, y, z)]


                        mesh.verts.extend(l)
                        
                

				
                elif words[0] == 'f':
                        faceVertList = []


                        for faceIdx in words[1:]:


                            faceVert = mesh.verts[int(faceIdx)-1]
                            faceVertList.append(faceVert)
                        mesh.faces.extend(faceVertList)
                   
                elif words[0] == '#vertexcolor':
                    
                    r, g, b = int(float(words[2])*255), int(float(words[3])*255), int(float(words[4])*255)               

                    vertColList.append(( r,  g,  b, 255))
        mesh.vertexColors = 1

        print vertColList
                            

        

        


						
						
						
						
						# link the mesh to a new object
        ob = Blender.Object.New('Mesh', name)
        ob.link(mesh) # tell the object to use the mesh we just made
        scn = Blender.Scene.GetCurrent()
        for o in scn.getChildren():
                o.sel = 0
       
        scn.link(ob) # link the object to the current scene
        ob.sel= 1

		

					
                
                

        
		
		
		
        ob.Layers = scn.Layers
        Blender.Window.WaitCursor(0)
        Blender.Window.RedrawAll()


                        
                        
	
Blender.Window.FileSelector(import_obj, "Import")
Taking pieces of code here and there.



But I can't apply vertexcolor info to the mesh...

Any help will be appreciate

Tx a lot.

butelo
Posts: 0
Joined: Wed Oct 27, 2004 12:28 pm
Location: corunha galicia

Post by butelo » Mon Mar 20, 2006 7:32 pm

hi, its me again :twisted:

I've made some advances but I cannot set a sequence of vertex colors in Mesh Module

Can anybody tellme how I set a sequence of vertex colors, in Mesh Module?

In blender's API there's no any example, neither in google

tx

Post Reply