-
Notifications
You must be signed in to change notification settings - Fork 202
/
Copy pathUsdTransform3dMayaXformStack.cpp
926 lines (806 loc) · 34.6 KB
/
UsdTransform3dMayaXformStack.cpp
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
//
// Copyright 2020 Autodesk
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#include "UsdTransform3dMayaXformStack.h"
#include "private/UfeNotifGuard.h"
#include "private/Utils.h"
#include <mayaUsd/fileio/utils/xformStack.h>
#include <mayaUsd/ufe/RotationUtils.h>
#include <mayaUsd/ufe/UsdUndoableCommand.h>
#include <mayaUsd/ufe/Utils.h>
#include <mayaUsd/undo/UsdUndoBlock.h>
#include <mayaUsd/undo/UsdUndoableItem.h>
#include <maya/MEulerRotation.h>
#include <maya/MGlobal.h>
#include <maya/MMatrix.h>
#include <maya/MTransformationMatrix.h>
#include <maya/MVector.h>
#include <cstring>
#include <functional>
#include <map>
PXR_NAMESPACE_USING_DIRECTIVE
namespace {
using BaseUndoableCommand = Ufe::BaseUndoableCommand;
using OpFunc = std::function<UsdGeomXformOp(const BaseUndoableCommand&)>;
using namespace MayaUsd::ufe;
// Type traits for GfVec precision.
template <class V> struct OpPrecision
{
static UsdGeomXformOp::Precision precision;
};
template <>
UsdGeomXformOp::Precision OpPrecision<GfVec3f>::precision = UsdGeomXformOp::PrecisionFloat;
template <>
UsdGeomXformOp::Precision OpPrecision<GfVec3d>::precision = UsdGeomXformOp::PrecisionDouble;
VtValue getValue(const UsdAttribute& attr, const UsdTimeCode& time)
{
VtValue value;
attr.Get(&value, time);
return value;
}
// UsdMayaXformStack::FindOpIndex() requires an inconvenient isInvertedTwin
// argument, various rotate transform op equivalences in a separate
// UsdMayaXformStack::IsCompatibleType(). Just roll our own op name to
// Maya transform stack index position.
const std::unordered_map<TfToken, UsdTransform3dMayaXformStack::OpNdx, TfToken::HashFunctor>
gOpNameToNdx {
{ TfToken("xformOp:translate"), UsdTransform3dMayaXformStack::NdxTranslate },
{ TfToken("xformOp:translate:rotatePivotTranslate"),
UsdTransform3dMayaXformStack::NdxRotatePivotTranslate },
{ TfToken("xformOp:translate:rotatePivot"), UsdTransform3dMayaXformStack::NdxRotatePivot },
{ TfToken("xformOp:rotateX"), UsdTransform3dMayaXformStack::NdxRotate },
{ TfToken("xformOp:rotateY"), UsdTransform3dMayaXformStack::NdxRotate },
{ TfToken("xformOp:rotateZ"), UsdTransform3dMayaXformStack::NdxRotate },
{ TfToken("xformOp:rotateXYZ"), UsdTransform3dMayaXformStack::NdxRotate },
{ TfToken("xformOp:rotateXZY"), UsdTransform3dMayaXformStack::NdxRotate },
{ TfToken("xformOp:rotateYXZ"), UsdTransform3dMayaXformStack::NdxRotate },
{ TfToken("xformOp:rotateYZX"), UsdTransform3dMayaXformStack::NdxRotate },
{ TfToken("xformOp:rotateZXY"), UsdTransform3dMayaXformStack::NdxRotate },
{ TfToken("xformOp:rotateZYX"), UsdTransform3dMayaXformStack::NdxRotate },
{ TfToken("xformOp:orient"), UsdTransform3dMayaXformStack::NdxRotate },
{ TfToken("xformOp:rotateXYZ:rotateAxis"), UsdTransform3dMayaXformStack::NdxRotateAxis },
{ TfToken("!invert!xformOp:translate:rotatePivot"),
UsdTransform3dMayaXformStack::NdxRotatePivotInverse },
{ TfToken("xformOp:translate:scalePivotTranslate"),
UsdTransform3dMayaXformStack::NdxScalePivotTranslate },
{ TfToken("xformOp:translate:scalePivot"), UsdTransform3dMayaXformStack::NdxScalePivot },
{ TfToken("xformOp:transform:shear"), UsdTransform3dMayaXformStack::NdxShear },
{ TfToken("xformOp:scale"), UsdTransform3dMayaXformStack::NdxScale },
{ TfToken("!invert!xformOp:translate:scalePivot"),
UsdTransform3dMayaXformStack::NdxScalePivotInverse }
};
} // namespace
namespace MAYAUSD_NS_DEF {
namespace ufe {
namespace {
bool setXformOpOrder(const UsdGeomXformable& xformable)
{
// Simply adding a transform op appends to the op order vector. Therefore,
// after addition, we must sort the ops to preserve Maya transform stack
// ordering. Use the Maya transform stack indices to add to a map, then
// simply traverse the map to obtain the transform ops in order.
std::map<UsdTransform3dMayaXformStack::OpNdx, UsdGeomXformOp> orderedOps;
bool resetsXformStack = false;
auto oldOrder = xformable.GetOrderedXformOps(&resetsXformStack);
for (const auto& op : oldOrder) {
auto ndx = gOpNameToNdx.at(op.GetOpName());
orderedOps[ndx] = op;
}
// Set the transform op order attribute.
std::vector<UsdGeomXformOp> newOrder;
newOrder.reserve(oldOrder.size());
for (const auto& orderedOp : orderedOps) {
const auto& op = orderedOp.second;
newOrder.emplace_back(op);
}
return xformable.SetXformOpOrder(newOrder, resetsXformStack);
}
using NextTransform3dFn = std::function<Ufe::Transform3d::Ptr()>;
Ufe::Transform3d::Ptr
createTransform3d(const Ufe::SceneItem::Ptr& item, NextTransform3dFn nextTransform3dFn)
{
UsdSceneItem::Ptr usdItem = std::dynamic_pointer_cast<UsdSceneItem>(item);
#if !defined(NDEBUG)
if (!usdItem) {
TF_FATAL_ERROR(
"Could not create Maya transform stack Transform3d interface for null item.");
}
#endif
// If the prim isn't transformable, can't create a Transform3d interface
// for it.
UsdGeomXformable xformSchema(usdItem->prim());
if (!xformSchema) {
return nullptr;
}
bool resetsXformStack = false;
auto xformOps = xformSchema.GetOrderedXformOps(&resetsXformStack);
// Early out: if there are no transform ops yet, it's a match.
if (xformOps.empty()) {
return UsdTransform3dMayaXformStack::create(usdItem);
}
// If the prim supports the Maya transform stack, create a Maya transform
// stack interface for it, otherwise delegate to the next handler in the
// chain of responsibility.
auto stackOps = UsdMayaXformStack::MayaStack().MatchingSubstack(xformOps);
return stackOps.empty() ? nextTransform3dFn() : UsdTransform3dMayaXformStack::create(usdItem);
}
// Class for setMatrixCmd() implementation.
class UsdSetMatrix4dUndoableCmd : public UsdUndoableCommand<Ufe::SetMatrix4dUndoableCommand>
{
public:
UsdSetMatrix4dUndoableCmd(const Ufe::Path& path, const Ufe::Matrix4d& newM)
: UsdUndoableCommand<Ufe::SetMatrix4dUndoableCommand>(path)
{
// Decompose new matrix to extract TRS. Neither GfMatrix4d::Factor
// nor GfTransform decomposition provide results that match Maya,
// so use MTransformationMatrix.
MMatrix m;
std::memcpy(m[0], &newM.matrix[0][0], sizeof(double) * 16);
MTransformationMatrix xformM(m);
auto t = xformM.getTranslation(MSpace::kTransform);
double r[3];
double s[3];
MTransformationMatrix::RotationOrder rotOrder;
xformM.getRotation(r, rotOrder);
xformM.getScale(s, MSpace::kTransform);
constexpr double radToDeg = 57.295779506;
_newT = Ufe::Vector3d(t[0], t[1], t[2]);
_newR = Ufe::Vector3d(r[0] * radToDeg, r[1] * radToDeg, r[2] * radToDeg);
_newS = Ufe::Vector3d(s[0], s[1], s[2]);
}
~UsdSetMatrix4dUndoableCmd() override { }
bool set(const Ufe::Matrix4d&) override
{
// No-op: Maya does not set matrices through interactive manipulation.
TF_WARN("Illegal call to UsdSetMatrix4dUndoableCmd::set()");
return true;
}
protected:
void executeUndoBlock() override
{
// transform3d() and editTransform3d() are equivalent for a normal Maya
// transform stack, but not for a fallback Maya transform stack, and
// both can be edited by this command.
auto t3d = Ufe::Transform3d::editTransform3d(sceneItem());
t3d->translate(_newT.x(), _newT.y(), _newT.z());
t3d->rotate(_newR.x(), _newR.y(), _newR.z());
t3d->scale(_newS.x(), _newS.y(), _newS.z());
}
private:
Ufe::Vector3d _newT;
Ufe::Vector3d _newR;
Ufe::Vector3d _newS;
};
// Helper class to factor out common code for translate, rotate, scale
// undoable commands.
class UsdTRSUndoableCmdBase : public Ufe::SetVector3dUndoableCommand
{
private:
const UsdTimeCode _readTime;
const UsdTimeCode _writeTime;
VtValue _newOpValue;
UsdGeomXformOp _op;
OpFunc _opFunc;
UsdUndoableItem _undoableItem;
public:
struct State
{
virtual const char* name() const = 0;
virtual void handleUndo(UsdTRSUndoableCmdBase*)
{
TF_CODING_ERROR(
"Illegal handleUndo() call in UsdTRSUndoableCmdBase for state '%s'.", name());
}
virtual void handleSet(UsdTRSUndoableCmdBase*, const VtValue&)
{
TF_CODING_ERROR(
"Illegal handleSet() call in UsdTRSUndoableCmdBase for state '%s'.", name());
}
};
struct InitialState : public State
{
const char* name() const override { return "initial"; }
void handleUndo(UsdTRSUndoableCmdBase* cmd) override
{
// Maya triggers an undo on command creation, ignore it.
cmd->_state = &UsdTRSUndoableCmdBase::_initialUndoCalledState;
}
void handleSet(UsdTRSUndoableCmdBase* cmd, const VtValue& v) override
{
// Add undoblock to capture edits
UsdUndoBlock undoBlock(&cmd->_undoableItem);
// Going from initial to executing / executed state, save value.
cmd->_op = cmd->_opFunc(*cmd);
cmd->_newOpValue = v;
cmd->setValue(v);
cmd->_state = &UsdTRSUndoableCmdBase::_executeState;
}
};
struct InitialUndoCalledState : public State
{
const char* name() const override { return "initial undo called"; }
void handleSet(UsdTRSUndoableCmdBase* cmd, const VtValue&) override
{
// Maya triggers a redo on command creation, ignore it.
cmd->_state = &UsdTRSUndoableCmdBase::_initialState;
}
};
struct ExecuteState : public State
{
const char* name() const override { return "execute"; }
void handleUndo(UsdTRSUndoableCmdBase* cmd) override
{
// Undo
cmd->_undoableItem.undo();
cmd->_state = &UsdTRSUndoableCmdBase::_undoneState;
}
void handleSet(UsdTRSUndoableCmdBase* cmd, const VtValue& v) override
{
cmd->_newOpValue = v;
cmd->setValue(v);
}
};
struct UndoneState : public State
{
const char* name() const override { return "undone"; }
void handleSet(UsdTRSUndoableCmdBase* cmd, const VtValue&) override
{
// Redo
cmd->_undoableItem.redo();
cmd->_state = &UsdTRSUndoableCmdBase::_redoneState;
}
};
struct RedoneState : public State
{
const char* name() const override { return "redone"; }
void handleUndo(UsdTRSUndoableCmdBase* cmd) override
{
// Undo
cmd->_undoableItem.undo();
cmd->_state = &UsdTRSUndoableCmdBase::_undoneState;
}
// The redone state should normally be reached only once manipulation
// is over, after undo, so setting new values in the redone state seems
// illogical. However, during point snapping manipulation, within a
// single drag, the Maya move command repeatedly calls undo, then redo,
// setting new values after the redo. Treat such events identically to
// the Execute state.
void handleSet(UsdTRSUndoableCmdBase* cmd, const VtValue& v) override
{
cmd->_newOpValue = v;
cmd->setValue(v);
}
};
UsdTRSUndoableCmdBase(
const VtValue& newOpValue,
const Ufe::Path& path,
OpFunc opFunc,
const UsdTimeCode& writeTime_)
: Ufe::SetVector3dUndoableCommand(path)
,
// Always read from proxy shape time.
_readTime(getTime(path))
, _writeTime(writeTime_)
, _newOpValue(newOpValue)
, _op()
, _opFunc(std::move(opFunc))
{
}
// Ufe::UndoableCommand overrides.
void execute() override { handleSet(_newOpValue); }
void undo() override { _state->handleUndo(this); }
void redo() override { handleSet(_newOpValue); }
void handleSet(const VtValue& v) { _state->handleSet(this, v); }
void setValue(const VtValue& v)
{
auto attr = _op.GetAttr();
if (attr) {
_op.GetAttr().Set(v, _writeTime);
}
}
UsdTimeCode readTime() const { return _readTime; }
UsdTimeCode writeTime() const { return _writeTime; }
static InitialState _initialState;
static InitialUndoCalledState _initialUndoCalledState;
static ExecuteState _executeState;
static UndoneState _undoneState;
static RedoneState _redoneState;
State* _state { &_initialState };
};
UsdTRSUndoableCmdBase::InitialState UsdTRSUndoableCmdBase::_initialState;
UsdTRSUndoableCmdBase::InitialUndoCalledState UsdTRSUndoableCmdBase::_initialUndoCalledState;
UsdTRSUndoableCmdBase::ExecuteState UsdTRSUndoableCmdBase::_executeState;
UsdTRSUndoableCmdBase::UndoneState UsdTRSUndoableCmdBase::_undoneState;
UsdTRSUndoableCmdBase::RedoneState UsdTRSUndoableCmdBase::_redoneState;
// UsdRotatePivotTranslateUndoableCmd uses hard-coded USD common transform API
// single pivot attribute name, not reusable.
template <class V> class UsdVecOpUndoableCmd : public UsdTRSUndoableCmdBase
{
public:
UsdVecOpUndoableCmd(
const V& v,
const Ufe::Path& path,
OpFunc opFunc,
const UsdTimeCode& writeTime)
: UsdTRSUndoableCmdBase(VtValue(v), path, opFunc, writeTime)
{
}
// Executes the command by setting the translation onto the transform op.
bool set(double x, double y, double z) override
{
VtValue v;
v = V(x, y, z);
handleSet(v);
return true;
}
};
class UsdRotateOpUndoableCmd : public UsdTRSUndoableCmdBase
{
public:
UsdRotateOpUndoableCmd(
const GfVec3f& r,
const Ufe::Path& path,
OpFunc opFunc,
UsdTransform3dMayaXformStack::CvtRotXYZToAttrFn cvt,
const UsdTimeCode& writeTime)
: UsdTRSUndoableCmdBase(VtValue(r), path, opFunc, writeTime)
, _cvtRotXYZToAttr(cvt)
{
}
// Executes the command by setting the rotation onto the transform op.
bool set(double x, double y, double z) override
{
VtValue v;
v = _cvtRotXYZToAttr(x, y, z);
handleSet(v);
return true;
}
private:
// Convert from UFE RotXYZ rotation to a value for the transform op.
UsdTransform3dMayaXformStack::CvtRotXYZToAttrFn _cvtRotXYZToAttr;
};
} // namespace
UsdTransform3dMayaXformStack::UsdTransform3dMayaXformStack(const UsdSceneItem::Ptr& item)
: UsdTransform3dBase(item)
, _xformable(prim())
{
TF_AXIOM(_xformable);
}
/* static */
UsdTransform3dMayaXformStack::Ptr
UsdTransform3dMayaXformStack::create(const UsdSceneItem::Ptr& item)
{
return std::make_shared<UsdTransform3dMayaXformStack>(item);
}
Ufe::Vector3d UsdTransform3dMayaXformStack::translation() const
{
return getVector3d<GfVec3d>(
UsdGeomXformOp::GetOpName(UsdGeomXformOp::TypeTranslate, getTRSOpSuffix()));
}
Ufe::Vector3d UsdTransform3dMayaXformStack::rotation() const
{
if (!hasOp(NdxRotate)) {
return Ufe::Vector3d(0, 0, 0);
}
UsdGeomXformOp r = getOp(NdxRotate);
TF_AXIOM(r);
if (!r.GetAttr().HasValue()) {
return Ufe::Vector3d(0, 0, 0);
}
CvtRotXYZFromAttrFn cvt = getCvtRotXYZFromAttrFn(r.GetOpName());
return cvt(getValue(r.GetAttr(), getTime(path())));
}
Ufe::Vector3d UsdTransform3dMayaXformStack::scale() const
{
if (!hasOp(NdxScale)) {
return Ufe::Vector3d(1, 1, 1);
}
UsdGeomXformOp s = getOp(NdxScale);
TF_AXIOM(s);
if (!s.GetAttr().HasValue()) {
return Ufe::Vector3d(1, 1, 1);
}
GfVec3f v;
s.Get(&v, getTime(path()));
return toUfe(v);
}
Ufe::TranslateUndoableCommand::Ptr
UsdTransform3dMayaXformStack::translateCmd(double x, double y, double z)
{
return setVector3dCmd(
GfVec3d(x, y, z),
UsdGeomXformOp::GetOpName(UsdGeomXformOp::TypeTranslate, getTRSOpSuffix()),
getTRSOpSuffix());
}
Ufe::RotateUndoableCommand::Ptr
UsdTransform3dMayaXformStack::rotateCmd(double x, double y, double z)
{
GfVec3f v(x, y, z);
UsdGeomXformOp op;
TfToken attrName;
const bool hasRotate = hasOp(NdxRotate);
if (hasRotate) {
op = getOp(NdxRotate);
attrName = op.GetOpName();
}
// If there is no rotate transform op, we will create a RotXYZ.
CvtRotXYZToAttrFn cvt = hasRotate ? getCvtRotXYZToAttrFn(op.GetOpName()) : toXYZ;
OpFunc f = hasRotate
? OpFunc([attrName](const BaseUndoableCommand& cmd) {
auto usdSceneItem = std::dynamic_pointer_cast<UsdSceneItem>(cmd.sceneItem());
TF_AXIOM(usdSceneItem);
auto attr = usdSceneItem->prim().GetAttribute(attrName);
// return an invalid XformOp if the attribute edit is not allowed.
std::string errMsg;
if (!MayaUsd::ufe::isAttributeEditAllowed(attr, &errMsg)) {
MGlobal::displayError(errMsg.c_str());
return UsdGeomXformOp();
}
return UsdGeomXformOp(attr);
})
: OpFunc([opSuffix = getTRSOpSuffix(), setXformOpOrderFn = getXformOpOrderFn(), v](
const BaseUndoableCommand& cmd) {
// Use notification guard, otherwise will generate one notification
// for the xform op add, and another for the reorder.
InTransform3dChange guard(cmd.path());
auto usdSceneItem = std::dynamic_pointer_cast<UsdSceneItem>(cmd.sceneItem());
TF_AXIOM(usdSceneItem);
UsdGeomXformable xformable(usdSceneItem->prim());
// At this point we already know that writing to attribute is going
// to succeed but we do not know if writing to "transform op order" succeed since
// we could be a weaker layer.
std::string errMsg;
if (!MayaUsd::ufe::isAttributeEditAllowed(xformable.GetXformOpOrderAttr(), &errMsg)) {
MGlobal::displayError(errMsg.c_str());
return UsdGeomXformOp();
}
auto r = xformable.AddRotateXYZOp(UsdGeomXformOp::PrecisionFloat, opSuffix);
TF_AXIOM(r);
r.Set(v);
auto result = setXformOpOrderFn(xformable);
TF_AXIOM(result);
return r;
});
return std::make_shared<UsdRotateOpUndoableCmd>(
v, path(), std::move(f), cvt, UsdTimeCode::Default());
}
Ufe::ScaleUndoableCommand::Ptr UsdTransform3dMayaXformStack::scaleCmd(double x, double y, double z)
{
GfVec3f v(x, y, z);
UsdGeomXformOp op;
TfToken attrName;
const bool hasScale = hasOp(NdxScale);
if (hasScale) {
op = getOp(NdxScale);
attrName = op.GetOpName();
}
OpFunc f = hasScale
? OpFunc([attrName](const BaseUndoableCommand& cmd) {
auto usdSceneItem = std::dynamic_pointer_cast<UsdSceneItem>(cmd.sceneItem());
TF_AXIOM(usdSceneItem);
auto attr = usdSceneItem->prim().GetAttribute(attrName);
// return an invalid XformOp if the attribute edit is not allowed.
std::string errMsg;
if (!MayaUsd::ufe::isAttributeEditAllowed(attr, &errMsg)) {
MGlobal::displayError(errMsg.c_str());
return UsdGeomXformOp();
}
return UsdGeomXformOp(attr);
})
: OpFunc([opSuffix = getTRSOpSuffix(), setXformOpOrderFn = getXformOpOrderFn(), v](
const BaseUndoableCommand& cmd) {
InTransform3dChange guard(cmd.path());
auto usdSceneItem = std::dynamic_pointer_cast<UsdSceneItem>(cmd.sceneItem());
TF_AXIOM(usdSceneItem);
UsdGeomXformable xformable(usdSceneItem->prim());
// At this point we already know that writing to attribute is going
// to succeed but we do not know if writing to "transform op order" succeed since
// we could be a weaker layer.
std::string errMsg;
if (!MayaUsd::ufe::isAttributeEditAllowed(xformable.GetXformOpOrderAttr(), &errMsg)) {
MGlobal::displayError(errMsg.c_str());
return UsdGeomXformOp();
}
auto s = xformable.AddScaleOp(UsdGeomXformOp::PrecisionFloat, opSuffix);
TF_AXIOM(s);
s.Set(v);
auto result = setXformOpOrderFn(xformable);
TF_AXIOM(result);
return s;
});
return std::make_shared<UsdVecOpUndoableCmd<GfVec3f>>(
v, path(), std::move(f), UsdTimeCode::Default());
}
Ufe::TranslateUndoableCommand::Ptr
UsdTransform3dMayaXformStack::rotatePivotCmd(double x, double y, double z)
{
return pivotCmd(getOpSuffix(NdxRotatePivot), x, y, z);
}
Ufe::Vector3d UsdTransform3dMayaXformStack::rotatePivot() const
{
return getVector3d<GfVec3f>(
UsdGeomXformOp::GetOpName(UsdGeomXformOp::TypeTranslate, getOpSuffix(NdxRotatePivot)));
}
Ufe::TranslateUndoableCommand::Ptr
UsdTransform3dMayaXformStack::scalePivotCmd(double x, double y, double z)
{
return pivotCmd(getOpSuffix(NdxScalePivot), x, y, z);
}
Ufe::Vector3d UsdTransform3dMayaXformStack::scalePivot() const
{
return getVector3d<GfVec3f>(
UsdGeomXformOp::GetOpName(UsdGeomXformOp::TypeTranslate, getOpSuffix(NdxScalePivot)));
}
Ufe::TranslateUndoableCommand::Ptr
UsdTransform3dMayaXformStack::translateRotatePivotCmd(double x, double y, double z)
{
auto opSuffix = getOpSuffix(NdxRotatePivotTranslate);
auto attrName = UsdGeomXformOp::GetOpName(UsdGeomXformOp::TypeTranslate, opSuffix);
return setVector3dCmd(GfVec3f(x, y, z), attrName, opSuffix);
}
Ufe::Vector3d UsdTransform3dMayaXformStack::rotatePivotTranslation() const
{
return getVector3d<GfVec3f>(UsdGeomXformOp::GetOpName(
UsdGeomXformOp::TypeTranslate, getOpSuffix(NdxRotatePivotTranslate)));
}
Ufe::TranslateUndoableCommand::Ptr
UsdTransform3dMayaXformStack::translateScalePivotCmd(double x, double y, double z)
{
auto opSuffix = getOpSuffix(NdxScalePivotTranslate);
auto attrName = UsdGeomXformOp::GetOpName(UsdGeomXformOp::TypeTranslate, opSuffix);
return setVector3dCmd(GfVec3f(x, y, z), attrName, opSuffix);
}
Ufe::Vector3d UsdTransform3dMayaXformStack::scalePivotTranslation() const
{
return getVector3d<GfVec3f>(UsdGeomXformOp::GetOpName(
UsdGeomXformOp::TypeTranslate, getOpSuffix(NdxScalePivotTranslate)));
}
template <class V>
Ufe::Vector3d UsdTransform3dMayaXformStack::getVector3d(const TfToken& attrName) const
{
// If the attribute doesn't exist or have a value yet, return a zero vector.
auto attr = prim().GetAttribute(attrName);
if (!attr || !attr.HasValue()) {
return Ufe::Vector3d(0, 0, 0);
}
UsdGeomXformOp op(attr);
TF_AXIOM(op);
V v;
op.Get(&v, getTime(path()));
return toUfe(v);
}
template <class V>
Ufe::SetVector3dUndoableCommand::Ptr UsdTransform3dMayaXformStack::setVector3dCmd(
const V& v,
const TfToken& attrName,
const TfToken& opSuffix)
{
auto attr = prim().GetAttribute(attrName);
auto setXformOpOrderFn = getXformOpOrderFn();
OpFunc f = attr
? OpFunc([attrName](const BaseUndoableCommand& cmd) {
auto usdSceneItem = std::dynamic_pointer_cast<UsdSceneItem>(cmd.sceneItem());
TF_AXIOM(usdSceneItem);
auto attr = usdSceneItem->prim().GetAttribute(attrName);
// return an invalid XformOp if the attribute edit is not allowed.
std::string errMsg;
if (!MayaUsd::ufe::isAttributeEditAllowed(attr, &errMsg)) {
MGlobal::displayError(errMsg.c_str());
return UsdGeomXformOp();
}
return UsdGeomXformOp(attr);
})
: OpFunc(
// MAYA-108612: generalized lambda capture below is incorrect with
// gcc 6.3.1 on Linux. Call to getXformOpOrderFn() is non-virtual;
// work around by calling in function body. PPT, 11-Jan-2021.
// [opSuffix, setXformOpOrderFn = getXformOpOrderFn(), v](const BaseUndoableCommand&
// cmd) {
[opSuffix, setXformOpOrderFn, v](const BaseUndoableCommand& cmd) {
InTransform3dChange guard(cmd.path());
auto usdSceneItem = std::dynamic_pointer_cast<UsdSceneItem>(cmd.sceneItem());
TF_AXIOM(usdSceneItem);
UsdGeomXformable xformable(usdSceneItem->prim());
// At this point we already know that writing to attribute is going
// to succeed but we do not know if writing to "transform op order" succeed
// since we could be a weaker layer.
std::string errMsg;
if (!MayaUsd::ufe::isAttributeEditAllowed(
xformable.GetXformOpOrderAttr(), &errMsg)) {
MGlobal::displayError(errMsg.c_str());
return UsdGeomXformOp();
}
auto op = xformable.AddTranslateOp(OpPrecision<V>::precision, opSuffix);
TF_AXIOM(op);
op.Set(v);
auto result = setXformOpOrderFn(xformable);
TF_AXIOM(result);
return op;
});
return std::make_shared<UsdVecOpUndoableCmd<V>>(
v, path(), std::move(f), UsdTimeCode::Default());
}
Ufe::TranslateUndoableCommand::Ptr
UsdTransform3dMayaXformStack::pivotCmd(const TfToken& pvtOpSuffix, double x, double y, double z)
{
auto pvtAttrName = UsdGeomXformOp::GetOpName(UsdGeomXformOp::TypeTranslate, pvtOpSuffix);
GfVec3f v(x, y, z);
auto attr = prim().GetAttribute(pvtAttrName);
OpFunc f = attr
? OpFunc([pvtAttrName](const BaseUndoableCommand& cmd) {
auto usdSceneItem = std::dynamic_pointer_cast<UsdSceneItem>(cmd.sceneItem());
TF_AXIOM(usdSceneItem);
auto attr = usdSceneItem->prim().GetAttribute(pvtAttrName);
// return an invalid XformOp if the attribute edit is not allowed.
std::string errMsg;
if (!MayaUsd::ufe::isAttributeEditAllowed(attr, &errMsg)) {
MGlobal::displayError(errMsg.c_str());
return UsdGeomXformOp();
}
return UsdGeomXformOp(attr);
})
: OpFunc([pvtOpSuffix, setXformOpOrderFn = getXformOpOrderFn(), v](
const BaseUndoableCommand& cmd) {
// Without a notification guard each operation (each transform op
// addition, setting the attribute value, and setting the transform
// op order) will notify. Observers would see an object in an
// inconsistent state, especially after pivot is added but before
// its inverse is added --- this does not match the Maya transform
// stack. Use of SdfChangeBlock is discouraged when calling USD
// APIs above Sdf, so use our own guard.
InTransform3dChange guard(cmd.path());
auto usdSceneItem = std::dynamic_pointer_cast<UsdSceneItem>(cmd.sceneItem());
TF_AXIOM(usdSceneItem);
UsdGeomXformable xformable(usdSceneItem->prim());
auto p = xformable.AddTranslateOp(UsdGeomXformOp::PrecisionFloat, pvtOpSuffix);
// At this point we already know that writing to attribute is going
// to succeed but we do not know if writing to "transform op order" succeed since
// we could be a weaker layer.
std::string errMsg;
if (!MayaUsd::ufe::isAttributeEditAllowed(xformable.GetXformOpOrderAttr(), &errMsg)) {
MGlobal::displayError(errMsg.c_str());
return UsdGeomXformOp();
}
auto pInv = xformable.AddTranslateOp(
UsdGeomXformOp::PrecisionFloat, pvtOpSuffix, /* isInverseOp */ true);
TF_AXIOM(p && pInv);
p.Set(v);
auto result = setXformOpOrderFn(xformable);
TF_AXIOM(result);
return p;
});
return std::make_shared<UsdVecOpUndoableCmd<GfVec3f>>(
v, path(), std::move(f), UsdTimeCode::Default());
}
Ufe::SetMatrix4dUndoableCommand::Ptr
UsdTransform3dMayaXformStack::setMatrixCmd(const Ufe::Matrix4d& m)
{
return std::make_shared<UsdSetMatrix4dUndoableCmd>(path(), m);
}
UsdTransform3dMayaXformStack::SetXformOpOrderFn
UsdTransform3dMayaXformStack::getXformOpOrderFn() const
{
return setXformOpOrder;
}
std::map<UsdTransform3dMayaXformStack::OpNdx, UsdGeomXformOp>
UsdTransform3dMayaXformStack::getOrderedOps() const
{
std::map<OpNdx, UsdGeomXformOp> orderedOps;
bool resetsXformStack = false;
auto ops = _xformable.GetOrderedXformOps(&resetsXformStack);
for (const auto& op : ops) {
auto ndx = gOpNameToNdx.at(op.GetOpName());
orderedOps[ndx] = op;
}
return orderedOps;
}
bool UsdTransform3dMayaXformStack::hasOp(OpNdx ndx) const
{
auto orderedOps = getOrderedOps();
return orderedOps.find(ndx) != orderedOps.end();
}
UsdGeomXformOp UsdTransform3dMayaXformStack::getOp(OpNdx ndx) const
{
auto orderedOps = getOrderedOps();
return orderedOps.at(ndx);
}
TfToken UsdTransform3dMayaXformStack::getOpSuffix(OpNdx ndx) const
{
static std::unordered_map<OpNdx, TfToken> opSuffix
= { { NdxRotatePivotTranslate, UsdMayaXformStackTokens->rotatePivotTranslate },
{ NdxRotatePivot, UsdMayaXformStackTokens->rotatePivot },
{ NdxRotateAxis, UsdMayaXformStackTokens->rotateAxis },
{ NdxScalePivotTranslate, UsdMayaXformStackTokens->scalePivotTranslate },
{ NdxScalePivot, UsdMayaXformStackTokens->scalePivot },
{ NdxShear, UsdMayaXformStackTokens->shear } };
return opSuffix.at(ndx);
}
TfToken UsdTransform3dMayaXformStack::getTRSOpSuffix() const { return TfToken(); }
UsdTransform3dMayaXformStack::CvtRotXYZFromAttrFn
UsdTransform3dMayaXformStack::getCvtRotXYZFromAttrFn(const TfToken& opName) const
{
static std::unordered_map<TfToken, CvtRotXYZFromAttrFn, TfToken::HashFunctor> cvt = {
{ TfToken("xformOp:rotateX"), fromX }, { TfToken("xformOp:rotateY"), fromY },
{ TfToken("xformOp:rotateZ"), fromZ }, { TfToken("xformOp:rotateXYZ"), fromXYZ },
{ TfToken("xformOp:rotateXZY"), fromXZY }, { TfToken("xformOp:rotateYXZ"), fromYXZ },
{ TfToken("xformOp:rotateYZX"), fromYZX }, { TfToken("xformOp:rotateZXY"), fromZXY },
{ TfToken("xformOp:rotateZYX"), fromZYX }, { TfToken("xformOp:orient"), nullptr }
}; // FIXME, unsupported.
return cvt.at(opName);
}
UsdTransform3dMayaXformStack::CvtRotXYZToAttrFn
UsdTransform3dMayaXformStack::getCvtRotXYZToAttrFn(const TfToken& opName) const
{
static std::unordered_map<TfToken, CvtRotXYZToAttrFn, TfToken::HashFunctor> cvt = {
{ TfToken("xformOp:rotateX"), toX }, { TfToken("xformOp:rotateY"), toY },
{ TfToken("xformOp:rotateZ"), toZ }, { TfToken("xformOp:rotateXYZ"), toXYZ },
{ TfToken("xformOp:rotateXZY"), toXZY }, { TfToken("xformOp:rotateYXZ"), toYXZ },
{ TfToken("xformOp:rotateYZX"), toYZX }, { TfToken("xformOp:rotateZXY"), toZXY },
{ TfToken("xformOp:rotateZYX"), toZYX }, { TfToken("xformOp:orient"), nullptr }
}; // FIXME, unsupported.
return cvt.at(opName);
}
//------------------------------------------------------------------------------
// UsdTransform3dMayaXformStackHandler
//------------------------------------------------------------------------------
UsdTransform3dMayaXformStackHandler::UsdTransform3dMayaXformStackHandler(
const Ufe::Transform3dHandler::Ptr& nextHandler)
: Ufe::Transform3dHandler()
, _nextHandler(nextHandler)
{
}
/*static*/
UsdTransform3dMayaXformStackHandler::Ptr
UsdTransform3dMayaXformStackHandler::create(const Ufe::Transform3dHandler::Ptr& nextHandler)
{
return std::make_shared<UsdTransform3dMayaXformStackHandler>(nextHandler);
}
Ufe::Transform3d::Ptr
UsdTransform3dMayaXformStackHandler::transform3d(const Ufe::SceneItem::Ptr& item) const
{
return createTransform3d(item, [&]() { return _nextHandler->transform3d(item); });
}
Ufe::Transform3d::Ptr UsdTransform3dMayaXformStackHandler::editTransform3d(
const Ufe::SceneItem::Ptr& item UFE_V2(, const Ufe::EditTransform3dHint& hint)) const
{
// MAYA-109190: Moved the IsInstanceProxy() check here since it was causing the
// camera framing not properly be applied.
//
// HS January 15, 2021: After speaking with Pierre, there is a more robust solution to move this
// check entirely from here.
// According to USD docs, editing scene description via instance proxies and their properties is
// not allowed.
// https://graphics.pixar.com/usd/docs/api/_usd__page__scenegraph_instancing.html#Usd_ScenegraphInstancing_InstanceProxies
UsdSceneItem::Ptr usdItem = std::dynamic_pointer_cast<UsdSceneItem>(item);
if (usdItem->prim().IsInstanceProxy()) {
MGlobal::displayError(
MString("Authoring to the descendant of an instance [")
+ MString(usdItem->prim().GetName().GetString().c_str()) + MString("] is not allowed. ")
+ MString("Please mark 'instanceable=false' to author edits to instance proxies."));
return nullptr;
}
auto stage = usdItem->prim().GetStage();
if (stage) {
const SdfLayerHandle& editLayer = stage->GetEditTarget().GetLayer();
if (editLayer && stage->IsLayerMuted(editLayer->GetIdentifier())) {
MGlobal::displayError("Editing a muted layer is not allowed.");
return nullptr;
}
}
return createTransform3d(
item, [&]() { return _nextHandler->editTransform3d(item UFE_V2(, hint)); });
}
} // namespace ufe
} // namespace MAYAUSD_NS_DEF