Dynamic move object with Python Common

Hi Steve
I want move one object dynamically but the below Python code don’t make it, where is my error?:

#-*- encoding: utf-8 -*-
#  Rhino Common  Dynamic move object

import Rhino
import scriptcontext
import rhinoscriptsyntax as rs
def move_obj():
    obj=rs.GetObject("select one object")
    
    pt_start=rs.GetPoint("Reference point")  # con metodo script     
    #************** funzione che disegna dinamicamente *********************
    
    def GetPointDynamicDrawFunc( sender, args ): 
        vect=args.CurrentPoint-pt_start
        translation = rs.coerce3dvector(vect, True)
        translation=Rhino.Geometry.Transform.Translation(translation)
        print translation
        a=Rhino.DocObjects.RhinoObject.GetDynamicTransform(obj,translation)
        print a    
        
    gp = Rhino.Input.Custom.GetPoint()#creo oggetto gp    
    gp.DynamicDraw += GetPointDynamicDrawFunc  # chiamo in continuazione la funzione che mi mostra la linea dinamica
    gp.Get()   # qui ottengo i dati quando clicco     
    if( gp.CommandResult() == Rhino.Commands.Result.Success ):
        pt = gp.Point() 
        print pt

move_obj()

Ciao Vittorio

1 Like

You don’t do any drawing in “GetPointDynamicDrawFunc” you only calculate the translation.

args.Display.DrawObject(...)

Hi Menno

I add a line args.Display.DrawObject(obj) but nothing works.
Can You Help me?

#-*- encoding: utf-8 -*-
#  Rhino Common  Dynamic move object
import System.Drawing.Color
import Rhino
import scriptcontext
import rhinoscriptsyntax as rs
def move_obj():
    obj=rs.GetObject("select one object")    
    
    obj_color=System.Drawing.Color.Black
    pt_start=rs.GetPoint("Reference point")  # con metodo script     
    #************** funzione che disegna dinamicamente *********************
    
    def GetPointDynamicDrawFunc( sender, args ): 
        vect=args.CurrentPoint-pt_start        
        translation=Rhino.Geometry.Transform.Translation(translation)        
        Rhino.DocObjects.RhinoObject.GetDynamicTransform(obj,translation)        
        args.Display.DrawObject(obj) #What should I put in brackets?
        #args.Display.DrawObject(obj,obj_color) #that does not work 
    gp = Rhino.Input.Custom.GetPoint()#creo oggetto gp    
    gp.DynamicDraw += GetPointDynamicDrawFunc  # chiamo in continuazione la funzione che mi mostra la linea dinamica
    gp.Get()   # qui ottengo i dati quando clicco     
    if( gp.CommandResult() == Rhino.Commands.Result.Success ):
        pt = gp.Point() 
        print pt

move_obj()

Try this

    def GetPointDynamicDrawFunc( sender, args ): 
        vect=args.CurrentPoint-pt_start        
        translation=Rhino.Geometry.Transform.Translation(vect)        
        args.Display.DrawObject(obj.Geometry, translation)

I created this in C# to test:


protected override Result RunCommand(RhinoDoc doc, RunMode mode)
{
    ObjRef oRef;
    Result res = RhinoGet.GetOneObject("select one object", false, ObjectType.AnyObject, out oRef);
    if (res != Result.Success) return res;

    RhinoObject obj = oRef.Object();
    GeometryBase geo = obj.Geometry;
    Point3d from;
    res = RhinoGet.GetPoint("select ref point", false, out from);
    if (res != Result.Success) return res;

    GetPoint gp = new GetPoint();
    gp.DynamicDraw += (o, a) =>
                            {
                                Vector3d vect = a.CurrentPoint - from;
                                Transform trans = Transform.Translation(vect);
                                a.Display.DrawObject(obj, trans);
                            };
    GetResult result = gp.Get();
    if (result == GetResult.Point)
    {
        Point3d to = gp.Point();
        geo.Transform(Transform.Translation(to - from));
        doc.Objects.Add(geo);
    }

    return Result.Success;
}

#-*- encoding: utf-8 -*-
#  Rhino Common  Dynamic move object
import System.Drawing.Color
import Rhino
import scriptcontext
import rhinoscriptsyntax as rs


def move_obj():
    rc, objref = Rhino.Input.RhinoGet.GetOneObject("Select object", True, Rhino.DocObjects.ObjectType.AnyObject)
    if rc!=Rhino.Commands.Result.Success: return
    obj = objref.Object()
    if not obj: return
    rc, pt_start = Rhino.Input.RhinoGet.GetPoint("Start point", False)
    if( rc!=Rhino.Commands.Result.Success ): return 
    def GetPointDynamicDrawFunc( sender, args ): 
        vect=args.CurrentPoint-pt_start        
        translation=Rhino.Geometry.Transform.Translation(vect)          
        args.Display.DrawObject(obj,translation) 
    gp = Rhino.Input.Custom.GetPoint()
    gp.DynamicDraw += GetPointDynamicDrawFunc
    gp.Get()   
    if( gp.CommandResult() == Rhino.Commands.Result.Success ):
        pt = gp.Point() 
        print pt

move_obj()
1 Like

https://github.com/dalefugier/SamplePy/blob/master/SampleMove.py

1 Like