Scope Parenting

What’s up Blender Artists? I’ve been wondering how I could parent a scope to the top of a gun when you click on it. So far, I have my FPS setup complete, with an empty on top of the gun where the scope will go. I’m wondering if there’s a way to write a simple script to set the scope’s rotation and position to the empty, and parent it, with python. Any help would be appreciated.

I’ve never added a new object into the scene using Python but I imagine it would be easy.

Have the empty positioned in relation to the gun where the object origin for the scope will be when its added. Parent the empty to the gun.

Then have a mouseover sensor and mouse > left button sensor.

In python write something like if those two sensors are positive, add an object to the scene (being the scope), with the position being the worldPosition() of the empty. Then use setParent() to parent the scope to the empty/gun.

Thats where I’d start anyway. I can’t write the code out line for line for you I’d have to test it on a test scene of my own first :stuck_out_tongue:

If this post doesn’t make much sense to you I’ll take the time to do that.

I don’t need to create the scope object with python, I’m just trying to figure out how to write the script exactly like you said (i don’t know much python :)). I know how to use the world position, but how would I write the empty’s x y and z position in python? Would emptyName.x, emptyName.y, and emptyName.z work (i don’t think so)? That’s what I’m stuck on. Also, do I write anything in in the parentheses on setParent() ?

p.s. Thanks for replying lambofreak!

you would use your emptys.worldPosition just like you would use your scopes worldPosition…

 
if this == True:
     scope.worldPosition = empty.worldPostion

or just one setting

 
if this == True:
     scope.worldPosition[0] = empty.worldPostion[0]

scope.worldPosition[0] = x
scope.worldPosition[1] = y
scope.worldPosition[2] = z

hope thats what your asking about

Not sure about the parenthesis…I use them when the example in the IPO or example file uses them.
I think parenthesis make it return a value…though im not sure

Thanks superflip, I’ll try it out!

I can’t really get it to work. I’m not sure if I wrote it correctly:

import GameLogic

if mouseOver.positive and mouseLeft.positive:
scope1.worldPosition = scopeParent.worldPosition
scope1.setParent(“scopeParent”)

mouseOver and mouseLeft are the names of the sensors. I don’t think it is written correctly. Can anyone help me?

If you already have the object in a different layer then you could just use a python script to set it to the position of the empty
KX_Scene.addObject(Name/Instance of object you want to add, Instance of Object you want to add it to)
or in code (Should be ran from the empty)


import bge
empty=bge.logic.getCurrentController().owner
scene=bge.logic.getCurrentScene()
scene.addObject("Scope",empty)

(Replace Scope with the Object name of the scope)
Should add it relative to the empty’s position/orientation
and in the case that you cant run the script from the empty (key press is on a different object) just replace “empty=bge.logic.getCurrentController().owner” with “empty=bge.logic.getCurrentScene().objects[“Empty”]”

Hope I helped.

looks like it would work…what version of blender are you using…and did you define scope1 and scopeParent within the script? and which object is running the script

I’m using 2.63. Also, I didn’t define scope1 and scopeParent in the script. How would I go about doing this?

this is what the above would look like in a 2.63 script how i would do it…IF the script is ran from a gun or something…not the scope itself


 
import bge
from bge import logic
 
controller = logic.getCurrentController()
owner = controller.owner
scene =  logic.getCurrentScene()
scopeParent = scene.objects["scopeParent"]
scope1 = scene.objects["scope1"]
 
mouseOver = controller.sensors["mouseOver"]
mouseLeft = controller.sensors["mouseLeft"]
 
if mouseOver.positive and mouseLeft.positive:
      scope1.worldPosition = scopeParent.worldPosition
      scope1.setParent(scopeParent, 1, 0) ##added* ty Lambofreak

GameLogic does not work in 2.63 I think

Edit: Added Setparent, I think thats right

The script you’re trying to write is for 2.49, using the GameLogic module. Now we use ‘import bge’ which gives you the bge module and from there you can get bge.logic (more or less the equivalent of GameLogic).

The names of your sensors need to be given a variable otherwise your script will say they aren’t defined.

To do this just write for example ‘leftMouse = cont.sensors[“yourSensorNameHere”]’

Sorry I can’t write the script or make you a demo blend I’m at work right now :stuck_out_tongue:

Ninja Edit: Superflip beat me to it!

Ninja Edit 2: If I could just add a line onto the end of Superflip’s script: ‘scope1.setParent(scopeParent)’ << Not sure if ‘scopeParent’ needs to be in inverted commas or not?

Thanks superflip! :wink: