converting a Blender210 script to 2.28

Scripting in Blender with Python, and working on the API

Moderators: jesterKing, stiv

Post Reply
MattyMatt
Posts: 3
Joined: Sat Jul 26, 2003 9:51 pm

converting a Blender210 script to 2.28

Post by MattyMatt » Sun Jul 27, 2003 1:19 am

How can I do this in the new API?

Code: Select all


def test_mesh(self, meshname):
			object = Blender.getObject(meshname)
			mesh = Blender.getMesh(object.data)
			# mesh.enterEditMode()
			n_vtx = len(mesh.vertices)
			index = 0
			for v in mesh.vertices:
				for vj in self.joinvtxs:
					if (self.is_dist_lt_tol(v, vj)):
						mesh.vertices[index] = tuple([[vj[0], vj[1], vj[2]]])
						#mesh.setVertexCoordinates(index, ([vj[0], vj[1], vj[2]))
						#v[0] = vj[0]
						#v[1] = vj[1]
						#v[2] = vj[2]
						print "%d : (%1.3f,%1.3f,%1.3f)" % (index, v[0], v[1], v[2])						
				index = index + 1
			# mesh.leaveEditMode()
This includes some commented-out detritus from various attempts at rewriting. In the end I couldn't get the vertices to update in either 2.23 or 2.27 so now I'm rewriting for 2.28.

This is part of a script to snap vertices from a series of hand modelled meshes into exact alignment with a template mesh (which has its vertices in self.joinvtxs before the above function is run on each mesh). The is_dist_lt_tol() func simply tests if 2 tuples are closer than a global tolerance value.

I will paste this handy little script back here when it's working.


----------------------------------------

Update
This all works now, except for the PutRaw which says
AttributeError: nmesh vertices must be NMverts

Code: Select all


			object = Blender.Object.Get(meshname)
			mesh = Blender.NMesh.GetRaw(object.data.name)
			n_vtx = len(mesh.verts)
			index = 0
			for v in mesh.verts:
				for vj in self.joinvtxs:
					if (self.is_dist_lt_tol(v, vj)):
						mesh.verts[index] = tuple([[vj[0], vj[1], vj[2]]])
						print "%d : (%1.3f,%1.3f,%1.3f)" % (index, v[0], v[1], v[2]) 						
				index = index + 1
			Blender.NMesh.PutRaw(mesh, object.data.name) 
[/code]

willian
Posts: 20
Joined: Mon Jan 13, 2003 3:50 am

Post by willian » Sun Jul 27, 2003 10:24 pm

Your "mesh.verts" is a list of NMVert objects, so you first need to create each NMVert:

newvert = Blender.NMesh.Vert(x, y, z)

and then you can add/insert each newvert in mesh.verts

BTW: since you got an existing mesh with NMesh.GetRaw(), unless you want to create a new object, you don't *have* to use NMesh.PutRaw() -- a simple mesh.update() should be enough.

--
Willian

MattyMatt
Posts: 3
Joined: Sat Jul 26, 2003 9:51 pm

Post by MattyMatt » Mon Jul 28, 2003 2:32 am

Cheers, I found mesh.update() in the CVS docs

here's the whole script so far

Code: Select all

# this script is for exactly aligning the edges of multiple meshes   


import	Blender
from Blender import NMesh
import	math
#import	string
import	sys


class MeshEdgeAlign:

	def __init__(self, joinmesh,tolerance):
		self.joinmesh = joinmesh
		self.joinvtxs = []
		self.tolerance = tolerance
		self.scene   = None
		self.meshesused = []

	def use_mesh_series(self, meshroot, meshindices):
		for n in meshindices:
			meshname = "%s.%03i" % (meshroot,n)
			print "using %s" % meshname
			self.meshesused.append(meshname)
#			self.meshname = meshname
#			self.writeVertexes(meshname)

	def list_meshes_used(self):
		for n in self.meshesused:
			print "used %s" % n

	def test_all_meshes(self):
		object = Blender.Object.Get(self.joinmesh)
		mesh = Blender.NMesh.GetRaw(object.data.name)
		n_vtx = len(mesh.verts)
		for v in mesh.verts:
			self.joinvtxs.append([v.co[0],v.co[1],0.0000])  # v.co[2] replaced with literal 0 (because my edge happens to be on the z=0 plane)
				
		for n in self.meshesused:
			print "testing %s" % n
			self.test_mesh( n )

	def test_mesh(self, meshname):
			object = Blender.Object.Get(meshname)
			mesh = Blender.NMesh.GetRaw(object.data.name)
			n_vtx = len(mesh.verts)
			index = 0
			for v in mesh.verts:
				for vj in self.joinvtxs:
					if (self.is_dist_lt_tol(tuple([v.co[0], v.co[1], v.co[2]]), vj)):
						#mesh.verts[index] = tuple([[vj[0], vj[1], vj[2]]])
						print "%d : (%1.3f,%1.3f,%1.3f)" % (index, v[0], v[1], v[2])						
						v.co[0] = vj[0]
						v.co[1] = vj[1]
						v.co[2] = vj[2]
				index = index + 1
			mesh.update()
			#Blender.NMesh.PutRaw(mesh, object.data.name, 0) 



	def is_dist_lt_tol(self, v, vj):
		dx = v[0] - vj[0]
		dy = v[1] - vj[1]
		dz = v[2] - vj[2]
		d = math.sqrt( (dx * dx) + (dy * dy) + (dz * dz) ) 
		return (d < self.tolerance)


# perform alignment on my meshes ( adjust these manually until interactive version written) 

align = MeshEdgeAlign("Join_Face",0.16)		 # initialise class with ME:Join_Face as the template and 0.16 as the tolerance	 
scene = Blender.Scene.GetCurrent()
align.use_mesh_series("LowerFace",range(1,9))    # add Me:LowerFace.001 to ME:LowerFace.008 to list 
align.use_mesh_series("Forehead",range(1,4))	 # add ME:ForeHead.001 to ME:ForeHead.003 to list	
align.list_meshes_used()			 # debugging sanity check, lists to console 
align.test_all_meshes()				 # performs alignment


Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests