[CODE] Camera settings

[CODE] Camera settings

Postby chrisglasier » Tue Feb 09, 2010 2:52 am

If anyone has any ready made snippets of code for retrieving and setting the camera I would be grateful.

Thanks
0
With JSON machines we can analyse what is to be achieved so that IT can help with automation to achieve it.
User avatar
chrisglasier 
PluginStore Author
PluginStore Author
 

Re: [CODE] Camera settings

Postby todd burch » Tue Feb 09, 2010 5:35 am

Do you mean like
Code: Select all
eye = Sketchup.active_model.active_view.camera.eye
target = Sketchup.active_model.active_view.camera.target
up = Sketchup.active_model.active_view.camera.up
 
# flip the camera upside down
up.reverse!

Sketchup.active_model.active_view.camera.set(eye, target, up)


?
0

todd burch 
 

Re: [CODE] Camera settings

Postby chrisglasier » Tue Feb 09, 2010 5:43 am

Thanks Todd. If possible I want to code for whatever saving this camera location does ...
camera.png
.
0
With JSON machines we can analyse what is to be achieved so that IT can help with automation to achieve it.
User avatar
chrisglasier 
PluginStore Author
PluginStore Author
 

Re: [CODE] Camera settings

Postby chrisglasier » Tue Feb 09, 2010 5:56 am

So using your snippets I get two arrays to save as plain text and then return to set up the same scene.

camera002.png


Would that work, or is there something I should know about (seems too easy!)

Thanks
0
With JSON machines we can analyse what is to be achieved so that IT can help with automation to achieve it.
User avatar
chrisglasier 
PluginStore Author
PluginStore Author
 

Re: [CODE] Camera settings

Postby todd burch » Tue Feb 09, 2010 6:09 am

It can be that easy. It depends on where and how you are saving the information, and how much info you want to save.

If you browse the methods for the Camera class, there are a few others that can be saved as well, such as fov, perspective and aspect_ratio.

For positioning the camera, all you need is camera.eye and camera.target and camera.up. The other info is camera settings.
0

todd burch 
 

Re: [CODE] Camera settings

Postby Chris Fullmer » Tue Feb 09, 2010 6:10 am

I thought there was an easier way to do this, but you can just create a new camera object using all the data of the current view camera. Like this:

Code: Select all
cam = Sketchup.active_model.active_view.camera
cam1 = Sketchup::Camera.new(cam.eye, cam.target, cam.up, cam.perspective?, cam.fov)


Then to activate your new camera, use:

Sketchup.active_model.active_view.camera=cam1

That should do the trick. Again though, for some reason I was thinking there was an easier way to clone a camera. But just specifying every attribute one by one on creation seems to work fine.

Chris
0
Lately you've been tan, suspicious for the winter.
All my Plugins I've written
User avatar
Chris Fullmer 
SketchUp Team
SketchUp Team
 

Re: [CODE] Camera settings

Postby chrisglasier » Tue Feb 09, 2010 6:20 am

@Todd

Not quite there yet but I guess it will be saved something like:

Code: Select all
{"Camera:{"eye":[993.128, -480.024, 2582.52],"target":[215.488, 474.905, 217.629],"up":[-0.560065, 0.68775, 0.461873]}}


Thanks I will give the other things a whirl when I am more conversant with the basics.
0
Last edited by chrisglasier on Tue Feb 09, 2010 7:58 am, edited 1 time in total.
With JSON machines we can analyse what is to be achieved so that IT can help with automation to achieve it.
User avatar
chrisglasier 
PluginStore Author
PluginStore Author
 

Re: [CODE] Camera settings

Postby chrisglasier » Tue Feb 09, 2010 6:27 am

Chris Fullmer wrote:I thought there was an easier way to do this, but you can just create a new camera object using all the data of the current view camera. ...

That should do the trick.

Chris


Thanks Chris - crossed with my last post - takes a little time for the notification to hobble over here. I will have a look after lunch, but at first look seems there are no strings.
0
With JSON machines we can analyse what is to be achieved so that IT can help with automation to achieve it.
User avatar
chrisglasier 
PluginStore Author
PluginStore Author
 

Re: [CODE] Camera settings

Postby Chris Fullmer » Tue Feb 09, 2010 6:30 am

I really recommend stealing those values from the current view camera, like in my example. Unless you really need to set them from some other source.

The camera is really pretty simple. Just dig around the methods in the camera class, and notice there are 2 methods in the View class of worth (View.camera to get the current camera object and View.camera= to set the view to a camera object you have made).

That is pretty much it. Make a camera object, then set it to active if you want to see through that camera.

EDIT: Strings? Not quite sure what you mean by that. I hope that means you think that my example looks easy enough to implement :)
0
Lately you've been tan, suspicious for the winter.
All my Plugins I've written
User avatar
Chris Fullmer 
SketchUp Team
SketchUp Team
 

Re: [CODE] Camera settings

Postby chrisglasier » Tue Feb 09, 2010 7:15 am

Chris Fullmer wrote:EDIT: Strings? Not quite sure what you mean by that. I hope that means you think that my example looks easy enough to implement :)


Yes I thought it might be taken to mean that, but I was under heavy "dumplings getting cold" pressure. "Sik fan" (lit. eat rich) is ignored at great peril. I meant I need to store everything as strings (json).

But I think what you propose will be very useful for animation - something for the future. Thanks.
0
With JSON machines we can analyse what is to be achieved so that IT can help with automation to achieve it.
User avatar
chrisglasier 
PluginStore Author
PluginStore Author
 

Re: [CODE] Camera settings

Postby Chris Fullmer » Tue Feb 09, 2010 9:26 am

Well they can all be converted to strings, right? And then turned back into floats or true/false type values if you bring them back into SU. Am I missing something here? This is the answer to the question, unless I'm misunderstanding the question.
0
Lately you've been tan, suspicious for the winter.
All my Plugins I've written
User avatar
Chris Fullmer 
SketchUp Team
SketchUp Team
 

Re: [CODE] Camera settings

Postby chrisglasier » Tue Feb 09, 2010 9:37 am

Chris Fullmer wrote:Well they can all be converted to strings, right? And then turned back into floats or true/false type values if you bring them back into SU. Am I missing something here? This is the answer to the question, unless I'm misunderstanding the question.


I see. I have never done anything with the camera coding, that is why I asked the question in advance of starting that work. I am now doing exposing layers that have mixed groponents/dimensions/text, so perhaps I was not concentrating properly. But I hope to start/finish on the camera in a couple of days, so all the possibilities are most welcome - thanks Chris
0
With JSON machines we can analyse what is to be achieved so that IT can help with automation to achieve it.
User avatar
chrisglasier 
PluginStore Author
PluginStore Author
 

Re: [CODE] Camera settings

Postby DavidBoulder » Thu Feb 11, 2010 5:28 pm

I have a script I'm working on where I'm trying to iterate through scenes/pages and look for faces parallel to the camera for each scene. I have the code to iterate through faces and test to see if parallel to the camera, but I could find a way to grab the camera for other scenes to iterate my tests across all scenes.

I suppose I can change to each scene and then get current camera view, but I would think I should be able to grab the camera from a scene without having to change to that scene. Keep in mind, I don't know the scene names.

edited content below
----------------------------------------
OK, I solved my problem, here is a piece of the code.
Code: Select all
# draft script - David Goldwasser
# find faces parallel to camera and draw lines back to camera

model = Sketchup.active_model
entities = model.active_entities
pages = model.pages

faces = []
entities.each do |n|
 faces.push n if n.is_a? Sketchup::Face
end

pages.each do |page|
camera = page.camera
eye = camera.eye
 faces.each do |face|
  status = camera.direction.parallel? face.normal
  if status then
  edges = face.edges
  edges.each do |edge|
   pts = []
   pts [0] = eye
   pts[1] = edge.start.position
   pts[2] = edge.end.position
   face = entities.add_face pts
   end
  else
  end
 end
end


The pages.each do is where I was having problems before. I was actually switching to that page and then getting current view, camera etc. But the animation was causing problems, so I had to have a dummy UI.messagebox to get it to behave. Now I don't and it is much cleaner.

So I have an edges.each nested in a faces.each, nested in a pages.each. My indentation for isn't so clear when previewed here.
0
--

David Goldwasser
OpenStudio Developer
National Renewable Energy Laboratory
User avatar
DavidBoulder 
 

Result

Postby chrisglasier » Fri Feb 12, 2010 5:38 am

Thanks for your help.



For anyone interested here are extracts from the rather clunky code I ended up to support this v quick video hinting at the potential.

Ruby extractor:
Code: Select all
pages   = model.pages
      layers   = model.layers
      styles   = model.styles
      @parray   =[]
      pages.each do |page|
         model.pages.selected_page = page
         cam  = page.camera
         name = page.name
         pl = []
            page.layers.each do |layer|
               pl.push(layer.name)
            end
         pl = pl.join(",")
         st = page.style
         st = st.name
         pts = [cam.eye, cam.target, cam.up]
         per = cam.perspective?
         if(cam.perspective?)
            fov = cam.fov
            sub = [pts,per,fov]
         else
            sub = [pts,per]
         end
         sub = sub.join("yy")
         entry = [name,pl,st,sub]
         @parray.push(entry.join("xx"))   


JS converter
Code: Select all
p = ret[3].split(";");
   pages = [];
   for (a = 0; a < p.length; a += 1){
      p[a]   = p[a].split("xx");
      lgth   = fLength(nset);
      lab      = lgth.toString();
      o      = {};
      o.Type   = "Scene";
      o.Genus   = "New";
      o.Name   = p[a][0];
      o.Backlink   = [];
      o.Backlink.push(sRoot);
      o.Hidden   = [];
      nset[lab]   = o;
      nset[sRoot].Forelink.push(lab);
      pages.push(lab);
      nset[lab].Forelink = [];
      //mark hidden layers
      //alert(p[a])
      pp       = p[a][1].split(",");
      
      for (c = 0; c < pp.length; c += 1){
         for (d = 0; d < combo.length; d += 1){
            if(pp[c] === nset[combo[d]].Name){
               nset[combo[d]].Filter = 1;
            }
         }
      }
      for (d = 0; d < combo.length; d += 1){
         nset[combo[d]].Backlink = [];
         if(!nset[combo[d]].Filter){
            nset[lab].Forelink.push(combo[d].toString());
            nset[combo[d]].Backlink.push(lab);
         }
         else{
            delete nset[combo[d]].Filter;
         }
      }
      //name,pl,st,camry
   nset[lab].Style = p[a][2];
   nset[lab].Camera = p[a][3];
   }      


JS scene call
Code: Select all
function fScenes(){
   var cell,nt,node,name,kids,dn,cm,genus,cmps;
   cell = document.getElementById("ScenesOn")
   nt      = config.nametrail;
   node   = nt[nt.length-1][0];
   type   = nset[node].Type;
   if(type === "Scene"){
      name   = nset[node].Name;
      kids   = nset[node].Forelink;
      dn      = [];
      cm      = [];
      for(a = 0; a <kids.length; a += 1){
         genus = nset[kids[a]].Genus
         if(genus === "Dimension" || genus === "Notes"){
            dn.push(nset[kids[a]].Name);
            continue;
         }
         dn.push(nset[kids[a]].Name);
         cmps = nset[kids[a]].Forelink;
         for(b = 0; b <cmps.length; b += 1){
            cm.push(nset[cmps[b]].Index);
         }      
      }
      name   = nset[node].Name;
      style   = nset[node].Style;
      rec      = nset[node].Camera.split("yy");
      cam    = [];      //eye,target,up
      for(a=0; a < 3; a += 1){
         cam.push(rec[a].slice(1,-1));
      }
      cam.push(rec[3])
      if(rec[3]){
         cam.push(rec[4]);
      }
      data   = [name,dn.join(","),cm.join(","),style,cam];
      document.getElementById("trans").value = data.join("|");
      window.location = "skp:scene";
   }
}


Ruby callback
Code: Select all
@dlg.add_action_callback("scene") {|d, p|
      a = @dlg.get_element_value("trans")
      a = a.split("|")
      name   = a[0].split(",")
      lyrs   = a[1].split(",")
      cmps   = a[2].split(",")
      sty      = a[3].to_s
      cam      = a[4].split(",")
      model   = Sketchup.active_model
      entities   = model.entities
      entities.each do |entity|
         if  entity.class == Sketchup::ComponentInstance || entity.class == Sketchup::Group
            entity.visible = false
         end
      end
      cmps.each do |c|
         a = Integer(c)
         #p entities[a].name
         entities[a].visible = true
      end
      layers = model.layers
      layers.each do |layer|
      layer.visible = false
      end   
      lyrs.each do |lyr|
         layers[lyr].visible = true;
         #p layers[lyr].name
      end   
      
      pages   = model.pages
      styles   = model.styles
      page   = pages["Nameset"]
      styles.selected_style= styles[sty]
      page.use_style = styles.selected_style
      eye      = Geom::Point3d.new [Float(cam[0]).mm, Float(cam[1]).mm, Float(cam[2]).mm]
      target   = Geom::Point3d.new [Float(cam[3]).mm, Float(cam[4]).mm, Float(cam[5]).mm]
      up   = Geom::Vector3d.new [Float(cam[6]), Float(cam[7]), Float(cam[8])]
      camera = model.active_view.camera
      camera.set(eye,target,up)
      if cam[9] == "true"
         camera.perspective = true
      else
         camera.perspective = false
      end
      if camera.perspective?
         camera.fov = Float(cam[10])
      end
   }   



It seems to work OK but I don't like the zoom extents (I'm working on that).

As they say in the gallery ... C&C welcome.

Edit 15 Feb: Changed code in callback to match post post revelations
0
Last edited by chrisglasier on Wed Feb 17, 2010 9:51 am, edited 2 times in total.
With JSON machines we can analyse what is to be achieved so that IT can help with automation to achieve it.
User avatar
chrisglasier 
PluginStore Author
PluginStore Author
 

Zoom extents problem

Postby chrisglasier » Mon Feb 15, 2010 3:04 am

When I regenerate a scene without zoom extents the scale is very small. Zoom extents would be OK if I didn't want to capture the original zoom that produced close ups for example. But I think it important, so if anyone can help I would be grateful.

Thanks
0
With JSON machines we can analyse what is to be achieved so that IT can help with automation to achieve it.
User avatar
chrisglasier 
PluginStore Author
PluginStore Author
 

Re: [CODE] Camera settings

Postby Chris Fullmer » Mon Feb 15, 2010 4:40 am

I'm not quite sure what you mean. Zoom extents is a separate matter, not really related to scenes, except that it can change the camera position.

So do you want to use zoom extents? Or not use it? Or do you want to match the scene camera settings precisely? Or do you want to get the scene camera, zoom extents, then update the scene camera to reflect the new camera? Or probably something entirely different that I'm missing? :)

Chris
0
Lately you've been tan, suspicious for the winter.
All my Plugins I've written
User avatar
Chris Fullmer 
SketchUp Team
SketchUp Team
 

Re: [CODE] Camera settings

Postby chrisglasier » Mon Feb 15, 2010 4:54 am

Chris Fullmer wrote: ... Or do you want to match the scene camera settings precisely?

Yes I want to match them precisely. I just used zoom extents because the display was ridiculously tiny. Do you have have an idea why?


When zooming you change the camera position or something else? I didn't use new camera but active camera, perhaps that is what is wrong. All the scenes are reconstructed in the one Nameset tab by showing/hiding components (only layers for dims and notes).

Thanks
0
With JSON machines we can analyse what is to be achieved so that IT can help with automation to achieve it.
User avatar
chrisglasier 
PluginStore Author
PluginStore Author
 

Re: [CODE] Camera settings

Postby Chris Fullmer » Mon Feb 15, 2010 5:04 am

So to further understand now: You have an existing model with scenes in it. Then you iterate otver the scenes and get all the cameras and save the camera settings. Then you want to use your script to re-create the cameras from the scenes. Is that the idea?

If so, then what your script does should not differ from the cameras in the scenes. so if the camera is zoomed way out in the scene, then that is what your script should make too. IS this not currently what you are achieving?

If that is the case, then it sounds like somehow you are missing something in the camera settings when you extract them from the scenes. And the field of view might be my guess. Are you checking or setting the Field of View at any point in your script? I think ideall you would check the FOV of the scene camera and then st your camera to match when you create your camera object.

Chris
0
Lately you've been tan, suspicious for the winter.
All my Plugins I've written
User avatar
Chris Fullmer 
SketchUp Team
SketchUp Team
 

Re: [CODE] Camera settings

Postby chrisglasier » Mon Feb 15, 2010 5:24 am

Chris Fullmer wrote:So to further understand now: You have an existing model with scenes in it. Then you iterate otver the scenes and get all the cameras and save the camera settings. Then you want to use your script to re-create the cameras from the scenes. Is that the idea?


Yes - maybe recreate views of the model is a better term.

If so, then what your script does should not differ from the cameras in the scenes. so if the camera is zoomed way out in the scene, then that is what your script should make too. IS this not currently what you are achieving?


Please see pixs and code.

If that is the case, then it sounds like somehow you are missing something in the camera settings when you extract them from the scenes. And the field of view might be my guess. Are you checking or setting the Field of View at any point in your script? I think ideall you would check the FOV of the scene camera and then st your camera to match when you create your camera object.


Please have a look at the code. From the console and js output it seems the fov does not change. Perhaps I have made the error there.
0
With JSON machines we can analyse what is to be achieved so that IT can help with automation to achieve it.
User avatar
chrisglasier 
PluginStore Author
PluginStore Author
 

Re: [CODE] Camera settings

Postby chrisglasier » Mon Feb 15, 2010 5:28 am

... and the non perspective views are the same ...
0
With JSON machines we can analyse what is to be achieved so that IT can help with automation to achieve it.
User avatar
chrisglasier 
PluginStore Author
PluginStore Author
 

Re: [CODE] Camera settings

Postby Chris Fullmer » Mon Feb 15, 2010 6:14 am

I can't parse your code very quickly Chris. Especially once it goes out to js, I'm lost. But it looks like you are doing the right thing by getting the fov for the perspective cameras. But somewhere in there, something is getting messed up. If you get the values correctly and apply them back correctly, the cameras will match 100%. There should be no need to do a zoom extents. So it is clear that somewhere the values are not coming through correctly. I would do some solid value testing. Output all the values before they go to js, then output them again as you recreate the camera. See if they differe at all.

Also, if you could make a model with a scene, then run your script on it and recreate that camera, then create a new scene that uses your camera info it might be easier to compare the settings of the 2 cameras - the original and your recreation.

Chris
0
Lately you've been tan, suspicious for the winter.
All my Plugins I've written
User avatar
Chris Fullmer 
SketchUp Team
SketchUp Team
 

Re: [CODE] Camera settings

Postby chrisglasier » Mon Feb 15, 2010 6:33 am

Thanks for all your effort Chris. Just by posting those pix .mm came to mind ... errgh ... but somehow oddly satisfying. First go seems to work but still not quite it. You know when the French gave you the Statue of Liberty, it would have been nice if you had taken up their metric system.

Tally ho!
0
With JSON machines we can analyse what is to be achieved so that IT can help with automation to achieve it.
User avatar
chrisglasier 
PluginStore Author
PluginStore Author
 

Re: [CODE] Camera settings

Postby Chris Fullmer » Mon Feb 15, 2010 6:49 am

Hehe, yeah sorry about that imperial vs. metric thing. I hope you're able to pin down the problems!

Chris
0
Lately you've been tan, suspicious for the winter.
All my Plugins I've written
User avatar
Chris Fullmer 
SketchUp Team
SketchUp Team
 

Re: [CODE] Camera settings

Postby chrisglasier » Mon Feb 15, 2010 6:59 am

Yes guess what - fov is in mm by default!
Here's a kind of victory pic.
I amended the previous code in case anyone is thinking of doing something similarly lunatic.

Cheers.
0
Last edited by chrisglasier on Tue Feb 16, 2010 4:15 am, edited 2 times in total.
With JSON machines we can analyse what is to be achieved so that IT can help with automation to achieve it.
User avatar
chrisglasier 
PluginStore Author
PluginStore Author
 

Re: [CODE] Camera settings

Postby driven » Mon Feb 15, 2010 12:44 pm

chrisglasier wrote:Yes guess what - fov is in mm by default!
Here's a kind of victory pic.
I amended the previous code in case anyone is thinking of doing something similarly lunatic.

Cheers.

hi Chris,

I can't get the feb016.avi to run on any player, I'm on a Mac but don't normally have problems with .avi
can you out-put a quicktime.mov as well? Is it the same video that's up on youtube?
I'm very interested in your approach and although I'm only just starting to learn coding, I'd like to integrate namespace conventions into my efforts right from the start.

john
0
learn from the mistakes of others, you may not live long enough to make them all yourself...

driven 
PluginStore Author
PluginStore Author
 

Re: [CODE] Camera settings

Postby chrisglasier » Mon Feb 15, 2010 2:11 pm

Is it the same video that's up on youtube?


Yes. You could also look at my old website (button on left).

Now I'm off to the real movies for a while.

Interested in your interest.
0
With JSON machines we can analyse what is to be achieved so that IT can help with automation to achieve it.
User avatar
chrisglasier 
PluginStore Author
PluginStore Author
 

Parallel projection

Postby chrisglasier » Wed Feb 17, 2010 9:49 am

A couple of odd things I just discovered when doing something else is that whereas the perspectives are spot on the standard views are unstable. The top/plan views for example get some 164% bigger when imported. See pixs:
camera009.png
camera010.png

The other is that I .mm 'd the vector3ds for up. I changed them but it didn't make any difference. (Obviously I don't understand this stuff well enough.)
0
With JSON machines we can analyse what is to be achieved so that IT can help with automation to achieve it.
User avatar
chrisglasier 
PluginStore Author
PluginStore Author
 

Parallel projection

Postby chrisglasier » Wed Feb 17, 2010 10:58 am

And here's the matrix returns for one component in the original and recreated view.
camera011.png
0
With JSON machines we can analyse what is to be achieved so that IT can help with automation to achieve it.
User avatar
chrisglasier 
PluginStore Author
PluginStore Author
 

SketchUcation One-Liner Adverts

by Ad Machine » 5 minutes ago



Ad Machine 
Robot
 



 

Return to Developers' Forum

Who is online

Users browsing this forum: No registered users and 17 guests

Visit our sponsors: