-
Notifications
You must be signed in to change notification settings - Fork 0
/
mr_tempPin_create_follow_locator.py
120 lines (102 loc) · 4.24 KB
/
mr_tempPin_create_follow_locator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
"""
# ------------------------------------------------------------------------------ #
# SCRIPT: mr_tempPin_create_follow_locator.py
# VERSION: 0003
#
# CREATORS: Maria Robertson
# ---------------------------------------
#
# ---------------------------------------
# DESCRIPTION:
# ---------------------------------------
# For each selected object, create a locator inside an offset group that follows it.
# The locator can be freely placed while still following the original control.
#
# EXAMPLE USES:
# ---------------------------------------
# - Create follow locators, to be tagged as pivots with other scripts, such as mr_tempPin_pivotFromSelectionSet.py.
# - e.g. in order to easily set a particular point on a foot as a pivot.
#
# ---------------------------------------
# RUN COMMAND:
# ---------------------------------------
import importlib
import mr_tempPin_create_follow_locator
importlib.reload(mr_tempPin_create_follow_locator)
mr_tempPin_create_follow_locator.main()
# ---------------------------------------
# WISH LIST:
# ---------------------------------------
# - Be able to create follow locators for vertices. Explore using rivets.
#
# ---------------------------------------
# CHANGELOG:
# ---------------------------------------
# 2023-12-18 - 0003:
# - Converting original MEL script to Python.
#
# 2023-04-10 - 0002:
# - Minor edits.
#
# 2023-03-29 - 0001:
# - First pass.
# ------------------------------------------------------------------------------ #
"""
import maya.cmds as cmds
def main():
# -------------------------------------------------------------------
# 01.INITIALISE VARIABLES.
# -------------------------------------------------------------------
sel = cmds.ls(selection=True)
if not sel:
cmds.warning("Please select one or more objects to create a follow locator for.")
return
attr_to_lock = ["translateX", "translateY", "translateZ", "rotateX", "rotateY", "rotateZ", "v"]
attr_to_lock_and_hide = ["scaleX", "scaleY", "scaleZ"]
names_to_check = ["_offset", "_follow_loc"]
# -------------------------------------------------------------------
# 01. CHECK IF ANY SELECTED OBJECTS ARE EXISTING OFFSET GROUPS OR FOLLOW LOCATORS.
# -------------------------------------------------------------------
for obj in sel:
if any(name in obj for name in names_to_check):
cmds.warning("Please select objects that are not an offset group or follow locator.")
return
# -------------------------------------------------------------------
# 01. CREATE A FOLLOW LOCATOR.
# -------------------------------------------------------------------
follow_locators = []
for obj in sel:
# Check if a follow locator already exists.
loc = obj + "_follow_loc"
if cmds.objExists(loc):
cmds.warning(f"{loc} already exists.")
return
cmds.spaceLocator(name=loc)[0]
cmds.setAttr(loc + ".localScale", 18, 18, 18)
follow_locators.append(loc)
# Hide unrelated attributes.
formatted_attrs = [f"{loc}.{attr}" for attr in attr_to_lock_and_hide]
for attr in formatted_attrs:
cmds.setAttr(attr, lock=True, keyable=False)
loc_vis = loc + ".visibility"
cmds.setAttr(loc_vis, lock=True, keyable=False)
# -------------------------------------------------------------------
# 02. CREATE AN OFFSET GROUP.
# -------------------------------------------------------------------
offset = loc + "_offset"
# If an offset group already exists, just add the locator to it.
if cmds.objExists(offset):
cmds.parent(loc, offset)
else:
cmds.group(loc, name=offset)
cmds.parentConstraint(obj, offset)
# Hide unrelated attributes.
formatted_attrs = [f"{offset}.{attr}" for attr in attr_to_lock_and_hide]
for attr in formatted_attrs:
cmds.setAttr(attr, lock=True, keyable=False)
# Lock main attributes.
formatted_attrs = [f"{offset}.{attr}" for attr in attr_to_lock]
for attr in formatted_attrs:
cmds.setAttr(attr, lock=True)
# End with follow locators selected.
cmds.select(follow_locators)