Pass iterators as pointers to a function

46 views
Skip to first unread message

Enrico Losavio

unread,
Jun 10, 2014, 9:58:38 AM6/10/14
to python_in...@googlegroups.com
Hi everyone.
I'm new to Maya Python API. I've been studying it since a couple of months and now I'm trying to write my first script (to be later adapted into a node).
I'm writing a main function to iterate through a mesh, and a few secondary functions to retrieve certain information about it.
The point is, everytime i call a function I have to recreate the iterators, function sets and everything i need in order to make it work. This seems a lot like a waste of code  and of performances.
Do you know if there is a way to pass the iterators or any Maya object to a function as pointers? I know python doesn't deal with pointers, but it would be so much easier. I've tried to look in the MScriptUtil documentation, but i couldn't find anything helpful

Thanks for the help!

Marcus Ottosson

unread,
Jun 10, 2014, 10:57:24 AM6/10/14
to python_in...@googlegroups.com

Hi Enrico,

I’m not very acquainted with the API, and maybe I’m not understanding you correctly, but Python passes everything as references to other objects.

# This would pass `my_iterator` as reference to `maya_function`
maya_function(my_iterator)

If you could provide a short snippet of your issue, I might be able to help more.

Best,
Marcus



--
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/d5207b32-1472-4e51-90dd-fc4affbf4154%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Marcus Ottosson
konstr...@gmail.com

Justin Israel

unread,
Jun 10, 2014, 3:26:35 PM6/10/14
to python_in...@googlegroups.com

In addition to what Marcus said about passing the iterator instance, have you profiled your code to come to the conclusion that it is inefficient to create your iterator and function sets from within the function?

Enrico Losavio

unread,
Jun 15, 2014, 6:58:31 PM6/15/14
to python_in...@googlegroups.com
Hey guys, thanks for the reply (and sorry for my late reply).
I've been making some tests and yes, Marcus you're right. I thought that the variables passed to a function were always considered as local variables.
I studied a bit of programming years ago, so I don't remember the right terms, but if i write for example:

x = 5
def double(x):
    x
*= 2
double(x)
print x

it will of course print 5, not 10. 
I was looking for a way to have direct access to the outer variable.
In the case of a Maya Iterator, I've made a quick check simply printing the iterator.index() inside the function to see if it was kepping the pace (and it did).
def printIndex(polyIter):
   
print polyIter.index()

while not polyIter.isDone():
    printIndex
(polyIter)
    polyIter
.next()

Justin, as I was saying i'm not a seasoned programmer and I barely know what profiling the code means. I was just guessing that making unneeded operations would be a loss of performance. 

So that said... how can I tell if I'm passing the variable as a local variable or as a pointer?
I'm sorry if I look confused, but I am. Probably because I'm spending like tens of hours everyday in front of the code, studying and working at the same time and having random epiphanies occasionally :D


Il giorno martedì 10 giugno 2014 21:26:35 UTC+2, Justin Israel ha scritto:

In addition to what Marcus said about passing the iterator instance, have you profiled your code to come to the conclusion that it is inefficient to create your iterator and function sets from within the function?

On Jun 11, 2014 2:57 AM, "Marcus Ottosson" <konstr...@gmail.com> wrote:

Hi Enrico,

I’m not very acquainted with the API, and maybe I’m not understanding you correctly, but Python passes everything as references to other objects.

# This would pass `my_iterator` as reference to `maya_function`
maya_function(my_iterator)

If you could provide a short snippet of your issue, I might be able to help more.

Best,
Marcus

On 10 June 2014 14:58, Enrico Losavio <enrico...@gmail.com> wrote:
Hi everyone.
I'm new to Maya Python API. I've been studying it since a couple of months and now I'm trying to write my first script (to be later adapted into a node).
I'm writing a main function to iterate through a mesh, and a few secondary functions to retrieve certain information about it.
The point is, everytime i call a function I have to recreate the iterators, function sets and everything i need in order to make it work. This seems a lot like a waste of code  and of performances.
Do you know if there is a way to pass the iterators or any Maya object to a function as pointers? I know python doesn't deal with pointers, but it would be so much easier. I've tried to look in the MScriptUtil documentation, but i couldn't find anything helpful

Thanks for the help!

--
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_maya+unsub...@googlegroups.com.



--
Marcus Ottosson
konstr...@gmail.com

--
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_maya+unsub...@googlegroups.com.

Justin Israel

unread,
Jun 15, 2014, 7:09:41 PM6/15/14
to python_in...@googlegroups.com

Hi

Python is a pass-by-value language. And it doesn't have pointers. That means everything you pass is a copy of the value. Now that might sound strange to some, thinking it isn't true part time. When you pass an immutable object like an int,  it is obvious. But when you pass a list,  what you are passing is a copy of the lost reference. That means that while you can't replace the symbol with a new list (no pointers) you can modify it's members and the same list is affected.
So in the case where you want to modify and object being passed in,  either return the new value,  or pass a custom object that has attributes that can be modified. This is why you can advance the iterator. You pass a copy of the reference and then call next.

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/03bf3e54-3547-446d-b865-1d30535bd6d1%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages