0

Is there a way to center the pivot of an object without the use of xform?

I really would like to try and find a pyMel version of this, or the maya api, as xform is generally 10x slower than a pymel or api solution.

Obviously you can achieve it with xform like so:

xform(obj, cp=1)

But I'm trying to find another way, does anyone know anything?

Shannon Hochkins
  • 11,763
  • 15
  • 62
  • 95
  • You should be able to mess around with transform attributes directly, but I'm more curious about the basis for your "10x slower" comment. How did you arrive at that number? It must have been in reference to a different problem, since in this case you don't have a different solution against which to benchmark in the first place... (I am guessing that whatever you were doing before, the xform command was not the slow part, but the MEL surrounding it was. In that case, just calling xform from Python should help a lot.) – Mark R. Wilkins Aug 23 '13 at 07:56
  • xform triggers undo which is always a bit slow. But not updating undo is problematic, you can suspend undo for time being. But this makes it a usability problem. The api can do this faster if your interested in implementing your own undo. Anyway if your really so performance sensitive (premature optimization?) your only option is to use c++ for everything. – joojaa Aug 24 '13 at 07:24
  • @MarkR.Wilkins, I have read it multiple times that xForm is always slower than the alternative, also what JooJaa says about the undo function part of xForm, although I do think I need to dig into the api for cleaner coding like this :) – Shannon Hochkins Aug 26 '13 at 07:32

2 Answers2

1

In the API it would be calling mfnTransform.setRotatePivotTranslation and setScalePivotTranslation with 'balance' turned on. There's not enough overhead to warrant a workaround - it's hard to see how this could be a bottleneck.

theodox
  • 12,028
  • 3
  • 23
  • 36
  • Im not sure how you would implement this, are you able to demonstrate this with your code? – Shannon Hochkins Aug 24 '13 at 07:55
  • the long bit is turning a scene object into an mfnTransform, which usually means getting it from an mfnDagNode, which in turn usually comes from an mfnSelectionlistt... example halfway down this page: http://www.akeric.com/blog/?p=1067. It's a lot of work for little to no speed gain. don't bother – theodox Aug 24 '13 at 16:45
  • Well I already am using the api for other things so its not that much work, It would be cool if you could time functions to see which takes the longest – Shannon Hochkins Aug 26 '13 at 07:33
  • Use the timeit module: http://docs.python.org/2/library/timeit.html to get accurate profiling data. However in a case like this the results will depend very heavily on the number of objects you're processing - as i said, unless you are centering hundreds of pivots at a time the speed of this operation should not matter very much. All the functions do is set the rotate / scale pivots to zero and invert the rotatePivotTranslate / scalePivotTranslate matrices, it's not much math and it's all done in the maya core. – theodox Aug 26 '13 at 15:29
1

Would you like to find a PyMEL version? Or an object oriented way of doing this? xform(obj, cp=1) is within PyMEL. However the object oriented method to produce the same result is quite similar to theodox's response in which you would do the following:

obj.setScalePivot(obj.c.get())
obj.setRotatePivot(obj.c.get())

Centering an objects pivot is based on the center of the bounding box. obj.c.get will return to you just that. Just plug that into the methods above.

Argiri Kotsaris
  • 492
  • 3
  • 7
  • Do you think this would be faster than xform (cp=1)? – Shannon Hochkins Aug 24 '13 at 05:09
  • @ShannonHochkins No it will most likely work slightly slower. – joojaa Aug 24 '13 at 07:27
  • Do you mind me asking why @joojaa? Wouldn't xForm have to perform the exact same task? – Shannon Hochkins Aug 26 '13 at 07:33
  • @ShannonHochkins possibly but **xForm** does not have to exit native code. Timing commands is not that hard you know, test and see for yourself. Besides why would you care on way or another? – joojaa Aug 26 '13 at 07:39
  • I judt hate xform haha, ive never agreed with the way it reads, I much prefer knowing what thr command is doing in the background so I can learn more @joojaa,how can I time them? – Shannon Hochkins Aug 26 '13 at 10:52
  • @ShannonHochkins Honestly in this case the methods we've all shown would be considered unpythonic. Python focuses on readability so this would be fine if you were to write a function for it and call it `centerPivot(obj)`, however `xform` already does that and much more so this is more redundant which is definitely counter productive. – Argiri Kotsaris Aug 26 '13 at 15:26
  • @ShannonHochkins let me point put that neither the above nor the xForm gives you any idea on what they do behind the scenes. The point of using script interfaces is to get things done, not think about performance issues. If you really must obsessively know what something does in the background than Maya is not for you. You need to use some open source software instead. But no good open source 3d application exists (No blender is not good, its just annoying). In any case maya will never be really pythonic because the tool set was written for mel in C++. But then lot of real code is like that. – joojaa Aug 26 '13 at 16:02
  • I dont mean to the point of studying the original code,for example now I know how to reset a pivot without the use of xform, I like growing my knowledge – Shannon Hochkins Aug 26 '13 at 22:00
  • @ShannonHochkins Then a better question would have been how does Maya calculate the center pivot of an object? Would it not? – Argiri Kotsaris Aug 26 '13 at 22:22
  • Well that same question would result in a similar answer, seeing as my title is center pivot without xform, I'm going to get an answer other than xform, but I guess that would have been a good question too – Shannon Hochkins Aug 26 '13 at 23:01
  • I have ran a timeit test, and you were correct @joojaa, it was twice as slow actually! I wouldn't have thought it would but I guess you know alot more :) Thanks for your help guys! – Shannon Hochkins Aug 27 '13 at 02:19
  • I've also edited your question @ArgiriKotsaris if you want to publish it for other who may stumble accross this – Shannon Hochkins Aug 27 '13 at 02:19