On Sat, Oct 8, 2022 at 7:14 AM johancc <joao.chave...@gmail.com> wrote:

> Hey, trying to create a radial pattern with a sphere in Python, but
> somehow I am missing the last sphere.
>
> [image: sphere.png]
>
> As you can see by the image and code below, I am trying to distribute 7
> spheres in 180 degrees. If I set it to 360 degrees works fine as it already
> has the last point, the first sphere. But if I set it to 90 or 180 for
> example, it misses the last sphere. I need to somehow add this last sphere,
> but not sure how.
>
> Increasing the number of spheres (max_iterations) won't do the trick as it
> will distribute one more object within a similar radius.
>
> btw, this can easily be done with MASH and duplicate special, but I need
> this in python.
> Thank you.
>
>     import maya.cmds as cmds
>
>     degrees = 180
>     max_iterations = 7
>
>     for i in range(max_iterations):
>         #print (i)
>         rotation = (degrees/max_iterations) * i
>         #print(rotation)
>         my_sphere = cmds.polySphere(r=0.15)
>         cmds.move(2,my_sphere, x=1 , absolute=1)
>         cmds.move( 0,0,0, [ my_sphere[0]+'.scalePivot',
> my_sphere[0]+'.rotatePivot' ], xyz=1, absolute=1 )
>
>         cmds.rotate(rotation, my_sphere, y=1)
>

In the case of a complete circle this rotation calculation works because
the distribution is shorter and doesn't try to place spheres on a scale of
0-360 inclusive. So what you probably want is to actually place a sphere at
the last degree, inclusive.

Original:

degrees = 180
max_iterations = 7

[(degrees / max_iterations * i) for i in range(max_iterations)]#
Result: [0, 25, 50, 75, 100, 125, 150] #

[(degrees * (float(i) / (max_iterations-1))) for i in
range(max_iterations)]# Result: [0.0, 30.0, 60.0, 90.0, 120.0, 150.0,
180.0] #

Now with the updated calculation it creates the last sphere on the upper
limit of degrees (180). But you might not want it distributed that way for
a closed circle where it's going to overlap the first and last? So then
maybe you want to choose something like this?

for i in range(max_iterations):
    if degrees % 360 == 0:
        rotation = float(degrees) / max_iterations * i
    else:
        rotation = float(degrees) * i / (max_iterations-1))
    print("#{}: rotation={}".format(i, rotation))
    my_sphere = cmds.polySphere(r=0.15)
    cmds.move(2,my_sphere, x=1 , absolute=1)
    cmds.move( 0,0,0, [ my_sphere[0]+'.scalePivot',
my_sphere[0]+'.rotatePivot' ], xyz=1, absolute=1 )

    cmds.rotate(rotation, my_sphere, y=1)


-- 
> 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+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/python_inside_maya/32352ddd-7840-4e3a-a0da-194d7410b28bn%40googlegroups.com
> <https://groups.google.com/d/msgid/python_inside_maya/32352ddd-7840-4e3a-a0da-194d7410b28bn%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA1cQeBzxu4Ajah_a0jD0CgZ%2BQmqi9rHumCK8FpE6M-QNQ%40mail.gmail.com.

Reply via email to