custom objects on blender with python

im new in blender and python… im working on something, but at this point im stuck :frowning:

how can i make my own objects on blender using python? (btw. i am using blender 2.6).

I have a set of balls, these balls can get connected to another balls, and each ball can get attracted too another ball (if the balls are connected). I want the player to be able to control to which ball get closer to… and i also want the ball to be able to get new connections and to break connections.

I’m stuck, because i can’t find a way to achieve this without copy-pasting code.

I think the best way to achieve this would be: to make my own class, let’s call it “Node” class… with an array of “Nodes” as a property, and a method “getCloserTo(index)”, that is going to make “this” node closer to the “Nodes[i]” node, but i don’t where to put this script… do i need some kind of inheritance? do i need to put this script into some sensor? please help! :frowning:

i know this is not impossible because it’d be impossible to make neat games without this feature…

P.S.: sorry for the edit, made the problem a bit more understandable

There are a few ways to do this and this just happens to be the way that I do it.

You create a single Object of your ball in Blender and attach an “Always” sensor set to trigger every frame to a “Python” Controller running in ‘module’ mode. In that script you add can add your custom “Node” class.

Your script can access the Blender KX_GameObject and you can attach arbitrary data to that object. You can make an instance of your Node class and attach it to the KX_GameObject. You need to keep track of initialization and bootstrap data in but other than that it works pretty well.


# ** NOTE: This code is not tested.  It probably has minor syntax and/or logic errors.

import bge

class Node:
  __init__(self):
    # construction code

  main(self, controller):
    # boot-strap method if you want to drive all the logic from this class.

def main(controller):
  """The main entry point for the module.  BGE calls this and it checks for
     initialization and bootstraps to the custom class."""

  # BGE can pass the currently executing controller directly to a module script
  # Access the KX_GameObject that is being processed.
  gameObj = controller.owner

  # See if a custom Node has been attached yet
  if 'instance' not in gameObj:
    gameObj['instance'] = Node()

  # Boot strap to run the code on the attached object.
  gameObj['instance'].main(controller)