maya api question

72 views
Skip to first unread message

likage

unread,
Jul 25, 2016, 9:04:35 PM7/25/16
to Python Programming for Autodesk Maya
Hi all I am trying to do and learn some maya api (I have zero knowledge of them)
The following code was taken from an example that I have read online and while testing it out, I have a few questions in mind..

1. In that test() function, as the world_pos variable is calling out to the Point class, how would I know which of the 4 functions in class Point it is trying to call from?
2. Also, I tried to insert a print statement to show me what type it will return by using type(world_pos), it give me result <class 'spPaint3dContext.Point'> as I was expecting results such as dict, list etc. Then when I insert it into the functions within class, it is telling me that it is part of MVector etc.. 
So, is that any ways that I can expect it to tell me similar like dict/list etc?
class Point(object):
   
def __init__(self, x, y, z):
       
self.x = x
       
self.y = y
       
self.z = z


   
def asMPoint(self):
       
return om.MPoint(self.x, self.y, self.z)


   
def asMFPoint(self):
       
return om.MFloatPoint(self.x, self.y, self.z)


   
def asMVector(self):
       
return om.MVector(self.x, self.y, self.z)


   
def asMFVector(self):
       
return om.MFloatVector(self.x, self.y, self.z)
       
       
def test(v1, v2):
   
...
   
...
    click_pos
= om.MPoint()


    world_pos
= Point(click_pos.x, click_pos.y, click_pos.z)
   
# Suppose the following values for click _pos x, y and z...
   
# click_pos.x : -13.2409
   
# click_pos.y : 44.7756
   
# click_pos.z : -66.3071


This is a bit off-topic but I have this function where it has '2' return variables but they are controlled by this 'smooth' parameter. 
The reason that this function was wrote it this way was due to a checkbox, thus if it is checked - smooth = True, unchecked - smooth = False

Was wondering if this is an acceptable way for me to write it this way?
def getNormal(self, smooth = False):
   
if smooth:
        normal
= om.MVector()
        fn_mesh
= om.MFnMesh(self.dagMeshTargetSurface)
        fn_mesh
.getClosestNormal(self.hitPoint.asMPoint(), normal, om.MSpace.kWorld, None)


       
return normal
   
else:
        hit_facept
= om.MScriptUtil()
        om
.MScriptUtil().setInt(hit_facept.asIntPtr(),self.hit_face)
        itMesh
= om.MItMeshPolygon(self.dagMeshTargetSurface)
        itMesh
.setIndex(self.hit_face, hit_facept.asIntPtr())
        triVertsArray
= om.MPointArray()
        triIndexArray
= om.MIntArray()
        itMesh
.getTriangle(self.hit_triangle, triVertsArray, triIndexArray, om.MSpace.kWorld)


       
return self.getCrossProduct(triVertsArray[0], triVertsArray[1], triVertsArray[2])

Justin Israel

unread,
Jul 26, 2016, 1:26:41 AM7/26/16
to python_in...@googlegroups.com
On Tue, Jul 26, 2016 at 1:04 PM likage <dissid...@gmail.com> wrote:
Hi all I am trying to do and learn some maya api (I have zero knowledge of them)
The following code was taken from an example that I have read online and while testing it out, I have a few questions in mind..

1. In that test() function, as the world_pos variable is calling out to the Point class, how would I know which of the 4 functions in class Point it is trying to call from?

It is calling none of them. Only your __init__() (constructor) is being called in your example.
 
2. Also, I tried to insert a print statement to show me what type it will return by using type(world_pos), it give me result <class 'spPaint3dContext.Point'> as I was expecting results such as dict, list etc. Then when I insert it into the functions within class, it is telling me that it is part of MVector etc.. 
So, is that any ways that I can expect it to tell me similar like dict/list etc?

Why do you thing it would be returning a list vs a dict vs other? And I don't mean these questions in a condescending way. I think it would be good to hear your thought process on how you view the way a class works. Then we can address the specifics. What do you expect to get back when you construct a Point object in different ways? How would you expect to see a list vs a dict result? Given that your example never calls any of your 4 extra methods, how do you envision them being called implicitly?
 
--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/76b30274-4600-4022-a450-90b9c75c399b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Alok Gandhi

unread,
Jul 26, 2016, 5:17:58 AM7/26/16
to python_in...@googlegroups.com
I would comment on the second part of your question :
Regarding the getNormal() method, the short answer is yes. To write clean readable code, the only thing that is of importance (more so in python) is to name your methods, variable with utmost care. Since getNormal() would return a MVector in both cases (and here I am assuming that your getCrossProduct() method returns a MVector) it is fine to write that code. With the name "getNormal" you are making a promise that the method will return a 'normal' (which here semantically means MVector), as long as you keep that promise, it is acceptable by all means.


For more options, visit https://groups.google.com/d/optout.



--

likage

unread,
Jul 26, 2016, 12:51:38 PM7/26/16
to Python Programming for Autodesk Maya
Why do you thing it would be returning a list vs a dict vs other? And I don't mean these questions in a condescending way. I think it would be good to hear your thought process on how you view the way a class works. Then we can address the specifics. What do you expect to get back when you construct a Point object in different ways? How would you expect to see a list vs a dict result? Given that your example never calls any of your 4 extra methods, how do you envision them being called implicitly?

I was expecting it to return it in terms that I understand... Truth be told, I do not have the slightest idea of what is being returned when using maya api as I have no knowledge in the api area, a total noob here..
In python term, for example, if I am using [], I know that if I type in type(), it will give me <type 'list'> as the result, returning it as list.

Correct me if I am wrong but can I assume that in api term, the methods that I am using, for example om.MPoint, om.MFloatVector, the 'term' that it will return will be called MPoint and MFloatVector? Again, I apologize if this sounds like a stupid question

likage

unread,
Jul 26, 2016, 1:04:10 PM7/26/16
to Python Programming for Autodesk Maya
I would comment on the second part of your question :
Regarding the getNormal() method, the short answer is yes. To write clean readable code, the only thing that is of importance (more so in python) is to name your methods, variable with utmost care. Since getNormal() would return a MVector in both cases (and here I am assuming that your getCrossProduct() method returns a MVector) it is fine to write that code. With the name "getNormal" you are making a promise that the method will return a 'normal' (which here semantically means MVector), as long as you keep that promise, it is acceptable by all means.

Okay, while I understand what you are saying but (this may sounds stupid of me) when smooth is True, I thought it will return a MVector while False, it will return MPoint?
Rather, how can I tell what does it returns?

Justin Israel

unread,
Jul 26, 2016, 4:22:38 PM7/26/16
to Python Programming for Autodesk Maya
This isn't a Maya specific problem though, from what I can see. In your example you never call any of your 4 specialized methods. You only create a Point (a custom python class you have defined)  with some params. So let's pretend that there is no Maya API under the hood here. What I was wondering is how you figured any of those methods would magically get called for you without you calling them directly? Or is your test function incomplete and there is more detail that you have not shared? 

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

likage

unread,
Jul 26, 2016, 6:39:38 PM7/26/16
to Python Programming for Autodesk Maya
I do not have any code to show. This is something that I have copied and paste somewhere..

But if say I manage to have some values and I set a variable as 'test.asMPoint()', am I correct to say that it will try to read in 3 values and it will be using the class Point, asMPoint function?

And hence my return output will be a MPoint?
 
Pardon me that this is a very bad example..

Justin Israel

unread,
Jul 26, 2016, 6:44:51 PM7/26/16
to python_in...@googlegroups.com
On Wed, Jul 27, 2016 at 10:39 AM likage <dissid...@gmail.com> wrote:
I do not have any code to show. This is something that I have copied and paste somewhere..

But if say I manage to have some values and I set a variable as 'test.asMPoint()', am I correct to say that it will try to read in 3 values and it will be using the class Point, asMPoint function?

And hence my return output will be a MPoint?

Yes exactly. That was the missing bit. You weren't showing the part that you just described. 

When you call asMPoint(), you will be creating an instance of MPoint from your instance variables x, y, and z and returning it. It should be pretty obviously what is being called, when you actually call it directly.


 
 
Pardon me that this is a very bad example..

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

Alok Gandhi

unread,
Jul 26, 2016, 7:16:02 PM7/26/16
to python_in...@googlegroups.com
You are returning a cross product when smooth is false. A cross product is a vector. Also as I said before, your example does not show implementation of getCrossProduct() so it is not clear what actually you are returning. But again, as I mentioned earlier, if your method name is getCrossProduct() then it _should_ return an MVector. As to how can you tell what it is returning, as part of the debugging process, you can always check with type() but remember type will only return the immediate (the class) but it will not cover the inheritance. To check that you have to use isinstance().

Here is an example:
>>> class Test1 (object):
        pass
>>> class Test2 (Test1):
        pass
>>> a = Test1()
>>> b = Test2()
>>> type(a) is Test1
True
>>> type(b) is Test2
True

>>> type(b) is Test1
False

>>> isinstance(b, Test1)
True
>>> isinstance(b, Test2)
True
>>> isinstance(a, Test1)
True
>>> isinstance(a, Test2)
False
>>> isinstance([], list)
True
>>> isinstance({}, dict)
True

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--

likage

unread,
Jul 27, 2016, 3:20:14 PM7/27/16
to Python Programming for Autodesk Maya
Yes exactly. That was the missing bit. You weren't showing the part that you just described. 
 
When you call asMPoint(), you will be creating an instance of MPoint from your instance variables x, y, and z and returning it. It should be pretty obviously what is being called, when you actually call it directly.

In this code of mine, I was getting confused because it seems to be calling a function within a function / function within a class etc, again, this is just a small portion of the code..
For eg. 
current_pt = om.MFloatPoint()

hit_01
= om.MScriptUtil()
hit_02
= om.MScriptUtil()
hit_01
.createFromInt(0)
hit_02
.createFromInt(0)
...
...

return FindThis(Point(current_pt.x, current_pt.y, current_pt.z), hit_01.getInt(...), ...)

Basically it seems to be 'calling' from all other classes/functions in which they are using maya api too.
Which is why when I tried to do a debug trying to find out what types the variables are, I had thought it will give me a more direct answer - MVector, MPoint etc, just like the type() command which will tell me whether if it is a dict, str etc.

Pardon me if I did not phrase it all out correctly and properly which has causes some misunderstandings/ frustrations...

Justin Israel

unread,
Jul 27, 2016, 5:46:11 PM7/27/16
to python_in...@googlegroups.com
Unfortunately I am still a bit confused about your question...
It is the bit about "calling from all other classes". When I look at your example code, it seems very apparent to me that you are constructing your own custom Point() instance, by passing some data into it. The Point instance is passed to the FindThis() call, along with the results of those getInt() calls. 

 

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages