Help weight transfer script

Help weight transfer script

davinwest72
Participant Participant
4,474 Views
24 Replies
Message 1 of 25

Help weight transfer script

davinwest72
Participant
Participant

Edit. Solved.

0 Likes
4,475 Views
24 Replies
Replies (24)
Message 21 of 25

davinwest72
Participant
Participant

Edit. Solved.

0 Likes
Message 22 of 25

davinwest72
Participant
Participant

Edit. Solved.

0 Likes
Message 23 of 25

denisT.MaxDoctor
Advisor
Advisor

I don't believe anyone wants to debug your code. If you have error messages, please post them... make a test scene for those who want to reproduce the problem... describe all the steps on how to reproduce the problem...

Below is a working example and how to present the discussion material. The right way to ask a question will give you a better chance of getting a helpful answer.

 

 

fn mapNearestVerts source target = 
(
	fn findNearest p verts = 
	(
		dist = distance p verts[1]
		n = 1
		for k=2 to verts.count where (d = distance p verts[k]) < dist do
		(
			dist = d
			n = k
		)
		n
	)
	
	source_mesh = snapshotasmesh source
	target_mesh = snapshotasmesh target
	
	source_map = #()
	numv = source_map.count = source_mesh.numverts
	
	target_verts = for v=1 to target_mesh.numverts collect (getvert target_mesh v)
	
	for v=1 to numv do source_map[v] = findNearest (getvert source_mesh v) target_verts 
	source_map
)

fn copySkinData node = 
(
	sk = node.modifiers[skin]
	if iskindof sk Skin do
	(
		max modify mode
		modpanel.setcurrentobject sk
		num = skinops.getnumberbones sk
		bones = for k=1 to num collect 
		(
			skinops.getbonename sk k 0
		)
		weights = for k=1 to skinops.getnumbervertices sk collect
		(
			num = skinops.getvertexweightcount sk k 
			
			_bones = #()
			_names = #()
			_weights = #()
			
			for i=1 to num do
			(
				id = skinops.getvertexweightboneid sk k i
				name = skinops.getbonename sk id 0
				weight = skinops.getvertexweight sk k i
				
				append _bones id 
				append _names name
				append _weights weight 
			)
			
			#(k, _bones, _names, _weights)
		)
		
		#(bones, weights)
	)
)

fn pasteSkinData node data map: = 
(
	sk = node.modifiers[skin]
	if iskindof sk Skin do
	(
		max modify mode
		modpanel.setcurrentobject sk
		
		bones = data[1]
		num = bones.count
		for k=1 to num do
		(
			if isvalidnode (bone = getnodebyname bones[k]) do 
			(
				skinops.addbone sk bone 1
			)
		)
		skinOps.Invalidate sk 1
		
		weights = data[2]
		numv = skinops.getnumbervertices sk
		for k=1 to numv do
		(
			n = if map != unsupplied then map[k] else k
			_d = weights[n]
			
			_bones = for b in _d[3] collect (finditem bones b)
			skinops.replacevertexweights sk k _bones _d[4]    
		)
		#(skinops.getnumberbones sk, skinops.getnumbervertices sk)
	)
)




delete objects
/********** create sample geometry *****************/

t = Cylinder name:#target radius:10 height:100 sides:8 heightsegs:8 isSelected:on wirecolor:orange
s = Capsule name:#source radius:12 height:100 heighttype:0 sides:12 heightsegs:24 smooth:on wirecolor:green


/*************** make skeleton *********************/
parent = dummy name:#bone_00 boxsize:[30,30,30]
sk_bones = #(parent)
for k=1 to 4 do in parent 
(
	parent = dummy name:(uniquename parent.name) boxsize:[30,30,30] pos:[0,0,k*25]
	append sk_bones parent
)

/********** apply skin for target ******************/
(
	max modify mode
	sk = Skin()
	addmodifier t sk
	modpanel.setcurrentobject sk
		
	for bone in sk_bones do
	(
		skinops.addbone sk bone 1
	)
)

/********** create test animation ******************/
with animate on 
(
	at time 25  for k=2 to sk_bones.count do in coordsys local (rotate sk_bones[k] (eulerangles 0 40 0))
	at time 75  for k=2 to sk_bones.count do in coordsys local (rotate sk_bones[k] (eulerangles 0 -80 0))
	at time 100 for k=2 to sk_bones.count do in coordsys local (rotate sk_bones[k] (eulerangles 0 40 0))
)


/********** find nearest verts *********************/
source_map = 
(
	format "source:% target:%\n" (s.mesh.numverts) (t.mesh.numverts)

	t0 = timestamp()
	h0 = heapfree
	
	mm = mapNearestVerts s t

	format "\ttime:% heap:% count:%\n" (timestamp() - t0) (h0 - heapfree) source_map.count
	
	mm
)

/********** copy target skin data ******************/
data = copySkinData t

/********** apply skin for source ******************/
select s
max modify mode
addmodifier s (Skin())

/************* transfer skin data ******************/
pasteSkinData s data map:source_map

 

 

0 Likes
Message 24 of 25

davinwest72
Participant
Participant

Edit. Solved.

0 Likes
Message 25 of 25

denisT.MaxDoctor
Advisor
Advisor

@davinwest72 wrote:

... Does anyone know why I am receiving this error? 


What is the error? The system displays an error message, the debugger points to the error string... Can you show it? A screenshot usually works well.

How do you reproduce the error?
Run the code? But we need an example (or a test scene).

 

 

0 Likes