Moving Sphere GUI

Hello,
Im fairly new to python and blender and have a little question. Im trying to create a slider that will move an object (e.g a sphere) along the x axis. The code I have so far, rather than moving the original object along, just creates a new one at the new x co-od. I tried using the getcurrent scene function to select the sphere but that didnt work either. Any suggestions or points in the right direction would be most appreciated!
Thanks!
Mel


import Blender
from Blender import *

sliderx = Draw.Create(1)

def MakeSphere():

    me = Mesh.Primitives.Icosphere()
    myObj = Object.New('Mesh')
    myObj.link(me)
    sc = Scene.GetCurrent()
    sc.link(myObj)

    myObj.LocX = myObj.LocX + sliderx.val

EV_BT_CANCEL = 2
EV_SL_X = 1

line = [None, None, None, None, None, None, None, None, None, None]
line[0] = 5
line[1] = 50

def draw_gui():
    global sliderx
    sliderx = Draw.Slider("X: ", EV_SL_X, 5, line[1], 100, 25, sliderx.val, 0.00, 10, 1, "X cood")
    Draw.PushButton("cancel", EV_BT_CANCEL, 80, line[0], 60, 25, "cancel")
    
def event(event, val):
    if event == Draw.ESCKEY or event == Draw.QKEY:
        stop = Draw.PupMenu("OK?%t|Stop the script")
        if stop == 1:
            Draw.Exit()
            
    if event == Draw.CKEY:
        make = Draw.PupMenu("create sphere?%t|Construct the sphere")
        if make == 1:
            MakeSphere()
            Blender.Redraw()
            
    if not val:
        if event in [Draw.LEFTMOUSE, Draw.MIDDLEMOUSE, Draw.RIGHTMOUSE]:
            MakeSphere()
            Blender.Redraw()
        
def button_event(evt):
        if evt==EV_BT_CANCEL:
            Draw.Exit()
            
Draw.Register(draw_gui, event, button_event)

Try the code underneath, it works on the selected object.


import Blender

sliderx = Blender.Draw.Create(1.0)

def draw_gui():
    global sliderx
    objects = Blender.Object.GetSelected()
    if len(objects)!=0:
        sliderx = Blender.Draw.Slider("X: ",1, 5, 40, 100, 25, sliderx.val, 0.0, 10.0, 1, "X cood")
    else:
        Blender.BGL.glColor3i(0,0,0)
        Blender.BGL.glRasterPos2i(5,40)
        Blender.Draw.Text("Please select an object first","normal")
    Blender.Draw.PushButton("Quit", 2, 5, 5, 60, 25, "Quit")

def move(sliderx):
    if len(Blender.Object.GetSelected())!=0:
        ob = Blender.Object.GetSelected()[0]
        ob.LocX = sliderx.val
    else:
        Blender.Redraw()

def quit():
    stop = Blender.Draw.PupMenu("OK?%t|Stop the script")
    if stop == 1:
        Blender.Draw.Exit()

def event(event, val):
    if event == Blender.Draw.ESCKEY or event == Blender.Draw.QKEY:
        quit()

def button_event(evt):
    if evt==1:
        move(sliderx)
        Blender.Redraw()
    elif evt==2:
        quit()

Blender.Draw.Register(draw_gui, event, button_event)

You can change X pos of some (selected) object with this:


newLocation = 100 # or in your script sliderx.val
obj = Blender.Object.GetSelected()[0]
obj.setLocation(newLocation,0,0)

Edit: Or use that Crouch’s code :slight_smile:

Thank you both very much! I now see where I was going wrong with selecting the object.

Just a follow up question, does anyone know if there is a way to make the move with the slider continuous using the redraw function? As in rather than moving the slider to 8 and the object move there, have it move with the slider as it goes to 8? Or is that not possible?

The tenth parameter in the slider function is the realtime parameter [1]. In the script I posted it should already do what you want, but I just tested it and it doesn’t seem to work.

Perhaps it’s a good idea to start a new thread for your question.