Notifications
Clear all

[Closed] Skin Weight code optimization

 MZ1

“(k/BoneArray.count)” this is trick to update only last one? Nice!

 MZ1

I just updated my code with your suggestion:

(
	delete objects
-- 	clearlistener()
	gc()
	Time_Start = timestamp()
	Memory_Start = heapfree
	
	/* Functions */
	
	fn PrepareModifier TheModifier =
	(
		Obj = (refs.dependentNodes TheModifier)[1]
		if isvalidnode Obj do
		(
			setCommandPanelTaskMode mode:#modify
			select Obj
			if modPanel.getCurrentObject() != TheModifier do modPanel.setCurrentObject TheModifier
		)
	)
	
	fn GetBoneIndex SkinModifier TheBone = 
	(
		if isvalidnode TheBone then
		(
			findItem (for i =1 to (skinOps.GetNumberBones SkinModifier) collect (skinOps.GetBoneName SkinModifier i 0)) TheBone.name
		)
		else 0
	)
	
	/* Localizing SkinOps */
	
	local GetNumberVertices = skinOps.GetNumberVertices
	local GetVertexWeightCount = skinOps.GetVertexWeightCount
	local GetVertexWeight = skinOps.GetVertexWeight
	local GetVertexWeightBoneID = skinOps.GetVertexWeightBoneID
	local SetVertexWeights = skinOps.SetVertexWeights
	local ReplaceVertexWeights = skinOps.ReplaceVertexWeights
	local SelectVertices = skinOps.SelectVertices
	local SelectBone = skinOps.SelectBone
	local AddBone = skinOps.addbone	
	local RemoveBone = skinOps.RemoveBone
	local BlendSelectedBone = skinOps.blendSelected
	local GetNumberBones = skinOps.GetNumberBones
	local GetBoneName = skinOps.GetBoneName
	
	/* Default Variables */
	
	Width = 100.0
	Height = 100.0
	WidthSegments = 4
	HeightSegments = 4
	WidthDistance = Width/WidthSegments
	HeightDistance = Height/HeightSegments

	/* Skin Object */
	
	Obj = Rectangle name:"Skin Object" length:Height width:Width transform:(matrix3 [1,0,0] [0,0,1] [0,-1,0] [Width/2,0,Height/2]) wirecolor:blue
	GarmentModifier = Garment_Maker autoMesh:true preserve:true relax:true outputType:0 figure:undefined stretchMapping:false showMesh:false showSeams:false
	addmodifier Obj GarmentModifier
	SkinModifier = skin()
	addmodifier Obj SkinModifier
	
	/* Bones */
	
	BoneArray = #()
	for i = 1 to WidthSegments + 1 do
	(
		for j = 1 to HeightSegments + 1 do
		(
			append BoneArray (sphere name:("Bone_"+(i as string)+"_"+(j as string)) radius:5 segments:6 pos:[(i-1)*WidthDistance,0,(j-1)*HeightDistance] wirecolor:yellow)
		)
	)
	
	/* Skin */
	
	PrepareModifier SkinModifier
	for TheBone in BoneArray do AddBone SkinModifier TheBone 0
	classof (refs.dependentnodes SkinModifier)[1]
	
	/* Skin Weights */
	
	WDis = WidthDistance/2
	HDis = HeightDistance/2
	NumberVertices = GetNumberVertices SkinModifier
	BonesTrueIndexes = #()
	BonePoses = #()
	for TheBone in BoneArray do
	(
		append BonesTrueIndexes (GetBoneIndex SkinModifier TheBone)
		append BonePoses TheBone.pos
	)
	ObjMesh = Obj.mesh
	ObjTransform  = Obj.objecttransform
	for i = 1 to NumberVertices do
	(
		VertexPos = getvert ObjMesh i * ObjTransform
		Val = [ VertexPos.x - WDis, VertexPos.x + WDis, VertexPos.z - HDis, VertexPos.z + HDis ]
		for BoneId = 1 to BoneArray.count do
		(
			BonePos = BonePoses[BoneId]
			if BonePos.x >= Val[1] and BonePos.x < Val[2] and BonePos.z >= Val[3] and BonePos.z < Val[4] do
			(
				ReplaceVertexWeights SkinModifier i BonesTrueIndexes[BoneId] 1
			)
		)
	)
	free ObjMesh
	
	/* Blend Weights */
	
	SelectVertices SkinModifier #{1..NumberVertices}
	for i = 1 to BoneArray.count do
	(
		SelectBone SkinModifier i
		BlendSelectedBone SkinModifier
	)
	
	/* End */
	
	Time_End = timestamp()
	Memory_End = heapfree
	format "Time:% Memory:%\n" ((Time_End - Time_Start)/1000.0) (Memory_Start - Memory_End)
)
 MZ1

It seems it takes more memory:

	for BoneId = 1 to BoneArray.count do
	(
		BonePos = BonePoses[BoneId]
		if BonePos.x >= Val[1] and BonePos.x < Val[2] and BonePos.z >= Val[3] and BonePos.z < Val[4] do
		(
			ReplaceVertexWeights SkinModifier i BonesTrueIndexes[BoneId] 1
		)
	)

Time:0.348 Memory:143476L


	Index_Array = #()
	Weight_Array = #()
	for BoneId = 1 to BoneArray.count do
	(
		BonePos = BonePoses[BoneId]
		if BonePos.x >= Val[1] and BonePos.x < Val[2] and BonePos.z >= Val[3] and BonePos.z < Val[4] do
		(
			append Index_Array BonesTrueIndexes[BoneId]
			append Weight_Array 1
		)
	)
	if Index_Array.count > 0 do ReplaceVertexWeights SkinModifier i Index_Array Weight_Array

Time:0.334 Memory:253656L


Page 2 / 2