In all honesty, you’ve got alot going on there written in a way that will make it more difficult for you to understand/follow.
My first suggestion is to break down what is going on and rewrite it slightly to make it easier to read. Doing this frequently will absolutely help you learn scripting - its still one of the best learning techniques I use everyday.
fol = mc.createNode("follicle")
mc.setAttr(fol + ".visibility", 0)
temp_fol = mc.listRelatives(fol, p=True)[0]
fols_tr.append(mc.rename(temp_fol, "{}follicle_{:02d}".format(prefix, x+1)))
fols.append(mc.listRelatives(fols_tr[-1], s=True)[0])
Here, you’re creating a follicle, hiding it, getting its transform, renaming it (adding the new name to a list) and finally renaming the follicle (and adding that to another list).
The rest of the script is then referring to the last item in the list, which will just make it more confusing for you to read.
try this instead;
follicle = mc.createNode("follicle")
fols.append(follicle)
follicle_transform = mc.listRelatives(follicle, parent=True)[0]
follicle_transform = mc.rename(follicle, "{}follicle_{:02d}".format(prefix, x + 1))
fols_tr.append(follicle_transform)
mc.setAttr(follicle + ".visibility", 0)
With this, you can now refer to the renamed follicle parent as follicle_transform instead of fols_tr[-1].
For your renaming part, I see you’re dumping the list of follicle names you have and then relying on selections. This can be ok to start with but will soon drive you mad.
Instead, just rename them in the same loop.
...
mc.parent(follicle_transform, follicles_grp)
tx = mc.getAttr(follicle_transform + '.translateX')
if abs(tx) < 0.02:
follicle_transform = mc.rename(follicle_transform, follicle_transform.replace("up_rb", "mid_up_rb"))
elif tx < 0:
follicle_transform = mc.rename(follicle_transform, follicle_transform.replace("up_rb", "r_up_rb"))
else:
follicle_transform = mc.rename(follicle_transform, follicle_transform.replace("up_rb", "l_up_rb"))
# create final bind joints on the surface
joint = mc.createNode("joint", n="{}{:d}_jnt".format(prefix, x + 1))
bind_jnts.append(joint)
mc.setAttr(joint + ".radius", bnd_joints_rad)
mc.parent(joint, follicle_transform, r=True)
We’re using the str.replace() method to rename the follicle with a new follicle name.
We’re also using the abs method I mentioned earlier to accept translateX values from -0.002 to 0.002 as the “middle”.
I hope this helps 