optimization…
first of all we have to understand that optimization equals organization.
Shorter code doesn’t mean better!
We must organize our code in such a way as not to repeat a part of the code if it is changed, and to make it simple and clear in the case of an extension.
this is how i do my tools, and how i learn my students:
global TOSYK_CharacterRig_Setup
(
struct CharacterRig_Struct
(
InitBone =
(
struct InitBone
(
private
tm,
public
name,
origin = [0,0,0],
node,
fn getNode = getnodebyname name,
fn setup = if isvalidnode node do node.transform = tm * node.transform,
fn backup = if isvalidnode node do node.transform = inverse tm * node.transform,
fn update =
(
node = getnodebyname name
tm = (eulerangles origin[1] origin[2] origin[3]) as matrix3
),
on create do
(
update()
)
)
),
BoneGroup =
(
struct BoneGroup (name, bones)
),
private
shoulder_origin = [-20,5,25],
elbow_origin = [0,0,20],
public
bone_groups =
#(
BoneGroup name:#shoulder bones:
#(
InitBone name:#j_shoulder_ri origin:shoulder_origin,
InitBone name:#j_shoulder_lo origin:shoulder_origin
),
BoneGroup name:#elbow bones:
#(
InitBone name:#j_elbow_ri origin:elbow_origin,
InitBone name:#j_elbow_bulge_ri origin:elbow_origin,
InitBone name:#j_elbow_le origin:elbow_origin,
InitBone name:#j_elbow_bulge_le origin:elbow_origin
)
-- all other bone groups ...
),
mapped fn update_bone bone = bone.update(),
mapped fn setup_bone bone = bone.setup(),
mapped fn backup_bone bone = bone.backup(),
fn update =
(
for bg in bone_groups do update_bone bg.bones
),
fn setup =
(
for bg in bone_groups do setup_bone bg.bones
),
fn backup =
(
for bg in bone_groups do backup_bone bg.bones
),
dialog =
(
rollout dialog "TOSYK Rig" width:200
(
local owner = if owner != undefined do owner
group "Edit: "
(
button pose_all_bt "Pose All Bones" width:182 align:#left offset:[-4,0] tooltip:"Setup All Bones\n RC\t- Backup"
)
button update_all_bt "Update Rig Data" width:182 align:#left offset:[-4,4] tooltip:"Update Rig Data"
on pose_all_bt pressed do undo "Setup" on
(
if isstruct owner do owner.setup()
)
on pose_all_bt rightclick do undo "Beckup" on
(
if isstruct owner do owner.backup()
)
fn makeColorByName name hash:1 =
(
maxops.colorById (getHashValue name hash) &c
c
)
group "Debug: "
(
button create_test_bt "Create Test" width:182 align:#left offset:[-4,0] tooltip:"Create Test"
)
on create_test_bt pressed do undo "Create Test" on if isstruct owner do
(
delete objects
for bg in owner.bone_groups do
(
col = makeColorByName (bg.name as string)
for node in bg.bones do
(
point name:node.name axis:on pos:(random [100,100,100] -[100,100,100]) wirecolor:col
)
)
owner.update()
)
on dialog close do
(
)
on dialog open do
(
)
)
),
fn destroy_dialog =
(
try (destroydialog ::TOSYK_CharacterRig_Setup.dialog) catch()
),
fn create_dialog =
(
destroy_dialog()
createdialog dialog
),
on create do
(
destroy_dialog()
update()
dialog.owner = this
)
)
global _cs = TOSYK_CharacterRig_Setup = CharacterRig_Struct()
ok
)
/*
_cs.create_dialog()
*/
The DEBUG part is really not needed for your final code (tool). But it’s easier to make a simple scene for debugging complex solutions. (in my case, I had nothing to debug the code, so I do a test that covers the all needs to implement the idea). You can remove or comment this part of code
Ask questions if don’t understand any of my code solutions. Of course if you want to learn instead of just use it…