1/******************************************************************************
2	NintendoWare for CTR Maya Plugin
3
4	File:         NW4C_SetCombineGroup.mel
5	Description:  set combine group
6	Date:         2010/07/01
7	Author:       Naomasa Ariki
8
9	Copyright (C)2009-2010 Nintendo Co., Ltd. / HAL Laboratory, Inc. All rights reserved.
10******************************************************************************/
11
12// UpdateWindow DoSearch
13
14/******************************************************************************
15	is scene anim object
16******************************************************************************/
17proc int IsSceneAnimObject(string $node)
18{
19	string $type = nodeType($node);
20	string $childs[] = `listRelatives -pa -s $node`;
21	if (size(`ls -cameras -lights -typ environmentFog $childs`) > 0 ||
22		$type == "lookAt")
23	{
24		return true;
25	}
26	return false;
27}
28
29/******************************************************************************
30	get selected transform (except camera & light)
31
32		return node size
33******************************************************************************/
34proc int GetSelectedXform(string $xforms[])
35{
36	clear($xforms);
37
38	string $nodes[] = `ls -sl -typ transform`;
39	for ($node in $nodes)
40	{
41		if (!IsSceneAnimObject($node))
42		{
43			$xforms[size($xforms)] = $node;
44		}
45	}
46
47	return size($xforms);
48}
49
50/******************************************************************************
51	combine group
52******************************************************************************/
53global proc int nw4cSetCombineGroup_Get_UseCombineGroup(string $node)
54{
55	return (`attributeQuery -n $node -ex "nw4cUseCombineGroup"`) ?
56		`getAttr ($node + ".nw4cUseCombineGroup")` : 0;
57}
58
59global proc nw4cSetCombineGroup_Add_UseCombineGroup(string $node)
60{
61	if (!`attributeQuery -n $node -ex "nw4cUseCombineGroup"`)
62	{
63		addAttr -ln "nw4cUseCombineGroup" -at "bool" -dv 0 -h 1 $node;
64	}
65}
66
67global proc nw4cSetCombineGroup_Set_UseCombineGroup(string $node, int $val)
68{
69	nw4cSetCombineGroup_Add_UseCombineGroup($node);
70	setAttr ($node + ".nw4cUseCombineGroup") $val;
71}
72
73proc SetJob_UseCombineGroup(string $node)
74{
75	if (!`attributeQuery -n $node -ex "nw4cUseCombineGroup"`)
76	{
77		return;
78	}
79	scriptJob -p nw4cSetCombineGroup_UseCombineGroup -rp -ac ($node + ".nw4cUseCombineGroup")
80		("if (`nw4cSetCombineGroup_Get_UseCombineGroup " + $node +
81		 "`) radioButtonGrp -e -sl 1 nw4cSetCombineGroup_UseCombineGroup;" +
82		 "else radioButtonGrp -e -sl 1 nw4cSetCombineGroup_DontCare;" +
83		 "control -e -en `nw4cSetCombineGroup_Get_UseCombineGroup " + $node + "` nw4cSetCombineGroup_CombineGroup;");
84}
85
86global proc nw4cSetCombineGroup_UseCombineGroupCB(string $mainNode)
87{
88	string $xforms[];
89	GetSelectedXform($xforms);
90
91	int $val = `radioButtonGrp -q -sl nw4cSetCombineGroup_UseCombineGroup`;
92	control -e -en $val nw4cSetCombineGroup_CombineGroup;
93	control -e -en $val nw4cSetCombineGroup_SetSequentialBtn;
94
95
96	for ($node in $xforms)
97	{
98		nw4cSetCombineGroup_Set_UseCombineGroup($node, $val);
99	}
100	SetJob_UseCombineGroup($mainNode);
101}
102
103/******************************************************************************
104	combine group number
105******************************************************************************/
106global proc int nw4cSetCombineGroup_Get_CombineGroup(string $node)
107{
108	return (`attributeQuery -n $node -ex "nw4cCombineGroup"`) ?
109		`getAttr ($node + ".nw4cCombineGroup")` : 0;
110}
111
112global proc nw4cSetCombineGroup_Add_CombineGroup(string $node)
113{
114	if (!`attributeQuery -n $node -ex "nw4cCombineGroup"`)
115	{
116		addAttr -ln "nw4cCombineGroup" -at "short" -min 0 -max 255 -dv 0 -h 1 $node;
117	}
118}
119
120global proc nw4cSetCombineGroup_Set_CombineGroup(string $node, int $val)
121{
122	nw4cSetCombineGroup_Add_CombineGroup($node);
123	setAttr ($node + ".nw4cCombineGroup") $val;
124}
125
126proc SetJob_CombineGroup(string $node)
127{
128	if (!`attributeQuery -n $node -ex "nw4cCombineGroup"`)
129	{
130		return;
131	}
132	scriptJob -p nw4cSetCombineGroup_CombineGroup -rp -ac ($node + ".nw4cCombineGroup")
133		("intField -e -v (`nw4cSetCombineGroup_Get_CombineGroup "
134			+ $node + "`) nw4cSetCombineGroup_CombineGroup");
135}
136
137global proc nw4cSetCombineGroup_CombineGroupCB(string $mainNode)
138{
139	string $xforms[];
140	GetSelectedXform($xforms);
141
142	int $val = `intField -q -v nw4cSetCombineGroup_CombineGroup`;
143	if ($val < 0)
144	{
145		$val = 0;
146		intField -e -v $val nw4cSetCombineGroup_CombineGroup;
147	}
148	else if ($val > 255)
149	{
150		$val = 255;
151		intField -e -v $val nw4cSetCombineGroup_CombineGroup;
152	}
153
154	for ($node in $xforms)
155	{
156		nw4cSetCombineGroup_Set_CombineGroup($node, $val);
157	}
158
159	SetJob_CombineGroup($mainNode);
160}
161
162/******************************************************************************
163	update window
164******************************************************************************/
165
166global proc nw4cSetCombineGroup_UpdateWindow()
167{
168	//-----------------------------------------------------------------------------
169	// get selection
170	string $xforms[];
171	GetSelectedXform($xforms);
172	int $enableFlag = (size($xforms) > 0);
173
174	//-----------------------------------------------------------------------------
175	// set node name
176	string $nodesName;
177	if ($enableFlag)
178	{
179		$nodesName = $xforms[0];
180		int $nodeSize = size($xforms);
181		for ($inode = 1; $inode < $nodeSize; ++$inode)
182		{
183			if ($inode == 3)
184			{
185				$nodesName += ", ... (" + $nodeSize + ")";
186				break;
187			}
188			$nodesName += ",  " + $xforms[$inode];
189		}
190	}
191	else
192	{
193		$nodesName = "(None)";
194	}
195	text -e -l $nodesName nw4cSetCombineGroup_NodeName;
196
197	//-----------------------------------------------------------------------------
198	// set current attr & change command
199	int $useCombineGroup = 0;
200	if ($enableFlag)
201	{
202		string $node = $xforms[0];
203
204		$useCombineGroup =
205			`nw4cSetCombineGroup_Get_UseCombineGroup $node`;
206		radioButtonGrp -e -sl (!$useCombineGroup)
207			-on1 ("nw4cSetCombineGroup_UseCombineGroupCB " + $node)
208			nw4cSetCombineGroup_DontCare;
209		radioButtonGrp -e -sl $useCombineGroup
210			-on1 ("nw4cSetCombineGroup_UseCombineGroupCB " + $node)
211			nw4cSetCombineGroup_UseCombineGroup;
212
213		intField -e -v `nw4cSetCombineGroup_Get_CombineGroup $node`
214			-cc ("nw4cSetCombineGroup_CombineGroupCB " + $node)
215			nw4cSetCombineGroup_CombineGroup;
216
217		// setAttr �ȂǂŕύX���ꂽ�ꍇ�ɁA
218		// �R���g���[���̒l���X�V���邽��
219		SetJob_UseCombineGroup($node);
220		SetJob_CombineGroup($node);
221	}
222
223	//-----------------------------------------------------------------------------
224	// set enable state
225	control -e -en $enableFlag nw4cSetCombineGroup_DontCare;
226	control -e -en $enableFlag nw4cSetCombineGroup_UseCombineGroup;
227	control -e -en ($enableFlag && $useCombineGroup) nw4cSetCombineGroup_CombineGroup;
228	control -e -en $enableFlag nw4cSetCombineGroup_Description;
229	control -e -en ($enableFlag && $useCombineGroup) nw4cSetCombineGroup_SetSequentialBtn;
230}
231
232/******************************************************************************
233	get hierarchy level
234******************************************************************************/
235proc int GetXformHierarchyLevel(string $node)
236{
237	string $buffer[];
238	string $longName = `longNameOf($node)`;
239	return `tokenize $longName "|" $buffer`;
240}
241
242
243/******************************************************************************
244	sort xforms
245******************************************************************************/
246proc SortXforms(string $xforms[], string $parents[], int $levels[])
247{
248	int $xfSize = size($xforms);
249	for ($isg = 0; $isg < $xfSize - 1; ++$isg)
250	{
251		for ($jsg = $isg + 1; $jsg < $xfSize; ++$jsg)
252		{
253			string $xf0   = $xforms[$isg];
254			string $prnt0 = $parents[$isg];
255			int $lv0      = $levels[$isg];
256			string $xf1   = $xforms[$jsg];
257			string $prnt1 = $parents[$jsg];
258			int $lv1      = $levels[$jsg];
259			int $cond     = 0;
260			int $cmp;
261
262			if ($lv0 == $lv1)
263			{
264				// ���x���������Ȃ玟�͐e�̖��O�Ń\�[�g����
265				$cmp = strcmp($prnt0, $prnt1);
266				if ($cmp == 0)
267				{
268					// �e�̖��O�������Ȃ�m�[�h���Ń\�[�g����
269					$cmp = strcmp($xf0, $xf1);
270				}
271				$cond = ($cmp > 0);
272			}
273			else
274			{
275				$cond = ($lv0 > $lv1);
276			}
277
278			if ($cond)
279			{
280				$xforms[$isg]  = $xf1;
281				$parents[$isg] = $prnt1;
282				$levels[$isg]  = $lv1;
283				$xforms[$jsg]  = $xf0;
284				$parents[$jsg] = $prnt0;
285				$levels[$jsg]  = $lv0;
286			}
287		}
288	}
289}
290
291
292
293
294
295
296/******************************************************************************
297	�O���[�v�ԍ���A���I�ɐݒ肷��
298******************************************************************************/
299global proc nw4cSetCombineGroup_SetSequential()
300{
301	string $xforms[];
302	string $parents[];
303	int $hierarchyLevels[];
304	int $numXforms;
305	int $i;
306
307	GetSelectedXform($xforms);
308	$numXforms = size($xforms);
309
310	// �O���[�v�ԍ����擾����
311	int $val = `intField -q -v nw4cSetCombineGroup_CombineGroup`;
312
313	// �擾�����O���[�v�ԍ��� xform �̐������� 255 ������΃G���[�Ƃ���
314	if (($val+$numXforms) <= 255)
315	{
316		// �e xform �̐e�m�[�h�ƊK�w�\���̃��x�����擾����
317		for ($i=0; $i<$numXforms; $i++)
318		{
319			$parents[$i] = `firstParentOf $xforms[$i]`;
320			$hierarchyLevels[$i] = `GetXformHierarchyLevel $xforms[$i]`;
321		}
322
323		// xform ���\�[�g����
324		// �K�w�\���̃��x��, �e�m�[�h�̖��O, �m�[�h�̖��O�̏��ԂŃ\�[�g����
325		SortXforms($xforms, $parents, $hierarchyLevels);
326
327		print("NW4C_SetCombineGroup: Set sequaltial\n");
328		for ($i=0; $i<$numXforms; $i++)
329		{
330			print("  " + $xforms[$i] + ": combine group " + ($val + $i) + "\n");
331			nw4cSetCombineGroup_Set_CombineGroup($xforms[$i], ($val + $i));
332		}
333	}
334	else
335	{
336		error "The combine group number exceeds the limitation (255).\n";
337	}
338}
339
340/******************************************************************************
341	search mode callback
342******************************************************************************/
343global proc nw4cSetCombineGroup_SearchModeCB()
344{
345	int $dontCare =
346		(`radioButtonGrp -q -sl nw4cSetCombineGroup_SearchDontCare` == 1);
347	int $useCombineGroup =
348		(`radioButtonGrp -q -sl nw4cSetCombineGroup_SearchUseCombineGroup` == 1);
349
350	control -e -en $useCombineGroup nw4cSetCombineGroup_SearchCompare;
351	control -e -en $useCombineGroup nw4cSetCombineGroup_SearchCombineGroup;
352	control -e -en ($dontCare || $useCombineGroup) nw4cSetCombineGroup_SearchBtn;
353}
354
355/******************************************************************************
356	search value callback
357******************************************************************************/
358global proc nw4cSetCombineGroup_SearchValueCB()
359{
360	int $val = `intField -q -v nw4cSetCombineGroup_SearchCombineGroup`;
361	if ($val < 0)
362	{
363		intField -e -v 0 nw4cSetCombineGroup_SearchCombineGroup;
364	}
365	else if ($val > 255)
366	{
367		intField -e -v 255 nw4cSetCombineGroup_SearchCombineGroup;
368	}
369}
370
371/******************************************************************************
372	do search
373******************************************************************************/
374global proc nw4cSetCombineGroup_DoSearch()
375{
376	int $searchUse =
377		(`radioButtonGrp -q -sl nw4cSetCombineGroup_SearchUseCombineGroup` == 1);
378	int $compare = `optionMenu -q -sl nw4cSetCombineGroup_SearchCompare` - 1;
379	nw4cSetCombineGroup_SearchValueCB(); // clamp value
380	int $searchVal = `intField -q -v nw4cSetCombineGroup_SearchCombineGroup`;
381
382	int $nodeCount = 0;
383	select -cl;
384	string $xforms[] = `ls -typ transform`;
385	for ($node in $xforms)
386	{
387		if (IsSceneAnimObject($node))
388		{
389			continue;
390		}
391		int $useCombineGroup = nw4cSetCombineGroup_Get_UseCombineGroup($node);
392		int $CombineGroup = nw4cSetCombineGroup_Get_CombineGroup($node);
393		int $match = 0;
394		if (!$searchUse)
395		{
396			$match = !$useCombineGroup;
397		}
398		else
399		{
400			if (!$useCombineGroup)
401			{
402				$match = 0;
403			}
404			else if ($compare == 0)
405			{
406				$match = ($CombineGroup < $searchVal);
407			}
408			else if ($compare == 1)
409			{
410				$match = ($CombineGroup <= $searchVal);
411			}
412			else if ($compare == 2)
413			{
414				$match = ($CombineGroup == $searchVal);
415			}
416			else if ($compare == 3)
417			{
418				$match = ($CombineGroup >= $searchVal);
419			}
420			else
421			{
422				$match = ($CombineGroup > $searchVal);
423			}
424		}
425		if ($match)
426		{
427			select -add $node;
428			++$nodeCount;
429		}
430	}
431
432	if ($nodeCount == 0)
433	{
434		print "Not found\n";
435	}
436	else
437	{
438		string $msg = "Found: " + $nodeCount + " transform node";
439		if ($nodeCount >= 2)
440		{
441			$msg += "s";
442		}
443		print ($msg + "\n");
444	}
445}
446
447/******************************************************************************
448	main
449******************************************************************************/
450global proc NW4C_SetCombineGroup()
451{
452	int $afterMaya2011Adjust = 0;
453	if (getApplicationVersionAsFloat() >= 2011)
454		$afterMaya2011Adjust = 1;
455
456	//-----------------------------------------------------------------------------
457	// create window
458	int $winW = 400;
459	int $winH = 370;
460	if (!`window -exists nw4cSetCombineGroup_Win`)
461	{
462		window -t "NW4C Set Combine Group" -wh $winW $winH -mxb 0 -mb 1
463			nw4cSetCombineGroup_Win;
464/*
465		menu -l "Help";
466		menuItem -l ("Help on " + `window -q -t nw4cSetCombineGroup_Win` + "...")
467			-c "NW4C_ShowHelp \"html/NW4C_SetCombineGroup.html\" \"\"";
468*/
469		columnLayout -adj 1 -cat "both" 4 -cal "center" -rs 4;
470
471		//-----------------------------------------------------------------------------
472		// node name
473		frameLayout -l "Selected Transform Node" -cll 0 -cl 0 -bv 1 -bs "etchedIn";
474			columnLayout -adj 1 -cal "center" -rs 8;
475
476			text -l "" nw4cSetCombineGroup_NodeName;
477
478			setParent ..; // columnLayout
479		setParent ..; // formLayout
480
481		//-----------------------------------------------------------------------------
482		// set
483		frameLayout -l "Set" -cll 0 -cl 0 -bv 1 -bs "etchedIn";
484			columnLayout -adj 1 -cal "center" -rs 0;
485			string $form2 = `formLayout -nd 400`;
486				radioButtonGrp -l "Combine Group" -nrb 1 -cw2 110 80
487					-l1 "Don't care"
488					nw4cSetCombineGroup_DontCare;
489
490				radioButtonGrp -l "" -nrb 1 -cw2 110 18
491					-l1 ""
492					-scl nw4cSetCombineGroup_DontCare
493					nw4cSetCombineGroup_UseCombineGroup;
494
495				intField -w 50 -v 0
496					nw4cSetCombineGroup_CombineGroup;
497
498				text -l "Setting range 0 - 255"
499					nw4cSetCombineGroup_Description;
500			setParent ..; // formLayout
501			formLayout -e
502				-af nw4cSetCombineGroup_DontCare "top" 8
503				-af nw4cSetCombineGroup_DontCare "left" 0
504				-an nw4cSetCombineGroup_DontCare "bottom"
505				-an nw4cSetCombineGroup_DontCare "right"
506
507				-ac nw4cSetCombineGroup_UseCombineGroup "top" 8 nw4cSetCombineGroup_DontCare
508				-af nw4cSetCombineGroup_UseCombineGroup "left" 0
509				-an nw4cSetCombineGroup_UseCombineGroup "bottom"
510				-an nw4cSetCombineGroup_UseCombineGroup "right"
511
512				-ac nw4cSetCombineGroup_CombineGroup "top" 4 nw4cSetCombineGroup_DontCare
513				-ac nw4cSetCombineGroup_CombineGroup "left" 0 nw4cSetCombineGroup_UseCombineGroup
514				-an nw4cSetCombineGroup_CombineGroup "bottom"
515				-an nw4cSetCombineGroup_CombineGroup "right"
516
517				-ac nw4cSetCombineGroup_Description "top" 4 nw4cSetCombineGroup_CombineGroup
518				-ac nw4cSetCombineGroup_Description "left" 4 nw4cSetCombineGroup_UseCombineGroup
519				-af nw4cSetCombineGroup_Description "bottom" 8
520				-an nw4cSetCombineGroup_Description "right"
521				$form2;
522			rowColumnLayout -nc 2 -cw 1 200 -cw 2 150;
523				text -l ""; // dummy
524				button -l "Set Sequential"
525					-c "nw4cSetCombineGroup_SetSequential"
526					nw4cSetCombineGroup_SetSequentialBtn;
527			setParent ..; // rowColumnLayout
528			separator -h 8 -st "none";
529
530			setParent ..; // columnLayout
531		setParent ..; // frameLayout
532
533		//-----------------------------------------------------------------------------
534		// search
535		frameLayout -l "Search" -cll 0 -cl 0 -bv 1 -bs "etchedIn";
536			columnLayout -adj 1 -cal "center" -rs 0;
537			string $form3 = `formLayout -nd 400`;
538				radioButtonGrp -l "" -nrb 1 -cw2 110 80
539					-l1 "Don't care"
540					-on1 "nw4cSetCombineGroup_SearchModeCB"
541					nw4cSetCombineGroup_SearchDontCare;
542
543				radioButtonGrp -l "" -nrb 1 -cw2 110 18
544					-l1 ""
545					-scl nw4cSetCombineGroup_SearchDontCare
546					-on1 "nw4cSetCombineGroup_SearchModeCB"
547					nw4cSetCombineGroup_SearchUseCombineGroup;
548
549				optionMenu -w 110
550					nw4cSetCombineGroup_SearchCompare;
551					menuItem -l "<  : ( under )";
552					menuItem -l "<= : ( below )";
553					menuItem -l "=  : ( exactly )";
554					menuItem -l ">= : ( above )";
555					menuItem -l ">  : ( over )";
556
557				intField -w 50 -h 26 -v 0
558					-cc "nw4cSetCombineGroup_SearchValueCB"
559					nw4cSetCombineGroup_SearchCombineGroup;
560			setParent ..; // formLayout
561			formLayout -e
562				-af nw4cSetCombineGroup_SearchDontCare "top" 8
563				-af nw4cSetCombineGroup_SearchDontCare "left" 0
564				-an nw4cSetCombineGroup_SearchDontCare "bottom"
565				-an nw4cSetCombineGroup_SearchDontCare "right"
566
567				-ac nw4cSetCombineGroup_SearchUseCombineGroup "top" 10 nw4cSetCombineGroup_SearchDontCare
568				-af nw4cSetCombineGroup_SearchUseCombineGroup "left" 0
569				-an nw4cSetCombineGroup_SearchUseCombineGroup "bottom"
570				-an nw4cSetCombineGroup_SearchUseCombineGroup "right"
571
572				-ac nw4cSetCombineGroup_SearchCompare "top" (4 + $afterMaya2011Adjust * 3) nw4cSetCombineGroup_SearchDontCare
573				-ac nw4cSetCombineGroup_SearchCompare "left" 0 nw4cSetCombineGroup_SearchUseCombineGroup
574				-an nw4cSetCombineGroup_SearchCompare "bottom"
575				-an nw4cSetCombineGroup_SearchCompare "right"
576
577				-ac nw4cSetCombineGroup_SearchCombineGroup "top" 4 nw4cSetCombineGroup_SearchDontCare
578				-ac nw4cSetCombineGroup_SearchCombineGroup "left" 4 nw4cSetCombineGroup_SearchCompare
579				-af nw4cSetCombineGroup_SearchCombineGroup "bottom" 8
580				-an nw4cSetCombineGroup_SearchCombineGroup "right"
581				$form3;
582			rowColumnLayout -nc 2 -cw 1 200 -cw 2 150;
583				text -l ""; // dummy
584				button -l "Search"
585					-c "nw4cSetCombineGroup_DoSearch"
586					nw4cSetCombineGroup_SearchBtn;
587			setParent ..; // rowColumnLayout
588			separator -h 8 -st "none";
589
590			setParent ..; // columnLayout
591		setParent ..; // formLayout
592		optionMenu -e -sl 3 nw4cSetCombineGroup_SearchCompare;
593
594		setParent ..; // columnLayout
595
596		//-----------------------------------------------------------------------------
597		// close button
598		string $closeCmd = "deleteUI nw4cSetCombineGroup_Win";
599		string $form = `formLayout -nd 100`;
600			string $closeBtn = `button -h 26 -l "Close" -c $closeCmd`;
601			formLayout -e
602				-af $closeBtn "top" 0
603				-af $closeBtn "left" 0
604				-af $closeBtn "bottom" 0
605				-af $closeBtn "right" 0
606				$form;
607		setParent ..; // formLayout
608
609		setParent ..; // columnLayout
610
611		setFocus $closeBtn;
612
613		//-----------------------------------------------------------------------------
614		// set selection change job
615		scriptJob -p nw4cSetCombineGroup_Win
616			-e "SelectionChanged" "nw4cSetCombineGroup_UpdateWindow";
617	}
618	if (`window -q -w nw4cSetCombineGroup_Win` < $winW)
619	{
620		window -e -w $winW nw4cSetCombineGroup_Win;
621	}
622	if (`window -q -h nw4cSetCombineGroup_Win` < $winH)
623	{
624		window -e -h $winH nw4cSetCombineGroup_Win;
625	}
626	//window -e -wh $winW $winH nw4cSetCombineGroup_Win;
627
628	//-----------------------------------------------------------------------------
629	// update window
630	nw4cSetCombineGroup_UpdateWindow;
631	nw4cSetCombineGroup_SearchModeCB;
632
633	//-----------------------------------------------------------------------------
634	// show window
635	showWindow nw4cSetCombineGroup_Win;
636}
637