Script Wish: Rename selected object(s) to match the name to the nearest other object

Script Wish: Rename selected object(s) to match the name to the nearest other object

Haider_of_Sweden
Collaborator Collaborator
994 Views
8 Replies
Message 1 of 9

Script Wish: Rename selected object(s) to match the name to the nearest other object

Haider_of_Sweden
Collaborator
Collaborator

After watching this video, I had an idea for a script.
https://youtu.be/G47NnPXsPHM?list=PLig7uFYLdEk44biA_p5Mip8F6jBkVNtkw&t=345

In this video, we need to pair bones to their counterpart. Although there is a 'Match by name' function available, it requires the bones to have identical names, which may not always be the case.

 

  • The new bones are aligned to the same place as the old bones by the user.
  • The script would align new bones with their counterparts in the same position as the old bones.
  • It would search for the nearest other object within a specified XYZ threshold, read its name, and apply it to the selected object.
  • If multiple objects are selected, the script would repeat this process for each object.

 

A safety measure is to pick the first other-object's name, in case more than one object is located there, and prints a message "object X has more objects nearby, check manually" to make the user aware.

 

Thanks for your consideration

0 Likes
995 Views
8 Replies
Replies (8)
Message 2 of 9

denisT.MaxDoctor
Advisor
Advisor

I think that for those who manually create skeletons and align all the bones, it wouldn't be a big problem to specify matching pairs by picking, as the author of the video does.

😅

0 Likes
Message 3 of 9

Haider_of_Sweden
Collaborator
Collaborator

precisely, and hence the script wish 🥰

0 Likes
Message 4 of 9

denisT.MaxDoctor
Advisor
Advisor

@Haider_of_Sweden wrote:

precisely, and hence the script wish 🥰


Well, if you really wish to, make an example scene and post the file.

0 Likes
Message 5 of 9

denisT.MaxDoctor
Advisor
Advisor

Finding the most accurately aligned pair of objects is a non-trivial task. The problem is that at the same position we may have completely different matrix orientations as well as scales. For supporting mirror animating, most custom and built-in skeleton systems (CAT, Biped) have flip and mirror transformations to ensure that the skeleton works correctly in mirror cases. It can be different from the target (FBX) skeleton, and in most cases is different.

0 Likes
Message 6 of 9

denisT.MaxDoctor
Advisor
Advisor

Of course, most modern skeletons have bone lengths along the X-axis, but this is more of a probable assumption than an unambiguous condition. We can also ignore the scale, the use of which is considered an unfortunate solution and is not welcomed in practice.
So we can look for correspondence by two quantities - position (distance) and direction (in our case angle between X-axis).

0 Likes
Message 7 of 9

Haider_of_Sweden
Collaborator
Collaborator

@denisT.MaxDoctor wrote:


Well, if you really wish to, make an example scene and post the file.


I provided an example scene, which I think shows that what I am looking for might be easier as anticipated.

We do not need to care about the objects rotation or scale. We are only looking for their names.

 

The objects under "Need renaming" are to be renamed to the same name as the object closest to it.

I put intentionally rotated and scaled objects (Source nodes), for the sake of having them there, but we only need their names.

 

I also put double objects, Pyramid002 and Pyramid_root at the exact same spot. We can let the script pick the first one it found, but give us a printed text to notify us that "Pyramid_root (or whichever got detected first, and used as source for the object name) had another object next to it, please validate manually"

 

Haider_of_Sweden_0-1711646293302.png

 

 

I hope this makes better sense now.

Thanks 🙂 

 

0 Likes
Message 8 of 9

denisT.MaxDoctor
Advisor
Advisor

@Haider_of_Sweden wrote:

@denisT.MaxDoctor wrote:


Well, if you really wish to, make an example scene and post the file.


I provided an example scene, which I think shows that what I am looking for might be easier as anticipated.

We do not need to care about the objects rotation or scale. We are only looking for their names.

 


something like this:

fn getBoneDistance source target dir:#x = 
(
	i = case dir of
	(
		#x: 1 
		#y: 2 
		#z: 3 
   default: 1
	)
	
	d = distance source target
	a = 1 - abs (dot (normalize source.transform[i]) (normalize target.transform[i]))
		
	d*d + a*a
)

fn findClosestBone bone targets: exclude: dir:#x unique:off = 
(
	if targets == unsupplied do targets = objects as array
	if exclude == unsupplied do exclude = #(bone)
	
	xdist = 1e9
	target = undefined
	for t in targets where finditem exclude t == 0 and (d = getBoneDistance bone t dir:dir) < xdist do
	(
		xdist = d
		target = t
	)
	if unique do 
	(
		if (k = finditem targets target) do deleteitem targets k
	)
	#(target, xdist)
)

sources = for obj in objects where iskindof obj Pyramid collect obj 
targets = for obj in objects where iskindof obj Box collect obj 

pairs = for b in sources collect #(b, findClosestBone b targets:targets exclude:sources unique:off)


for p in pairs do format "source:%\n\t>> %\n\tdistance:%\n" p[1].name (if isvalidnode p[2][1] do p[2][1].name) p[2][2]
0 Likes
Message 9 of 9

denisT.MaxDoctor
Advisor
Advisor

Another objective value is bone length, essentially the distance to the first child. It, too, can be used as a comparable parameter for determining "bone distance" (difference).

0 Likes