Scatter object brush 2D

this is a script I wrote to paint in tree’s on terrain-
once you run the script these are the controles
“LMB:Paint BMB:Quit Shift:Pan Ctrl:BrushSize”

it paints from the selected objects, its a fairly simple script so might be a good example, the panning is usefull in other tools to so you can move the view without restarting the script.

limitations are it only works in top down view (numpad 7), orthographic (numpad 5)

it needs a recent build of blender

randomizing scale and rotation can be done with another script I wrote - its in the object menu “Randomize Loc Size Rot” - that way you can tweak the randomness later on.

Name: 'Brush Selected Objects 2D'
Blender: 234
Group: 'Object'
Tooltip: 'Scatter brush selected objects X/Y'

import Blender
import BPyMesh
import BPyWindow

from Blender import *
mouseViewRay= BPyWindow.mouseViewRay
from Blender import Mathutils, Window, Scene, Draw, sys
from Blender.Mathutils import CrossVecs, Vector, Intersect, LineIntersect, AngleBetweenVecs

LMB= Window.MButs.L
RMB= Window.MButs.R
#MMB= Window.MButs.M
CTRL= Window.Qual.CTRL
SHIFT= Window.Qual.SHIFT
#ALT= Window.Qual.ALT

msg = 'LMB:Paint  BMB:Quit  Shift:Pan  Ctrl:BrushSize - %.2f'

def main():
    brush_size= 5.0
    # new_
    scn= Scene.GetCurrent()
    scn_objects = scn.objects
    objects= list(scn_objects.selected)
    rand_vec = Vector(0,0)
    if not objects:
        Draw.PupMenu('Aborting%t|no objects selected')
    Window.DrawProgressBar(0.0, msg % brush_size)
    z_level = Window.GetCursorPos()[2]
    i= 0 # counter
    mco_old = None
    mouse_buttons = Window.GetMouseButtons()
    while not (mouse_buttons & RMB):
        if mouse_buttons & LMB:
            mco = Window.GetMouseCoords()
            if mco != mco_old:
                mouseInView, OriginA, DirectionA = mouseViewRay(*mco)
                if mouseInView:
                    rand_vec.x = Noise.random()-0.5
                    rand_vec.y = Noise.random()-0.5
                    rand_vec.length = brush_size * Noise.random()
                    # Paint now
                    i +=1
                    if i == len(objects): i= 0
                    ob= objects[i].__copy__()
                    ob.clrParent(2, 1)
                    ob.loc= OriginA.x + rand_vec.x, OriginA.y + rand_vec.y, z_level
                mco_old= mco
            # Pan
            qual= Window.GetKeyQualifiers()
            while qual & SHIFT:
                x,y= Window.GetMouseCoords()
                mouseInView, mid_v, DirectionA = mouseViewRay(x,y, None, True)
                if mouseInView:
                    mouseInView, mouse_v, DirectionA = mouseViewRay(x,y, None, False)
                    if mouseInView:
                        v_off= Vector(Window.GetViewOffset())
                        off= (mouse_v-mid_v) * 0.05
                        Window.SetViewOffset(v_off - off)
                        qual= Window.GetKeyQualifiers()
            # Brush size
            if qual & CTRL:
                mco_old = Window.GetMouseCoords()
                brush_size_old= brush_size
                while qual & CTRL:
                    mco = Window.GetMouseCoords()
                    brush_size = brush_size_old + (mco_old[1] - mco[1]) * 0.01
                    if brush_size<0: brush_size = 0.0 # dissallw neg brushes.
                    # Bruush Size
                    Window.DrawProgressBar(0.0, msg % brush_size)
                    qual= Window.GetKeyQualifiers()
        mouse_buttons = Window.GetMouseButtons()


I’m getting this error

Compiled with Python version 2.4.
Checking for installed Python… got it!
/Users/ton/Desktop/: No such file or directory
Traceback (most recent call last):
File “<string>”, line 11, in ?
File “C:\Program Files\Blender Foundation\Blender.blender\scripts\bpymodules”, line 164

arg, the line indings in got screwed up, though it looks luike your using blender 2.42 because it should be fixed in a recent build.

nop I’m using 242a here!

ack sorry, I thaught I mentioned oit,
youll need a recent cvs build.

That’s mean I have to complile from the source right?

I’ve got CVS client working just a few day ago! and I’ve the blender cvs version downloaded and updated everytime I went to the inthernet; as if I was prepare for this occasion.

I only have bloodshed dev c++ and Borland C++ builder but not ms visual c++ :frowning:

Never done my own complilation of blender is this hard to do?

Never done my own complilation of blender is this hard to do?
It depends on your definition of hard. Compiling is pretty trivial after you survive your first compiling.

Check out
to get started.

well if you’re on windows you can use

it’s what i used.

and ideasaman what exactly does the script do?

nevermind i just finished compiling and tested it.
it makes like randomly placed objects around the cursor

Does anyone has the project file for bloodshed dev c++?

Could you share as I find it very tedious to add file by hand.

If you have bloodshed then the mingw compiler and related tools comes with it. If you also install scons for windows then you should be able to just run the build script. (Although, as always with windows… you’re in for a bumpy ride)

edit, updated the script above to work with 2.49a.